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
|
|
|
import datetime
|
|
|
|
from flask_cors import cross_origin
|
2021-05-17 14:19:32 +00:00
|
|
|
from nest_backend.errors import *
|
2021-05-28 09:19:40 +00:00
|
|
|
from nest_crawler.repo_search import search_repo_conditions
|
2021-04-29 19:59:05 +00:00
|
|
|
|
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)
|
|
|
|
|
2021-04-29 19:59:05 +00:00
|
|
|
|
|
|
|
@cross_origin()
|
|
|
|
@jwt_required()
|
|
|
|
def page_repositories():
|
|
|
|
"""
|
2021-05-06 09:25:39 +00:00
|
|
|
---
|
|
|
|
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.
|
2021-05-06 09:25:39 +00:00
|
|
|
'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:
|
2021-05-11 17:08:14 +00:00
|
|
|
schema: Repository
|
2021-05-06 09:25:39 +00:00
|
|
|
responses:
|
2021-05-11 17:08:14 +00:00
|
|
|
'201':
|
2021-05-06 09:25:39 +00:00
|
|
|
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
|
2021-04-29 19:59:05 +00:00
|
|
|
"""
|
|
|
|
user = find_user(get_jwt_identity())
|
|
|
|
if request.method == "GET":
|
|
|
|
owner = Repository.query.filter_by(owner_id=user.email)
|
2021-05-28 09:19:40 +00:00
|
|
|
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"):
|
2021-05-07 14:59:21 +00:00
|
|
|
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"):
|
2021-05-07 14:59:21 +00:00
|
|
|
owner = owner.filter_by(is_active=False)
|
2021-05-06 15:20:07 +00:00
|
|
|
spectator = spectator.filter(not Repository.is_active)
|
2021-04-29 19:59:05 +00:00
|
|
|
owner = owner.all()
|
|
|
|
spectator = spectator.all()
|
2021-05-28 09:45:56 +00:00
|
|
|
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])
|
2021-04-29 19:59:05 +00:00
|
|
|
elif request.method == "POST":
|
2021-05-11 17:08:14 +00:00
|
|
|
# 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")):
|
2021-05-17 14:19:32 +00:00
|
|
|
return json_error("Missing arguments.", GENERIC_MISSING_FIELDS), 400
|
2021-04-29 19:59:05 +00:00
|
|
|
name = request.json.get("name")
|
2021-05-11 17:08:14 +00:00
|
|
|
try:
|
|
|
|
evaluation_mode = ConditionMode(request.json['evaluation_mode'])
|
2021-05-17 14:19:32 +00:00
|
|
|
except KeyError:
|
|
|
|
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400
|
2021-05-24 23:09:10 +00:00
|
|
|
except Exception as e:
|
|
|
|
return json_error("Unknown error: " + str(e)), 400
|
2021-05-11 17:08:14 +00:00
|
|
|
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()
|
2021-05-17 14:19:32 +00:00
|
|
|
conditions = [c for c in repository.conditions if c.id not in [a['id'] for a in request.json['conditions'] if
|
2021-05-28 09:19:40 +00:00
|
|
|
a['id'] in [b.id for b in
|
|
|
|
repository.conditions]]]
|
2021-05-17 14:19:32 +00:00
|
|
|
for c in conditions:
|
|
|
|
ext.session.delete(c)
|
|
|
|
ext.session.commit()
|
2021-05-11 17:08:14 +00:00
|
|
|
# Create brand new conditions
|
|
|
|
for c in request.json['conditions']:
|
|
|
|
if not c['id']:
|
|
|
|
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-11 17:08:14 +00:00
|
|
|
ext.session.add(Condition(type=type_, content=c['content'], repository_id=repository.id))
|
|
|
|
ext.session.commit()
|
2021-05-11 17:26:44 +00:00
|
|
|
repository.is_active = True
|
|
|
|
repository.start = datetime.datetime.now()
|
|
|
|
ext.session.commit()
|
2021-05-28 09:19:40 +00:00
|
|
|
try:
|
2021-05-28 12:07:26 +00:00
|
|
|
thread = threading.Thread(target=tweet_importer, args=(repository.id,))
|
|
|
|
thread.start()
|
2021-05-28 09:19:40 +00:00
|
|
|
except Exception:
|
|
|
|
return json_success(repository.to_json()), 201
|
2021-05-11 17:08:14 +00:00
|
|
|
return json_success(repository.to_json()), 201
|