2021-04-29 19:59:05 +00:00
|
|
|
from flask import render_template, abort, jsonify, request
|
2021-05-05 20:21:04 +00:00
|
|
|
from nest_backend.database import *
|
2021-05-12 20:22:42 +00:00
|
|
|
from flask_jwt_extended import jwt_required, get_jwt_identity
|
2021-05-05 20:21:04 +00:00
|
|
|
from nest_backend.gestione import *
|
2021-04-29 19:59:05 +00:00
|
|
|
from flask_cors import cross_origin
|
|
|
|
import datetime
|
2021-05-17 14:19:32 +00:00
|
|
|
from nest_backend.errors import *
|
2021-04-29 19:59:05 +00:00
|
|
|
|
2021-05-12 20:22:42 +00:00
|
|
|
|
2021-04-29 19:59:05 +00:00
|
|
|
@cross_origin()
|
|
|
|
@jwt_required()
|
|
|
|
@repository_auth
|
|
|
|
def page_repository(rid):
|
|
|
|
"""
|
2021-05-05 20:45:36 +00:00
|
|
|
---
|
|
|
|
get:
|
2021-05-06 01:07:34 +00:00
|
|
|
summary: Get details about a repository.
|
2021-05-05 20:45:36 +00:00
|
|
|
parameters:
|
|
|
|
- in: path
|
|
|
|
schema: IntegerParameterSchema
|
2021-05-06 09:25:39 +00:00
|
|
|
security:
|
|
|
|
- jwt: []
|
2021-05-05 20:45:36 +00:00
|
|
|
responses:
|
|
|
|
'200':
|
|
|
|
description: The details about the requested schema. The schema is incapsulated in Success.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Repository
|
|
|
|
'404':
|
|
|
|
description: Could not find the requested repository.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'403':
|
|
|
|
description: The user is not authorized.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'401':
|
|
|
|
description: The user is not logged in.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
tags:
|
|
|
|
- repository-related
|
|
|
|
delete:
|
2021-05-06 01:07:34 +00:00
|
|
|
summary: Deletes a repository.
|
2021-05-05 20:45:36 +00:00
|
|
|
parameters:
|
|
|
|
- in: path
|
|
|
|
schema: IntegerParameterSchema
|
2021-05-06 09:25:39 +00:00
|
|
|
security:
|
|
|
|
- jwt: []
|
2021-05-05 20:45:36 +00:00
|
|
|
responses:
|
2021-05-11 17:08:14 +00:00
|
|
|
'204':
|
2021-05-05 20:45:36 +00:00
|
|
|
description: The repository has been deleted successfully.
|
|
|
|
'404':
|
|
|
|
description: Could not find the requested repository.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'403':
|
|
|
|
description: The user is not authorized.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'401':
|
|
|
|
description: The user is not logged in.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'500':
|
|
|
|
description: Could not delete the repository.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
tags:
|
|
|
|
- repository-related
|
|
|
|
patch:
|
2021-05-06 01:07:34 +00:00
|
|
|
summary: Updates a repository.
|
2021-05-06 09:25:39 +00:00
|
|
|
security:
|
|
|
|
- jwt: []
|
2021-05-05 20:45:36 +00:00
|
|
|
requestBody:
|
|
|
|
required: true
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: RepositoryUpdate
|
|
|
|
parameters:
|
|
|
|
- in: path
|
|
|
|
schema: IntegerParameterSchema
|
|
|
|
|
|
|
|
responses:
|
2021-05-11 17:08:14 +00:00
|
|
|
'204':
|
2021-05-05 20:45:36 +00:00
|
|
|
description: The repository has been updated successfully.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Repository
|
|
|
|
'404':
|
|
|
|
description: Could not find the requested repository.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'403':
|
|
|
|
description: The user is not authorized.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'401':
|
|
|
|
description: The user is not logged in.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
tags:
|
|
|
|
- repository-related
|
2021-05-06 15:20:07 +00:00
|
|
|
put:
|
|
|
|
summary: Overwrites a repository.
|
|
|
|
security:
|
|
|
|
- jwt: []
|
|
|
|
requestBody:
|
|
|
|
required: true
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Repository
|
|
|
|
parameters:
|
|
|
|
- in: path
|
|
|
|
schema: IntegerParameterSchema
|
|
|
|
|
|
|
|
responses:
|
|
|
|
'200':
|
|
|
|
description: The repository has been updated successfully.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Repository
|
|
|
|
'404':
|
|
|
|
description: Could not find the requested repository.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'403':
|
|
|
|
description: The user is not authorized.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'401':
|
|
|
|
description: The user is not logged in.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
'400':
|
|
|
|
description: The request was malformed.
|
|
|
|
content:
|
|
|
|
application/json:
|
|
|
|
schema: Error
|
|
|
|
tags:
|
|
|
|
- repository-related
|
2021-04-29 19:59:05 +00:00
|
|
|
"""
|
|
|
|
user = find_user(get_jwt_identity())
|
2021-04-29 20:16:37 +00:00
|
|
|
repository = Repository.query.filter_by(id=rid).first()
|
2021-05-01 12:25:50 +00:00
|
|
|
if not repository:
|
2021-05-17 14:19:32 +00:00
|
|
|
return json_error("Could not find repository.", REPOSITORY_NOT_FOUND), 404
|
2021-04-29 19:59:05 +00:00
|
|
|
if request.method == "GET":
|
|
|
|
return json_success(repository.to_json()), 200
|
|
|
|
elif request.method == "PATCH":
|
|
|
|
if repository.owner_id != user.email:
|
2021-05-17 14:19:32 +00:00
|
|
|
return json_error("You are not the owner of this repository.", REPOSITORY_NOT_OWNER), 403
|
2021-04-29 19:59:05 +00:00
|
|
|
if 'name' in request.json:
|
|
|
|
repository.name = request.json['name']
|
2021-05-06 15:20:07 +00:00
|
|
|
if 'close' in request.json and not repository.end and repository.is_active:
|
2021-04-29 19:59:05 +00:00
|
|
|
repository.end = datetime.datetime.now()
|
2021-05-06 15:20:07 +00:00
|
|
|
repository.is_active = False
|
|
|
|
if 'open' in request.json and not repository.is_active and not repository.end:
|
|
|
|
repository.is_active = True
|
|
|
|
if 'evaluation_mode' in request.json:
|
|
|
|
try:
|
2021-05-10 14:25:34 +00:00
|
|
|
evaluation_mode = ConditionMode(request.json['evaluation_mode'])
|
2021-05-06 15:20:07 +00:00
|
|
|
except KeyError:
|
2021-05-17 14:19:32 +00:00
|
|
|
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400
|
2021-05-06 15:20:07 +00:00
|
|
|
repository.evaluation_mode = evaluation_mode
|
2021-05-07 17:46:14 +00:00
|
|
|
ext.session.commit()
|
2021-05-11 17:08:14 +00:00
|
|
|
return json_success(repository.to_json()), 204
|
2021-04-29 19:59:05 +00:00
|
|
|
elif request.method == "DELETE":
|
2021-05-01 12:25:50 +00:00
|
|
|
if repository.owner_id != user.email and not user.isAdmin:
|
2021-05-17 14:19:32 +00:00
|
|
|
return json_error("You are not the owner of this repository.", REPOSITORY_NOT_OWNER), 403
|
2021-05-01 12:25:50 +00:00
|
|
|
try:
|
2021-05-27 09:27:23 +00:00
|
|
|
# Deleting Tweets...
|
|
|
|
tweets = [t.tweet for t in repository.tweets]
|
|
|
|
for tweet in tweets:
|
|
|
|
if len(tweet.repositories) < 2:
|
|
|
|
# Delete Tweets if only attached to this repo
|
|
|
|
for conn in tweet.repositories:
|
|
|
|
ext.session.delete(conn)
|
|
|
|
for conn in tweet.conditions:
|
|
|
|
ext.session.delete(conn)
|
|
|
|
ext.session.delete(tweet)
|
|
|
|
ext.session.commit()
|
|
|
|
else:
|
|
|
|
conns = [conn for conn in tweet.repositories if conn.rid == rid]
|
|
|
|
for conn in conns:
|
|
|
|
ext.session.delete(conn)
|
|
|
|
ext.session.commit()
|
|
|
|
# Deleting authorizations...
|
|
|
|
for auth in repository.authorizations:
|
|
|
|
ext.session.delete(auth)
|
|
|
|
ext.session.commit()
|
|
|
|
# Deleting conditions...
|
|
|
|
for condition in repository.conditions:
|
2021-05-27 09:34:38 +00:00
|
|
|
for c in condition.tweets:
|
|
|
|
ext.session.delete(c)
|
|
|
|
for c in condition.alerts:
|
|
|
|
ext.session.delete(c)
|
|
|
|
ext.session.commit()
|
2021-05-27 09:27:23 +00:00
|
|
|
ext.session.delete(condition)
|
|
|
|
ext.session.commit()
|
|
|
|
# Deleting Alerts...
|
|
|
|
for alert in repository.alerts:
|
|
|
|
for elem in alert.conditions:
|
|
|
|
condition = elem.condition
|
|
|
|
ext.session.delete(elem)
|
|
|
|
ext.session.commit()
|
|
|
|
if not condition.repository_id:
|
|
|
|
ext.session.delete(condition)
|
|
|
|
ext.session.commit()
|
|
|
|
for notification in alert.notifications:
|
|
|
|
ext.session.delete(notification)
|
|
|
|
ext.session.commit()
|
|
|
|
ext.session.delete(alert)
|
|
|
|
ext.session.commit()
|
2021-05-07 17:46:14 +00:00
|
|
|
ext.session.delete(repository)
|
|
|
|
ext.session.commit()
|
2021-05-01 12:25:50 +00:00
|
|
|
except Exception as e:
|
2021-05-07 17:46:14 +00:00
|
|
|
ext.session.rollback()
|
2021-05-17 14:19:32 +00:00
|
|
|
return json_error("Cant delete repository because of dependencies.", REPOSITORY_DEPENDENCY_FAILURE), 500
|
2021-05-11 17:08:14 +00:00
|
|
|
return json_success("Success"), 204
|
2021-05-06 15:20:07 +00:00
|
|
|
elif request.method == "PUT":
|
|
|
|
if not json_request_authorizer(request.json, repository):
|
2021-05-17 14:19:32 +00:00
|
|
|
return json_error("Missing one or more parameters in repository json.", GENERIC_MISSING_FIELDS), 400
|
2021-05-06 15:20:07 +00:00
|
|
|
# Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now.
|
|
|
|
try:
|
2021-05-11 16:45:40 +00:00
|
|
|
evaluation_mode = ConditionMode(request.json['evaluation_mode'])
|
2021-05-06 15:20:07 +00:00
|
|
|
except KeyError:
|
2021-05-17 14:19:32 +00:00
|
|
|
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400
|
2021-05-11 16:45:40 +00:00
|
|
|
repository.evaluation_mode = evaluation_mode
|
2021-05-06 15:20:07 +00:00
|
|
|
repository.name = request.json['name']
|
|
|
|
repository.is_active = request.json['is_active']
|
2021-05-20 11:48:15 +00:00
|
|
|
ext.session.commit()
|
2021-05-06 15:20:07 +00:00
|
|
|
ids = [c['id'] for c in request.json['conditions'] if c['id']]
|
|
|
|
# Delete no longer needed conditions.
|
|
|
|
for c in repository.conditions:
|
|
|
|
if c.id not in ids:
|
2021-05-07 17:46:14 +00:00
|
|
|
ext.session.delete(c)
|
2021-05-06 15:20:07 +00:00
|
|
|
# Create brand new conditions
|
|
|
|
for c in request.json['conditions']:
|
|
|
|
if not c['id']:
|
2021-05-11 16:45:40 +00:00
|
|
|
try:
|
|
|
|
type_ = ConditionType(c['type'])
|
|
|
|
except KeyError:
|
2021-05-17 14:19:32 +00:00
|
|
|
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400
|
2021-05-12 20:22:42 +00:00
|
|
|
content = c['content']
|
|
|
|
if type_ == ConditionType.hashtag:
|
|
|
|
content = hashtag_validator(content)
|
|
|
|
ext.session.add(Condition(type=type_, content=content, repository_id=rid))
|
2021-05-19 22:45:00 +00:00
|
|
|
ext.session.commit()
|
2021-05-06 15:20:07 +00:00
|
|
|
return json_success(repository.to_json()), 200
|