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")
def unbook_workday(holiday_id): try: if request.method == 'DELETE': engine.query(PublicHoliday).filter(PublicHoliday.id == holiday_id).delete() return json.dumps({"status": "ok", "message": "saved"}) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def delete_switchday(switch_id): try: if request.method == 'DELETE': engine.query(Switchday).filter(Switchday.id == switch_id).delete() return json.dumps({"status": "ok", "message": "saved"}) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def noshowup(group_id, chosen_date): try: gid = group_id dt = chosen_date if request.method == 'POST': d = request.get_json() userId = d['userId'] isWorkday = d['isWorkday'] #field name has_not_worked is wrongly named. it should be has worked instead. if isWorkday: r = engine.query(Workday).filter( Workday.group_id == gid, Workday.standin_user_id == userId, Workday.work_date == dt).all() if r: newR = r[0] newR.has_not_worked = True engine.save(newR) else: r = engine.query(StandinDay).filter( StandinDay.group_id == gid, StandinDay.standin_user_id == userId, StandinDay.standin_date == dt).all() if r: newR = r[0] newR.has_not_worked = True engine.save(newR) return json.dumps({"status": "ok", "message": "saved"}) elif request.method == 'GET': w = engine.query(Workday).filter(Workday.group_id == gid, Workday.work_date == dt).all() s = engine.query(StandinDay).filter( StandinDay.group_id == gid, StandinDay.standin_date == dt).all() w_dumps = json.dumps(w, cls=AlchemyEncoder) s_dumps = json.dumps(s, cls=AlchemyEncoder) result = { 'standin': json.loads(s_dumps), 'workday': json.loads(w_dumps) } return json.dumps(result) # return render_template('workday/{0}.html'.format('show-ups'), workday_owners=[], standin_owners=[]) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def open_switchday(group_id, show_workday): try: if request.method == 'GET': r = [] if int(show_workday) == 1: r = engine.query(Switchday).filter(Switchday.group_id==group_id, Switchday.is_work_day==True).all() else: r = engine.query(Switchday).filter(Switchday.group_id==group_id, Switchday.is_work_day==False).all() newS = sorted(r, key=itemgetter('switch_date')) return json.dumps(newS, cls=AlchemyEncoder) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def working_day(group_id): try: if request.method == 'POST': d = request.get_json() if not d['standin_user_id']: standin_user_id = None else: standin_user_id = d['standin_user_id'] w = Workday(d['created_by_id'], group_id, d['work_date'], d['from_time'], d['to_time'], standin_user_id, d['work_date'], d['is_half_day']) id = w.id engine.save(w) return json.dumps({ "status": "ok", "message": "workday was created", "id": id }) elif request.method == 'GET': r = engine.query(Workday).filter( Workday.group_id == group_id).all() newS = sorted(r, key=itemgetter('work_date')) return json.dumps(newS, cls=AlchemyEncoder) # return render_template('workday/{0}.html'.format('work-day')) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def term_details(group_id): try: r = engine.query(Term).filter(Term.group_id == group_id).all() return json.dumps(r, cls=AlchemyEncoder) #return json.dumps([{'name': 'vt2016', 'start-date': '2016-09-01', 'end-date': '2016-12-31', 'id': '1'}]) except Exception as e: logging.exception(e) return render_template("oops.html")
def delete(group_id, standin_user_id): try: # todo do not let user deselect a chosen date X days from that date gid = group_id if request.method == 'DELETE': d = request.get_json() dt = d['chosen_date'] engine.query(Switchday).filter(Switchday.group_id==gid, Switchday.switch_date==dt, Switchday.standin_user_id==standin_user_id).delete() return json.dumps({"status": "ok", "message": "saved"}) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def nonopenstandin(group_id): try: s = engine.query(StandinDay).filter( StandinDay.group_id == group_id, StandinDay.standin_user_id != None).all() newS = sorted(s, key=itemgetter('standin_date')) return json.dumps(newS, cls=AlchemyEncoder) except Exception as e: logging.exception(e) return render_template("oops.html")
def myworkday(group_id, user_id): try: w = engine.query(Workday).filter( Workday.group_id == group_id, Workday.standin_user_id == user_id).all() newS = sorted(w, key=itemgetter('work_date')) return json.dumps(newS, cls=AlchemyEncoder) except Exception as e: logging.exception(e) return render_template("oops.html")
def getUser(user_id): try: if request.method == 'GET': res = engine.query(User).filter(User.id == user_id)\ .all(attributes=['id', 'name', 'given_name', 'family_name', 'image_url', 'role', 'is_active']) return json.dumps(res[0], cls=AlchemyEncoder) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def children(term_id): try: if request.method == 'POST': d = request.get_json() tid = term_id child_count = int(d['child_count']) r = Children(tid, child_count) engine.query(Children).filter(Children.term_id == tid).delete() engine.save(r) return json.dumps({"status": "ok", "message": "saved"}) elif request.method == 'GET': r = engine.query(Children).filter( Children.term_id == term_id).all() return json.dumps(r, cls=AlchemyEncoder) #return render_template('term/{0}.html'.format('children')) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def rule(group_id, term_id): try: if request.method == 'POST': d = request.get_json() gid = group_id rule_definition = d['definition'] r = Rule(gid, term_id, json.dumps(rule_definition)) engine.query(Rule).filter(Rule.group_id == gid, Rule.term_id == term_id).delete() engine.save(r) return json.dumps({"status": "ok", "message": "saved"}) elif request.method == 'GET': r = engine.query(Rule).filter(Rule.group_id == group_id, Rule.term_id == term_id).all() if not r: return json.dumps({}) return json.dumps(r[0], cls=AlchemyEncoder) #return render_template('rule/{0}.html'.format('rule')) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def member(group_id): try: if request.method == 'POST': d = request.get_json() add_users = d['new_user_ids'] for u in add_users: x = Member(group_id, u) engine.save(x) removed_ids = d['removed_member_ids'] for rid in removed_ids: engine.query(Member).filter(Member.id == rid).delete() return json.dumps({"status": "ok", "message": "saved"}) elif request.method == 'GET': m = engine.query(Member).filter(Member.group_id == group_id).all() return json.dumps(m, cls=AlchemyEncoder) #return render_template('member/{0}.html'.format('member')) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def standin_day(): try: if request.method == 'POST': d = request.get_json() if not d['standin_user_id']: standin_user_id = None else: standin_user_id = d['standin_user_id'] gid = d['group_id'] standin_date = datetime.datetime.fromtimestamp(d['standin_date']) # create if not exists existStandin = engine.query(StandinDay).filter( StandinDay.group_id == gid, StandinDay.standin_date == calendar.timegm( standin_date.timetuple())).all() if not existStandin: #create w = StandinDay(gid, calendar.timegm(standin_date.timetuple()), standin_user_id, d['booking_date']) engine.save(w) return json.dumps({"status": "ok", "message": "saved"}) else: return json.dumps({"status": "error", "message": "duplicate"}) elif request.method == 'GET': vacant_dates = engine.query(StandinDay).filter( StandinDay.standin_user_id == None).all() return json.dumps(vacant_dates) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def manage(group_id, user_id): try: # todo do not let user deselect a chosen date X days from that date gid = group_id if request.method == 'POST': # only used to offer own date for switching d = request.get_json() chosen_date = d['chosen_date'] is_workday = d['is_workday'] dt = chosen_date id = None # todo: handle inside a db transaction w = engine.query(Switchday).filter(Switchday.group_id==gid, Switchday.switch_date==dt).all() if not w: w = Switchday(group_id, d['chosen_date'], d['from_time'], d['to_time'], user_id, d['is_half_day'], is_workday) id = w.id engine.save(w) notify.notify_switch(user_id, d['chosen_date'], is_workday) notify.notify_switch_broadcast(user_id, d['chosen_date'], is_workday) else: return abort(409) return json.dumps({"status": "ok", "message": "saved", "id": id}) elif request.method == 'GET': r = engine.query(Switchday).filter(Switchday.group_id==group_id, Switchday.standin_user_id==user_id).all() newS = sorted(r, key=itemgetter('switch_date')) return json.dumps(newS, cls=AlchemyEncoder) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
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")
def getStandinVikarieForNextXDays(group_id, start_after_x_days=2, next_x_days=5): '''with default parameters, if job is executed on friday, it should return weekdays''' # todo : make a utility to return next week start and end dates date_start = datetime.datetime.now().date() + datetime.timedelta( days=start_after_x_days + 1) date_end = date_start + datetime.timedelta(days=next_x_days + 1) epoch_start = calendar.timegm(date_start.timetuple()) epoch_end = calendar.timegm(date_end.timetuple()) s = engine.query(StandinDay).filter( StandinDay.group_id == group_id, StandinDay.standin_user_id != None, StandinDay.standin_date >= epoch_start).all() newS = sorted(s, key=itemgetter('standin_date')) return [i for i in newS if i.standin_date < epoch_end]
def summon(group_id): try: if request.method == 'POST': d = request.get_json() w = Summon(d['created_by_id'], group_id, d['work_date'], d['from_time'], d['to_time']) id = w.id engine.save(w) notify.notify_summon(group_id, d['work_date']) return json.dumps({"status": "ok", "message": "saved", "id": id}) elif request.method == 'GET': r = engine.query(Summon).filter(Summon.group_id == group_id).all() newS = sorted(r, key=itemgetter('work_date')) return json.dumps(newS, cls=AlchemyEncoder) # return render_template('workday/{0}.html'.format('summon')) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def holiday_day(group_id): try: if request.method == 'POST': d = request.get_json() w = PublicHoliday(d['created_by_id'], group_id, d['holiday_date'], False) engine.save(w) return json.dumps({"status": "ok", "message": "holiday was created"}) elif request.method == 'GET': r = engine.query(PublicHoliday).filter(PublicHoliday.group_id == group_id).all(attributes=['holiday_date', 'id']) newS = sorted(r, key=itemgetter('holiday_date')) return json.dumps(newS, cls=AlchemyEncoder) # return render_template('workday/{0}.html'.format('work-day')) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def standin_range(): '''create entries of standin days without any standin user''' try: if request.method == 'POST': d = request.get_json() gid = d['group_id'] standin_user_id = None startDate = datetime.datetime.fromtimestamp(d['start_date']) endDate = datetime.datetime.fromtimestamp(d['end_date']) items_to_save = [] existingStandins = engine.query(StandinDay).filter( StandinDay.group_id == gid, StandinDay.standin_date >= calendar.timegm(startDate.timetuple()), StandinDay.standin_date <= calendar.timegm(endDate.timetuple())).all() dictExistingStandinDates = { x.standin_date: 1 for x in existingStandins } while startDate <= endDate: if not dictExistingStandinDates.get( calendar.timegm(startDate.timetuple())): w = StandinDay(gid, calendar.timegm(startDate.timetuple()), standin_user_id, int(time.time())) items_to_save.append(w) startDate = startDate + datetime.timedelta(days=1) if items_to_save: engine.save(items_to_save) return json.dumps({"status": "ok", "message": "saved"}) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def getOpenStandin(group_id): s = engine.query(StandinDay).filter( StandinDay.group_id == group_id, StandinDay.standin_user_id == None).all() newS = sorted(s, key=itemgetter('standin_date')) return newS
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")
def getGroupAdmins(group_id): return engine.query(User).filter(User.role == E_ROLE.GroupAdmin).all()
def getGroupByDomain(domain): return engine.query(Group).filter(Group.domain == domain).all()
def getOpenWorkday(group_id): w = engine.query(Workday).filter(Workday.group_id == group_id, Workday.standin_user_id == None).all() newS = sorted(w, key=itemgetter('work_date')) return newS
def getEmail(userId): res = engine.query(User).filter(User.id == userId) \ .all(attributes=['email']) return res[0]['email']
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")
def getVikarieUserId(group_id, dateOfSummon): res = engine.query(StandinDay).filter(StandinDay.group_id == group_id, StandinDay.standin_date == dateOfSummon) \ .all(attributes=['standin_user_id']) return res[0]['standin_user_id']