Esempio n. 1
0
def register():
    try:
        if request.method == 'POST':
            d = request.get_json()
            res = engine.query(User).filter(User.email == d['email']).all()
            if not res:
                u = User(d['name'], d['givenName'], d['familyName'],
                         d['email'], None, d['tokenId'], d['imageUrl'])
                engine.save(u)
                userid = u.id
                role = u.role
                isActive = u.is_active
            else:
                newR = res[0]
                userid = newR.id
                role = newR.role
                isActive = newR.is_active
                newR.auth_token = d['tokenId']
                newR.image_url = d['imageUrl']
                engine.sync(newR)
            return json.dumps({
                'userId': userid,
                'role': role,
                'isActive': isActive
            })
        elif request.method == 'GET':
            d = request.get_json()
            res = engine.query(User).filter(User.id == d['userId']).all()
            return json.dumps(res, cls=AlchemyEncoder)
        else:
            return abort(404)
    except Exception as e:
        logging.exception(e)
        return render_template("oops.html")
Esempio n. 2
0
def unbook_workday(workday_id):
    try:
        if request.method == 'PUT':
            r = engine.query(Workday).filter(Workday.id == workday_id).all()
            if r:
                newR = r[0]
                newR.standin_user_id = None
                newR.booking_date = int(time.time())
                engine.sync(newR)

            return json.dumps({"status": "ok", "message": "saved"})
        else:
            return abort(404)
    except Exception as e:
        logging.exception(e)
        return render_template("oops.html")
Esempio n. 3
0
def onswitch_worksignup(group_id):
    try:
        # todo do not let user deselect a chosen date X days from that date
        d = request.get_json()
        gid = group_id

        if request.method == 'POST':
            user_id = d['user_id']
            chosen_date = d['chosen_date']
            is_workday = d['is_workday']
            standin_user_id = d['standinUserId']

            dt = chosen_date
            if is_workday:
                # todo: handle inside a db transaction

                w = engine.query(Workday).filter(
                    Workday.group_id == gid, Workday.work_date == dt).all()
                if w:
                    newW = w[0]
                    # important for concurrent updates
                    if newW.standin_user_id == standin_user_id:
                        newW.standin_user_id = user_id
                        newW.booking_date = int(time.time())
                        id = newW.id
                        engine.sync(newW)
                        notify.notify_switched(standin_user_id, dt, is_workday,
                                               user_id)
                    else:
                        return abort(409)
                else:
                    return json.dumps({
                        "status": "ok",
                        "message": "saved",
                        "id": id
                    })
            else:
                w = engine.query(StandinDay).filter(
                    StandinDay.group_id == gid,
                    StandinDay.standin_date == dt).all()
                if w:
                    newW = w[0]
                    # important for concurrent updates
                    if newW.standin_user_id == standin_user_id:
                        newW.standin_user_id = user_id
                        newW.booking_date = int(time.time())
                        id = newW.id
                        engine.sync(newW)
                        notify.notify_switched(standin_user_id, dt, is_workday,
                                               user_id)
                    else:
                        return abort(409)
                else:
                    return json.dumps({
                        "status": "ok",
                        "message": "saved",
                        "id": id
                    })

            return 'on switch - worksignup was saved'
        else:
            return abort(404)
    except Exception as e:
        logging.exception(e)
        return render_template("oops.html")
Esempio n. 4
0
def worksignup(group_id):
    try:
        # todo do not let user deselect a chosen date X days from that date
        d = request.get_json()
        gid = group_id

        if request.method == 'POST':
            user_id = d['user_id']
            chosen_date = d['chosen_date']
            is_workday = d['is_workday']

            id = None
            dt = chosen_date

            if is_workday:
                # todo: handle inside a db transaction

                w = engine.query(Workday).filter(
                    Workday.group_id == gid, Workday.work_date == dt).all()
                if w:
                    user_already_took_an_instance_flag = False
                    # since there can be multiple instances of workday on same date like planning day
                    for x in w:
                        #check if user already took one instance of this workday
                        if x.standin_user_id == user_id:
                            user_already_took_an_instance_flag = True

                    if not user_already_took_an_instance_flag:
                        for newW in w:
                            if not newW.standin_user_id:
                                newW.standin_user_id = user_id
                                newW.booking_date = int(time.time())
                                id = newW.id
                                engine.sync(newW)
                                notify.notify_booked(user_id, dt, is_workday)
                                user_already_took_an_instance_flag = True
                                break

                    if not user_already_took_an_instance_flag:
                        return abort(409)
                else:
                    return json.dumps({
                        "status": "ok",
                        "message": "workday doesnot exist"
                    })
            else:

                w = engine.query(StandinDay).filter(
                    StandinDay.group_id == gid,
                    StandinDay.standin_date == dt).all()
                if w:
                    newW = w[0]
                    if not newW.standin_user_id:
                        newW.standin_user_id = user_id
                        newW.booking_date = int(time.time())
                        id = newW.id
                        engine.sync(newW)
                        notify.notify_booked(user_id, dt, is_workday)
                    else:
                        return abort(409)
                else:
                    return json.dumps({
                        "status": "ok",
                        "message": "standin not found"
                    })

            return json.dumps({"status": "ok", "message": "saved", "id": id})
        else:
            return abort(404)
    except Exception as e:
        logging.exception(e)
        return render_template("oops.html")