-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
149 lines (112 loc) · 5.09 KB
/
controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from flask_restful import Resource, reqparse
from flask import jsonify
from models import *
from flask_jwt_extended import create_access_token, jwt_required, get_jwt_identity
import checker
import json
class UserRegister(Resource):
parser = reqparse.RequestParser()
parser.add_argument('email', type=str, help="Email is required", required=True)
parser.add_argument('password', type=str, help="Password is required", required=True)
def post(self):
data = UserRegister.parser.parse_args()
if User.find_by_email(data['email']):
return {'message': 'Email has already been taken'}, 400
user = User(**data)
user.hash_password(data['password'])
user.save_to_db()
return {'message': 'User has been created successfully'}, 201
class Login(Resource):
parser = reqparse.RequestParser()
parser.add_argument('email', type=str, help="Email is required", required=True)
parser.add_argument('password', type=str, help="Password is required", required=True)
def post(self):
data = Login.parser.parse_args()
user = User.find_by_email(data['email'])
if not user or not user.verify_password(data['password']):
return {'message': 'Wrong Email or Password'}, 404
access_token = create_access_token(identity=data['email'])
return jsonify(message="Login Succeeded!", access_token=access_token)
class Profile(Resource):
@jwt_required()
def get(self):
current_user = get_jwt_identity()
user = User.find_by_email(current_user)
user_schema = UserSchema()
output = user_schema.dump(user)
return jsonify(output)
class RepoInfo(Resource):
parser = reqparse.RequestParser()
parser.add_argument('url', type=str, help="URL is required", required=True)
parser.add_argument('dev', type=str, help="Dev is required", required=True)
@jwt_required()
def get(self):
current_user = get_jwt_identity()
user = User.find_by_email(current_user)
repo_schema = RepoSchema(many=True)
output = repo_schema.dump(user.repos)
return jsonify(output)
@jwt_required()
def post(self):
data = RepoInfo.parser.parse_args()
current_user = get_jwt_identity()
user = User.find_by_email(current_user)
repo = Repo(url = data["url"], userId=user.userId)
repo.save_to_db()
user.add_repo(repo)
dev = data["dev"]
if dev == "true" or dev == "True":
devdep = True
else:
devdep = False
output = checker.ILC(data["url"], devdep)
json_string = json.dumps(output,skipkeys = True,allow_nan = True) #json to string
check = Check(repoId=repo.repoId, inconsistency=json_string)
check.save_to_db()
repo.add_check(check)
out_json = json.loads(json_string) #string to json
string_license = checker.stringInconsistencies(out_json)
return jsonify({"tree": out_json, "str":string_license})
#old repo, get old repos check and update repo's check
class OldRepo(Resource):
parser = reqparse.RequestParser()
parser.add_argument('dev', type=str, help="Dev is required", required=True)
@jwt_required()
def get(self, id): #sorun: başka userın reposuna istek atılabiliyor
repo = Repo.find_by_id(id)
if not repo:
return {"message": "Repo Not Found"}, 404
out_json = json.loads(repo.checks[0].inconsistency) #string to json
string_license = checker.stringInconsistencies(out_json)
return jsonify({"tree": out_json, "str":string_license})
def post(self, id):
data = OldRepo.parser.parse_args()
repo = Repo.find_by_id(id)
if not repo:
return {"message": "Repo Not Found"}, 404
dev = data["dev"]
if dev == "true" or dev == "True":
devdep = True
else:
devdep = False
output = checker.ILC(repo.url, devdep)
json_string = json.dumps(output,skipkeys = True,allow_nan = True) #json to string
repo.checks[0].inconsistency = json_string
repo.commit()
string_license = checker.stringInconsistencies(output)
return jsonify({"tree": output, "str":string_license})
#change password
class PasswordChange(Resource):
parser = reqparse.RequestParser()
parser.add_argument('old_password', type=str, help="Old Password is required", required=True)
parser.add_argument('new_password', type=str, help="New Password is required", required=True)
@jwt_required()
def post(self):
data = PasswordChange.parser.parse_args()
current_user = get_jwt_identity()
user = User.find_by_email(current_user)
if not user.verify_password(data['old_password']):
return {'message': 'Currently Password is wrong'}, 404
user.hash_password(data['new_password'])
user.commit()
return {'message': 'Password changed successfully'}, 200