1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-12-25 04:24:19 +00:00
pds-2021-g2-nest/nest_backend/routes/repository/repositories.py

123 lines
4.9 KiB
Python
Raw Normal View History

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 *
import datetime
from flask_cors import cross_origin
import nest_backend.errors as errors
from nest_crawler.repo_search import search_repo_conditions
2021-05-28 12:07:26 +00:00
import threading
def tweet_importer(rid):
2021-05-28 14:16:09 +00:00
import nest_backend.app
2021-05-28 12:07:26 +00:00
with nest_backend.app.app.app_context():
search_repo_conditions(rid)
@cross_origin()
@jwt_required()
def page_repositories():
"""
---
get:
summary: Get a list of repositories.
security:
- jwt: []
responses:
'200':
2021-05-12 15:32:37 +00:00
description: The list of the repositories related to the user, incapsulated in Success.
'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
post:
summary: Creates a repository.
security:
- jwt: []
requestBody:
required: true
content:
application/json:
schema: Repository
responses:
'201':
description: The user has been created successfully.
content:
application/json:
schema: Repository
'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
"""
user = find_user(get_jwt_identity())
if request.method == "GET":
owner = Repository.query.filter_by(owner_id=user.email)
spectator = Authorization.query.filter_by(email=user.email).join(Repository).filter_by(is_deleted=False)
2021-04-30 13:48:05 +00:00
if request.args.get("onlyActive"):
owner = owner.filter_by(is_active=True)
2021-05-06 15:20:07 +00:00
spectator = spectator.filter(Repository.is_active)
2021-04-30 13:48:05 +00:00
elif request.args.get("onlyDead"):
owner = owner.filter_by(is_active=False)
2021-05-06 15:20:07 +00:00
spectator = spectator.filter(not Repository.is_active)
owner = owner.all()
spectator = spectator.all()
return json_success([r.to_json() for r in owner if not r.is_deleted] + [r.repository.to_json() for r in spectator if not r.repository.is_deleted])
elif request.method == "POST":
# Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now.
2021-05-13 22:13:33 +00:00
if not request.json.get("name") or not request.json.get("conditions") or not str(
request.json.get("evaluation_mode")):
return json_error("Missing arguments.", errors.GENERIC_MISSING_FIELDS), 400
name = request.json.get("name")
try:
evaluation_mode = ConditionMode(request.json['evaluation_mode'])
except KeyError:
return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
2021-05-24 23:09:10 +00:00
except Exception as e:
return json_error("Unknown error: " + str(e)), 400
repository = Repository(name=name, owner_id=user.email, is_active=False, evaluation_mode=evaluation_mode)
2021-05-07 17:46:14 +00:00
ext.session.add(repository)
ext.session.commit()
conditions = [c for c in repository.conditions if c.id not in [a['id'] for a in request.json['conditions'] if
a['id'] in [b.id for b in
repository.conditions]]]
for c in conditions:
ext.session.delete(c)
ext.session.commit()
# Create brand new conditions
for c in request.json['conditions']:
if not c['id']:
try:
type_ = ConditionType(c['type'])
except KeyError:
return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
ext.session.add(Condition(type=type_, content=c['content'], repository_id=repository.id))
ext.session.commit()
repository.is_active = True
repository.start = datetime.datetime.now()
ext.session.commit()
try:
2021-05-28 12:07:26 +00:00
thread = threading.Thread(target=tweet_importer, args=(repository.id,))
thread.start()
except Exception:
return json_success(repository.to_json()), 201
return json_success(repository.to_json()), 201