Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CM2305 Group 17 Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ewan Crowle
CM2305 Group 17 Project
Commits
a3aad89d
Commit
a3aad89d
authored
3 months ago
by
Jeyan Kanagaratnam
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
bf5c764d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
final_version/flasktest.py
+74
-0
74 additions, 0 deletions
final_version/flasktest.py
with
74 additions
and
0 deletions
final_version/flasktest.py
0 → 100644
+
74
−
0
View file @
a3aad89d
from
flask
import
Flask
,
render_template
,
jsonify
,
request
from
sqlalchemy
import
create_engine
from
sqlalchemy.orm
import
sessionmaker
from
models
import
Base
,
Return
,
Customer
,
Product
from
datetime
import
datetime
app
=
Flask
(
__name__
)
# Database configuration
SQLALCHEMY_DATABASE_URL
=
"
sqlite:///./local.db
"
engine
=
create_engine
(
SQLALCHEMY_DATABASE_URL
)
SessionLocal
=
sessionmaker
(
autocommit
=
False
,
autoflush
=
False
,
bind
=
engine
)
@app.route
(
'
/
'
)
def
index
():
return
render_template
(
'
index.html
'
)
@app.route
(
'
/returns
'
)
def
get_returns
():
start_date
=
request
.
args
.
get
(
'
start_date
'
)
end_date
=
request
.
args
.
get
(
'
end_date
'
)
db
=
SessionLocal
()
try
:
# Build query
query
=
db
.
query
(
Return
)
# Add date filtering
if
start_date
:
query
=
query
.
filter
(
Return
.
return_date
>=
datetime
.
fromisoformat
(
start_date
))
if
end_date
:
query
=
query
.
filter
(
Return
.
return_date
<=
datetime
.
fromisoformat
(
end_date
))
# Get return records
returns
=
query
.
all
()
# Calculate statistics
total_returns
=
len
(
returns
)
total_points
=
sum
(
return_item
.
reward_value
for
return_item
in
returns
)
active_users
=
len
(
set
(
return_item
.
customer_id
for
return_item
in
returns
))
# Calculate product statistics
product_stats
=
{}
for
return_item
in
returns
:
product_stats
[
return_item
.
product_id
]
=
product_stats
.
get
(
return_item
.
product_id
,
0
)
+
1
# Calculate weekly statistics
weekly_stats
=
{}
for
return_item
in
returns
:
day_of_week
=
return_item
.
return_date
.
weekday
()
weekly_stats
[
day_of_week
]
=
weekly_stats
.
get
(
day_of_week
,
0
)
+
1
return
jsonify
({
'
returns
'
:
[
{
'
id
'
:
r
.
id
,
'
customer_id
'
:
r
.
customer_id
,
'
product_id
'
:
r
.
product_id
,
'
reward_value
'
:
r
.
reward_value
,
'
return_date
'
:
r
.
return_date
.
isoformat
()
}
for
r
in
returns
],
'
total_returns
'
:
total_returns
,
'
total_points
'
:
total_points
,
'
active_users
'
:
active_users
,
'
product_stats
'
:
product_stats
,
'
weekly_stats
'
:
weekly_stats
})
finally
:
db
.
close
()
if
__name__
==
'
__main__
'
:
app
.
run
(
debug
=
True
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment