Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
ViaResto-website
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
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
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aymeric Chaumont
ViaResto-website
Merge requests
!39
auth
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
auth
implement_auth
into
main
Overview
0
Commits
4
Pipelines
8
Changes
16
Merged
auth
Antoine Gaudron-Desjardins
requested to merge
implement_auth
into
main
Jul 13, 2022
Overview
0
Commits
4
Pipelines
8
Changes
16
0
0
Merge request reports
Compare
main
version 3
26709f86
Jul 13, 2022
version 2
0fc66c2c
Jul 13, 2022
version 1
7d99217c
Jul 13, 2022
main (base)
and
latest version
latest version
609bec7a
4 commits,
Jul 15, 2022
version 3
26709f86
3 commits,
Jul 13, 2022
version 2
0fc66c2c
2 commits,
Jul 13, 2022
version 1
7d99217c
1 commit,
Jul 13, 2022
16 files
+
349
−
102
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
16
backend/db/crud.py
+
75
−
13
View file @ 609bec7a
Edit in single-file editor
Open in Web IDE
Show full file
@@ -4,6 +4,8 @@ Module to interact with the database
from
datetime
import
date
,
datetime
,
time
,
timedelta
from
sqlalchemy.orm
import
Session
from
sqlalchemy.sql
import
func
from
uuid
import
uuid4
import
secrets
import
pytz
from
db
import
models
,
schemas
@@ -177,31 +179,33 @@ def get_current_graph(place: str, db: Session):
# Define CRUD operation for the comments
def
get_comments
(
place
:
str
,
page
:
int
,
db
:
Session
):
"""
Get
the
1
0 last comments for the given place
"""
"""
Get the
2
0 last comments for the given place
"""
if
page
==
0
:
comments
=
db
.
query
(
models
.
Comments
).
order_by
(
models
.
Comments
.
published_at
.
desc
(),
models
.
Comments
.
id
.
desc
()).
all
()
else
:
comments
=
db
.
query
(
models
.
Comments
).
filter
(
models
.
Comments
,
models
.
Users
.
username
).
join
(
models
.
Users
).
filter
(
models
.
Comments
.
place
==
place
).
order_by
(
models
.
Comments
.
published_at
.
desc
(),
models
.
Comments
.
id
.
desc
()).
slice
(
(
page
-
1
)
*
1
0
,
page
*
1
0
).
all
()
return
comments
models
.
Comments
.
published_at
.
desc
(),
models
.
Comments
.
id
.
desc
()).
slice
(
(
page
-
1
)
*
2
0
,
page
*
2
0
).
all
()
return
list
(
schemas
.
Comment
(
**
comment
.
__dict__
,
username
=
username
)
for
comment
,
username
in
comments
)
def
create_comment
(
place
:
str
,
new_comments
:
schemas
.
CommentBase
,
db
:
Session
):
def
create_comment
(
user
:
schemas
.
User
,
place
:
str
,
new_comments
:
schemas
.
CommentBase
,
db
:
Session
):
"""
Add a new comment to the database
"""
date
=
datetime
.
now
(
tz
=
pytz
.
timezone
(
"
Europe/Paris
"
))
db_comment
=
models
.
Comments
(
**
new_comments
.
dict
(),
published_at
=
date
,
place
=
place
)
db_comment
=
models
.
Comments
(
**
new_comments
.
dict
(),
published_at
=
date
,
place
=
place
,
user_id
=
user
.
id
)
db
.
add
(
db_comment
)
db
.
commit
()
db
.
refresh
(
db_comment
)
return
db_comment
return
schemas
.
Comment
(
**
db_comment
.
__dict__
,
username
=
user
.
username
)
def
delete_comment
(
id
:
int
,
db
:
Session
):
@@ -337,3 +341,61 @@ def get_restaurants(db: Session):
restaurants
.
append
(
restaurant
)
return
restaurants
# Define CRUD operation for the authentication
def
init_user
(
db
:
Session
):
"""
Add a news to the database
"""
cookie
=
uuid4
()
state
=
secrets
.
token_urlsafe
(
30
)
expiration_date
=
datetime
.
now
(
tz
=
pytz
.
timezone
(
"
Europe/Paris
"
))
+
timedelta
(
minutes
=
10
)
db_user
=
models
.
Users
(
state
=
state
,
cookie
=
cookie
,
expiration_date
=
expiration_date
)
db
.
add
(
db_user
)
db
.
commit
()
db
.
refresh
(
db_user
)
return
db_user
def
get_user
(
cookie
:
str
,
db
:
Session
):
"""
Get user infos
"""
user
=
db
.
query
(
models
.
Users
).
filter
(
models
.
Users
.
cookie
==
cookie
).
one
()
if
pytz
.
timezone
(
"
Europe/Paris
"
).
localize
(
user
.
expiration_date
)
<
datetime
.
now
(
tz
=
pytz
.
timezone
(
"
Europe/Paris
"
)):
return
return
user
def
delete_state
(
user
:
schemas
.
User
,
db
:
Session
):
"""
Delete the state of a user
"""
user
.
state
=
None
db
.
add
(
user
)
db
.
commit
()
def
update_user
(
user
:
schemas
.
User
,
user_info
:
dict
,
db
:
Session
):
full_name
=
f
"
{
user_info
[
'
firstName
'
]
}
{
user_info
[
'
lastName
'
]
}
"
expiration_date
=
datetime
.
now
(
tz
=
pytz
.
timezone
(
"
Europe/Paris
"
))
+
timedelta
(
days
=
3
)
existing_user
=
db
.
query
(
models
.
Users
).
filter
(
models
.
Users
.
username
==
full_name
).
first
()
if
existing_user
:
existing_user
.
cookie
=
user
.
cookie
existing_user
.
expiration_date
=
expiration_date
db
.
delete
(
user
)
db
.
add
(
existing_user
)
db
.
commit
()
db
.
refresh
(
existing_user
)
return
existing_user
else
:
user
.
username
=
full_name
user
.
expiration_date
=
expiration_date
db
.
add
(
user
)
db
.
commit
()
db
.
refresh
(
user
)
return
user
def
end_session
(
cookie
:
str
,
db
:
Session
):
user
=
db
.
query
(
models
.
Users
).
filter
(
models
.
Users
.
cookie
==
cookie
).
one
()
user
.
expiration_date
=
datetime
.
now
(
tz
=
pytz
.
timezone
(
"
Europe/Paris
"
))
db
.
add
(
user
)
db
.
commit
()
return
Loading