add user model
This commit is contained in:
parent
78aa2ce3f1
commit
88ed4d9886
@ -40,22 +40,16 @@ class Update(db.Model):
|
|||||||
default=int(time.time()*1000))
|
default=int(time.time()*1000))
|
||||||
|
|
||||||
|
|
||||||
def generate_key():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Editor(db.Model):
|
class Editor(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
description = db.Column(db.String(75), nullable=False)
|
|
||||||
authority = db.Column(db.Integer, nullable=False, default=1)
|
authority = db.Column(db.Integer, nullable=False, default=1)
|
||||||
# 0: Full Access
|
# 0: Full Access
|
||||||
# 1: Edit values and Add files
|
# 1: Below + create Updates
|
||||||
# 2: Edit values
|
# 2: Edit values and Add files
|
||||||
# 3: No Access
|
# 3: No Access
|
||||||
|
|
||||||
token = db.Column(db.String(32), nullable=False, default=generate_key)
|
username = db.Column(db.String(32), nullable=False)
|
||||||
date = db.Column(db.BigInteger, nullable=False,
|
password = db.Column(db.String(32), nullable=False)
|
||||||
default=int(time.time()*1000))
|
|
||||||
|
|
||||||
|
|
||||||
def database():
|
def database():
|
||||||
@ -72,6 +66,12 @@ def database():
|
|||||||
final['values'].append(languageobject)
|
final['values'].append(languageobject)
|
||||||
return final
|
return final
|
||||||
|
|
||||||
|
def check_privelege(doer, privelege):
|
||||||
|
if Editor.query.filter_by(username=doer['username'], password=doer['password']).first().authority <= privelege:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def phoneme_add(info):
|
def phoneme_add(info):
|
||||||
"""Add or edit value associated with phoneme."""
|
"""Add or edit value associated with phoneme."""
|
||||||
# info = {
|
# info = {
|
||||||
@ -162,28 +162,33 @@ def backend():
|
|||||||
# return jsonify(database())
|
# return jsonify(database())
|
||||||
|
|
||||||
# POST method appends input to database['values']
|
# POST method appends input to database['values']
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
received = request.get_json()
|
received = request.get_json()
|
||||||
language = Language(name=received['name'], source=received['source'])
|
|
||||||
db.session.add(language)
|
|
||||||
|
|
||||||
for phoneme, value in received['phonemes'].items():
|
if check_privelege(received['editor'], 2):
|
||||||
with db.session.no_autoflush:
|
language = Language(name=received['name'], source=received['source'])
|
||||||
search = Phoneme.query.filter_by(name=phoneme).first()
|
db.session.add(language)
|
||||||
if not search:
|
|
||||||
search = Phoneme(name=phoneme)
|
for phoneme, value in received['phonemes'].items():
|
||||||
db.session.add(search)
|
with db.session.no_autoflush:
|
||||||
link = Frequency(value=value, phoneme=search)
|
search = Phoneme.query.filter_by(name=phoneme).first()
|
||||||
language.phonemes.append(link)
|
if not search:
|
||||||
db.session.add(link)
|
search = Phoneme(name=phoneme)
|
||||||
db.session.commit()
|
db.session.add(search)
|
||||||
|
link = Frequency(value=value, phoneme=search)
|
||||||
|
language.phonemes.append(link)
|
||||||
|
db.session.add(link)
|
||||||
|
db.session.commit()
|
||||||
# return jsonify(database())
|
# return jsonify(database())
|
||||||
|
|
||||||
# PATCH method inputs edited language and returns updated database
|
# PATCH method inputs edited language and returns updated database
|
||||||
elif request.method == "PATCH":
|
elif request.method == "PATCH":
|
||||||
received = request.get_json()
|
received = request.get_json()
|
||||||
patch_functions[received['action']](received['data'])
|
|
||||||
db.session.commit()
|
if check_privelege(received['editor'], 2):
|
||||||
|
patch_functions[received['action']](received['data'])
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
return jsonify(database())
|
return jsonify(database())
|
||||||
|
|
||||||
@ -194,17 +199,19 @@ def updates():
|
|||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
received = request.get_json()
|
received = request.get_json()
|
||||||
update = Update(author=received['author'],
|
if check_privelege(received['editor'], 1):
|
||||||
title=received['title'],
|
update = Update(author=received['author'],
|
||||||
content=received['content'])
|
title=received['title'],
|
||||||
db.session.add(update)
|
content=received['content'])
|
||||||
|
db.session.add(update)
|
||||||
|
|
||||||
elif request.method == "PATCH":
|
elif request.method == "PATCH":
|
||||||
received = request.get_json()
|
received = request.get_json()
|
||||||
update = Update.query.filter_by(id=received['id']).first()
|
if check_privelege(received['editor'], 1):
|
||||||
update.name = received['author']
|
update = Update.query.filter_by(id=received['id']).first()
|
||||||
update.title = received['title']
|
update.name = received['author']
|
||||||
update.content = received['content']
|
update.title = received['title']
|
||||||
|
update.content = received['content']
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return jsonify([{"author": update.name,
|
return jsonify([{"author": update.name,
|
||||||
@ -214,6 +221,21 @@ def updates():
|
|||||||
"date": update.date}
|
"date": update.date}
|
||||||
for update in Update.query.all()])
|
for update in Update.query.all()])
|
||||||
|
|
||||||
|
# Manipulate Editor
|
||||||
|
@app.route("/editors", methods=["POST"])
|
||||||
|
def editors():
|
||||||
|
if request.method == "POST":
|
||||||
|
received = request.get_json()
|
||||||
|
doer = received['editor']
|
||||||
|
if Editor.query.filter_by(username=received[username].count()) == 0 and Editor.query.filter_by(username=doer['username'], password=doer['password']).count() == 1:
|
||||||
|
user = Editor(authority = received[authority],
|
||||||
|
username = received[username],
|
||||||
|
password = received[password])
|
||||||
|
return user
|
||||||
|
else:
|
||||||
|
return "Bad Request"
|
||||||
|
else:
|
||||||
|
return "Bad Request"
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0")
|
app.run(host="0.0.0.0")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user