Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TDDD27_2019_codabify
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
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
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jennifer Lindgren
TDDD27_2019_codabify
Commits
cccb2d39
Commit
cccb2d39
authored
6 years ago
by
Jennifer Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
Backend: Models and methods for adding project data to database.
parent
6aa55b05
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
backend/__init__.py
+1
-1
1 addition, 1 deletion
backend/__init__.py
backend/app.py
+39
-0
39 additions, 0 deletions
backend/app.py
backend/database.py
+95
-3
95 additions, 3 deletions
backend/database.py
backend/db.sqlite
+0
-0
0 additions, 0 deletions
backend/db.sqlite
with
135 additions
and
4 deletions
backend/__init__.py
+
1
−
1
View file @
cccb2d39
...
...
@@ -19,6 +19,6 @@ class JSONEncoder(json.JSONEncoder):
app
=
Flask
(
__name__
)
app
.
config
[
'
JWT_SECRET_KEY
'
]
=
os
.
environ
.
get
(
'
SECRET
'
)
app
.
config
[
'
JWT_ACCESS_TOKEN_EXPIRES
'
]
=
datetime
.
timedelta
(
day
s
=
1
)
app
.
config
[
'
JWT_ACCESS_TOKEN_EXPIRES
'
]
=
datetime
.
timedelta
(
minute
s
=
1
0
)
app
.
json_encoder
=
JSONEncoder
CORS
(
app
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
backend/app.py
+
39
−
0
View file @
cccb2d39
...
...
@@ -40,6 +40,18 @@ def generate_response(response, status, headers={}):
def
is_valid_password
(
password
):
return
len
(
password
)
>=
8
and
len
(
password
)
<=
100
def
is_valid_collaborators
(
collaborators
):
if
not
collaborators
:
return
True
acceptedPermissions
=
[
'
viewonly
'
,
'
edit
'
]
for
collaborator
in
collaborators
:
if
(
not
collaborator
or
not
db
.
is_user_id
(
collaborator
[
'
userId
'
])):
return
False
for
permission
in
collaborator
[
'
permissions
'
]:
if
(
not
permission
in
acceptedPermissions
):
return
False
return
True
@jwt.unauthorized_loader
def
unauthorized_response
(
callback
):
return
jsonify
({
...
...
@@ -203,6 +215,33 @@ def delete_user_request(id):
return
generate_response
(
db
.
user_schema
.
jsonify
(
db
.
delete_user
(
id
)),
OK_STATUS_CODE
)
@app.route
(
'
/api/add_project
'
,
methods
=
[
'
POST
'
])
@jwt_required
def
new_project
():
title
=
request
.
json
[
'
title
'
]
creatorId
=
request
.
json
[
'
creatorId
'
]
collaborators
=
request
.
json
[
'
collaborators
'
]
if
request
.
json
[
'
collaborators
'
]
else
None
if
(
not
title
or
not
creatorId
):
return
generate_response
(
jsonify
({
'
success
'
:
False
,
'
message
'
:
'
Bad request.
'
}),
BAD_REQUEST_STATUS_CODE
)
elif
(
not
db
.
is_user_id
(
creatorId
)):
return
generate_response
(
jsonify
({
'
success
'
:
False
,
'
message
'
:
'
No user with that id exists
'
}),
BAD_REQUEST_STATUS_CODE
)
elif
(
not
is_valid_collaborators
(
collaborators
)):
return
generate_response
(
jsonify
({
'
success
'
:
False
,
'
message
'
:
'
Invalid collaborators
'
}),
BAD_REQUEST_STATUS_CODE
)
else
:
new_project
=
db
.
Project
(
title
,
creatorId
,
str
(
json
.
dumps
(
collaborators
)))
db
.
add_project
(
new_project
)
return
generate_response
(
db
.
project_schema
.
jsonify
(
new_project
),
OK_STATUS_CODE
)
if
__name__
==
'
__main__
'
:
app
.
run
(
debug
=
True
)
...
...
This diff is collapsed.
Click to expand it.
backend/database.py
+
95
−
3
View file @
cccb2d39
...
...
@@ -2,6 +2,7 @@ import os
from
__init__
import
app
from
datetime
import
datetime
from
flask
import
Flask
,
request
,
jsonify
from
flask_sqlalchemy
import
SQLAlchemy
from
flask_marshmallow
import
Marshmallow
...
...
@@ -15,7 +16,7 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db
=
SQLAlchemy
(
app
)
ma
=
Marshmallow
(
app
)
# User
class/model
# User
class
User
(
db
.
Model
):
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
email
=
db
.
Column
(
db
.
String
(
100
),
unique
=
True
)
...
...
@@ -27,15 +28,65 @@ class User(db.Model):
self
.
username
=
username
self
.
password
=
password
# User schema
class
UserSchema
(
ma
.
Schema
):
class
Meta
:
fields
=
(
'
id
'
,
'
email
'
,
'
username
'
)
# Init user schema
user_schema
=
UserSchema
(
strict
=
True
)
users_schema
=
UserSchema
(
many
=
True
,
strict
=
True
)
# Project
class
Project
(
db
.
Model
):
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
title
=
db
.
Column
(
db
.
String
(
100
),
nullable
=
False
)
creatorId
=
db
.
Column
(
db
.
Integer
,
db
.
ForeignKey
(
'
user.id
'
),
nullable
=
False
)
collaborators
=
db
.
Column
(
db
.
Text
)
created
=
db
.
Column
(
db
.
DateTime
,
nullable
=
False
,
default
=
datetime
.
utcnow
)
edited
=
db
.
Column
(
db
.
DateTime
)
files
=
db
.
Column
(
db
.
Text
)
messages
=
db
.
Column
(
db
.
Text
)
archived
=
db
.
Column
(
db
.
Boolean
,
default
=
False
)
def
__init__
(
self
,
title
,
creatorId
,
collaborators
=
None
,
created
=
None
,
edited
=
None
,
files
=
None
,
messages
=
None
,
archived
=
False
):
self
.
title
=
title
self
.
creatorId
=
creatorId
self
.
collaborators
=
collaborators
self
.
created
=
created
self
.
edited
=
edited
self
.
files
=
files
self
.
messages
=
messages
self
.
archived
=
archived
class
ProjectSchema
(
ma
.
Schema
):
class
Meta
:
fields
=
(
'
id
'
,
'
title
'
,
'
creatorId
'
,
'
collaborators
'
,
'
created
'
,
'
edited
'
,
'
files
'
,
'
messages
'
,
'
archived
'
)
project_schema
=
ProjectSchema
(
strict
=
True
)
projects_schema
=
ProjectSchema
(
many
=
True
,
strict
=
True
)
# Collaborator
class
Collaborator
():
def
__init__
(
self
,
userId
,
permissions
):
self
.
userId
=
userId
self
.
permissions
=
permissions
# File
class
File
():
def
__init__
(
self
,
name
,
content
,
comments
):
self
.
name
=
name
self
.
content
=
content
self
.
comments
=
comments
# Message
class
Message
():
def
__init__
(
self
,
message
,
authorId
,
time
):
self
.
message
=
message
self
.
authodId
=
authodId
self
.
time
=
time
# Methods
def
add_user
(
new_user
):
db
.
session
.
add
(
new_user
)
db
.
session
.
commit
()
...
...
@@ -68,6 +119,9 @@ def delete_all_users():
db
.
session
.
commit
()
return
num_users_deleted
def
is_user_id
(
creatorId
):
return
creatorId
!=
None
and
bool
(
User
.
query
.
get
(
creatorId
))
def
is_unregistered_email
(
email
):
return
not
bool
(
get_user_from_email
(
email
))
...
...
@@ -98,3 +152,41 @@ def change_password(username, newPassword):
user
.
password
=
newPassword
db
.
session
.
commit
()
return
user
def
add_project
(
project
):
db
.
session
.
add
(
project
)
db
.
session
.
commit
()
def
get_projects
():
all_projects
=
Project
.
query
.
all
()
result
=
projects_schema
.
dump
(
all_projects
)
return
result
.
data
def
get_project
(
id
):
project
=
Project
.
query
.
get
(
id
)
return
project
def
update_project
(
id
,
updated_project
):
project
=
Project
.
query
.
get
(
id
)
project
.
title
=
update_project
.
title
project
.
creatorId
=
update_project
.
creatorId
project
.
collaborators
=
update_project
.
collaborators
project
.
created
=
update_project
.
created
project
.
edited
=
update_project
.
edited
project
.
files
=
update_project
.
files
project
.
messages
=
update_project
.
messages
project
.
archived
=
update_project
.
archived
db
.
session
.
commit
()
return
project
def
delete_project
(
id
):
project
=
Project
.
query
.
get
(
id
)
db
.
session
.
delete
(
project
)
db
.
session
.
commit
()
return
project
def
delete_all_projects
():
num_projects_deleted
=
db
.
session
.
query
(
Project
).
delete
()
db
.
session
.
commit
()
return
num_projects_deleted
This diff is collapsed.
Click to expand it.
backend/db.sqlite
+
0
−
0
View file @
cccb2d39
No preview for this file type
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