2017-08-31 16:37:58 +00:00
|
|
|
from flask import Flask, Blueprint, request
|
|
|
|
|
from flask_security import (SQLAlchemyUserDatastore, login_required,
|
|
|
|
|
current_user, Security)
|
|
|
|
|
from flask_security.utils import verify_and_update_password, login_user
|
2017-08-31 14:14:54 +00:00
|
|
|
from flask_restless import APIManager, ProcessingException
|
2017-08-31 16:37:58 +00:00
|
|
|
from models import (db, User, Role, Gradeclass, Student, AttendanceUpdate,
|
|
|
|
|
Presence)
|
2017-08-31 14:14:54 +00:00
|
|
|
from sqlalchemy import func
|
2017-08-31 08:44:47 +00:00
|
|
|
import json
|
2017-08-31 14:14:54 +00:00
|
|
|
from datetime import datetime, date
|
|
|
|
|
from dateutil import parser
|
2017-08-31 08:44:47 +00:00
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
app.config['DEBUG'] = True
|
2017-08-31 14:14:54 +00:00
|
|
|
app.config['PORT'] = 5001
|
2017-08-31 08:44:47 +00:00
|
|
|
app.config['SECRET_KEY'] = 'super-secret'
|
|
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/db.sqlite'
|
|
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
|
app.config['DEFAULT_MAIL_SENDER'] = 'info@example.com'
|
|
|
|
|
app.config['SECURITY_PASSWORD_SALT'] = 'uaisfyasiduyaisiuf'
|
|
|
|
|
|
2017-08-31 14:14:54 +00:00
|
|
|
db.init_app(app)
|
2017-08-31 08:44:47 +00:00
|
|
|
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
|
|
|
|
security = Security(app, user_datastore)
|
|
|
|
|
|
2017-08-31 14:14:54 +00:00
|
|
|
|
2017-08-31 08:44:47 +00:00
|
|
|
def auth_func(*args, **kwargs):
|
|
|
|
|
# if not current_user.is_authenticated:
|
|
|
|
|
# raise ProcessingException(description='Not authenticated', code=401)
|
|
|
|
|
return True
|
|
|
|
|
|
2017-08-31 14:14:54 +00:00
|
|
|
|
|
|
|
|
verify_logged_id = dict(GET_SINGLE=[auth_func], GET_MANY=[auth_func])
|
2017-08-31 08:44:47 +00:00
|
|
|
|
|
|
|
|
manager = APIManager(app, flask_sqlalchemy_db=db)
|
2017-08-31 16:37:58 +00:00
|
|
|
manager.create_api(
|
|
|
|
|
Student,
|
|
|
|
|
methods=['GET', 'PUT'],
|
|
|
|
|
results_per_page=5,
|
|
|
|
|
preprocessors=verify_logged_id)
|
|
|
|
|
manager.create_api(
|
|
|
|
|
Gradeclass,
|
|
|
|
|
methods=['GET', 'PUT'],
|
|
|
|
|
results_per_page=5,
|
|
|
|
|
preprocessors=verify_logged_id)
|
2017-08-31 14:14:54 +00:00
|
|
|
|
2017-08-31 08:44:47 +00:00
|
|
|
|
|
|
|
|
@app.before_first_request
|
2017-08-31 14:14:54 +00:00
|
|
|
def create_test_data():
|
2017-08-31 08:44:47 +00:00
|
|
|
try:
|
|
|
|
|
db.drop_all()
|
|
|
|
|
db.create_all()
|
2017-08-31 16:37:58 +00:00
|
|
|
for i in range(3):
|
2017-08-31 14:14:54 +00:00
|
|
|
g_cls = Gradeclass("Class {}".format(i))
|
|
|
|
|
db.session.add(g_cls)
|
2017-08-31 16:37:58 +00:00
|
|
|
for s in range(5):
|
|
|
|
|
stu = Student("TestStudent#{}-Class{}".format(s, i), g_cls.id)
|
2017-08-31 14:14:54 +00:00
|
|
|
db.session.add(stu)
|
|
|
|
|
user_datastore.create_user(
|
|
|
|
|
email='user@example.com', password='password')
|
2017-08-31 08:44:47 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2017-08-31 14:14:54 +00:00
|
|
|
def parse_time(time_str):
|
|
|
|
|
utc_now = (time_str and time_str == 'now') or not time_str
|
|
|
|
|
time = datetime.utcnow() if utc_now else None
|
|
|
|
|
try:
|
|
|
|
|
time = parser.parse(attend_date)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
return time
|
|
|
|
|
|
2017-08-31 16:37:58 +00:00
|
|
|
|
2017-08-31 14:14:54 +00:00
|
|
|
def upsert_attendance(update_type, object_id, attend_time, pres_enum):
|
|
|
|
|
existing = AttendanceUpdate.query.filter(
|
2017-08-31 16:37:58 +00:00
|
|
|
AttendanceUpdate.update_type == update_type).filter(
|
|
|
|
|
AttendanceUpdate.value_identifier == object_id).filter(
|
|
|
|
|
func.date(
|
|
|
|
|
AttendanceUpdate.time) == attend_time.date()).first()
|
2017-08-31 14:14:54 +00:00
|
|
|
if existing:
|
|
|
|
|
existing.presence = pres_enum
|
|
|
|
|
else:
|
2017-08-31 16:37:58 +00:00
|
|
|
new_entry = AttendanceUpdate(update_type, object_id, attend_time,
|
|
|
|
|
pres_enum)
|
2017-08-31 14:14:54 +00:00
|
|
|
db.session.add(new_entry)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
2017-08-31 16:37:58 +00:00
|
|
|
|
2017-08-31 14:14:54 +00:00
|
|
|
def validate_attend(update_type, object_id):
|
2017-08-31 16:37:58 +00:00
|
|
|
# return True
|
2017-08-31 14:14:54 +00:00
|
|
|
if update_type in ['class', 'student'] and object_id:
|
2017-08-31 16:37:58 +00:00
|
|
|
g_cls_found = update_type == 'class' and Gradeclass.query.get(
|
|
|
|
|
object_id)
|
|
|
|
|
stud_found = update_type == 'student' and Student.query.get(object_id)
|
2017-08-31 14:14:54 +00:00
|
|
|
return g_cls_found or stud_found
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2017-08-31 16:37:58 +00:00
|
|
|
json_cont = {'Content-Type': 'application/json'}
|
|
|
|
|
resp = lambda val,code: (json.dumps({'status': val}), code, json_cont)
|
2017-08-31 14:14:54 +00:00
|
|
|
presense_keys = [str(i).replace('Presence.', '').upper() for i in Presence]
|
2017-08-31 08:44:47 +00:00
|
|
|
|
2017-08-31 14:14:54 +00:00
|
|
|
|
2017-08-31 16:37:58 +00:00
|
|
|
def get_attendance(update_type, request):
|
|
|
|
|
object_id = request.args.get('identifier', None)
|
|
|
|
|
start_timestr = request.args.get('start_time', None)
|
|
|
|
|
end_timestr = request.args.get('end_time', None)
|
|
|
|
|
start_time, end_time = parse_time(start_timestr), parse_time(end_timestr)
|
|
|
|
|
ret_data = resp('invalid',400)
|
|
|
|
|
if validate_attend(update_type, object_id) and start_time and end_time:
|
|
|
|
|
ret_data = resp('present',200)
|
|
|
|
|
return ret_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_attendance(update_type, request):
|
2017-08-31 14:14:54 +00:00
|
|
|
object_id = request.form.get('identifier', None)
|
|
|
|
|
presence = request.form.get('presence', None)
|
|
|
|
|
attend_str = request.form.get('time', None)
|
|
|
|
|
attend_time = parse_time(attend_str)
|
2017-08-31 16:37:58 +00:00
|
|
|
ret_data = resp('invalid',400)
|
|
|
|
|
if validate_attend(
|
|
|
|
|
update_type, object_id
|
|
|
|
|
) and presence and attend_time and presence.upper() in presense_keys:
|
2017-08-31 14:14:54 +00:00
|
|
|
pres_enum = Presence[presence.upper()]
|
|
|
|
|
upsert_attendance(update_type, object_id, attend_time, pres_enum)
|
2017-08-31 16:37:58 +00:00
|
|
|
ret_data = resp('updated',200)
|
|
|
|
|
return ret_data
|
|
|
|
|
|
2017-08-31 14:14:54 +00:00
|
|
|
|
2017-08-31 16:37:58 +00:00
|
|
|
@app.route('/api/attendance/<update_type>', methods=['GET', 'POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def attendance(update_type):
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
return get_attendance(update_type, request)
|
|
|
|
|
elif request.method == 'POST':
|
|
|
|
|
return update_attendance(update_type, request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/login', methods=['POST'])
|
2017-08-31 14:14:54 +00:00
|
|
|
def login():
|
|
|
|
|
username = request.form.get('username', None)
|
|
|
|
|
password = request.form.get('password', None)
|
2017-08-31 16:37:58 +00:00
|
|
|
ret_data = (json.dumps({'status': 'failed'}), 401, json_cont)
|
2017-08-31 14:14:54 +00:00
|
|
|
if username and password:
|
|
|
|
|
user = User.query.filter(User.email == username).first()
|
|
|
|
|
if not user:
|
2017-08-31 16:37:58 +00:00
|
|
|
ret_data = resp('notfound',401)
|
|
|
|
|
elif verify_and_update_password(password, user):
|
2017-08-31 14:14:54 +00:00
|
|
|
login_user(user)
|
2017-08-31 16:37:58 +00:00
|
|
|
ret_data = resp('success',200)
|
2017-08-31 14:14:54 +00:00
|
|
|
return ret_data
|
2017-08-31 08:44:47 +00:00
|
|
|
|
2017-08-31 16:37:58 +00:00
|
|
|
|
2017-08-31 08:44:47 +00:00
|
|
|
@app.route('/')
|
|
|
|
|
def home():
|
|
|
|
|
return 'hello'
|
|
|
|
|
|
2017-08-31 14:14:54 +00:00
|
|
|
|
2017-08-31 08:44:47 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app.run()
|