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 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 send(self): c = Content(self.type, self.metric) subject = (c.getSubject()).format(*(self.metric)) body = (c.getContent()).format(*(self.metric)) r = self.send_email( subject, body) e = EmailNotify(self.email_to, self.type) engine.save(e)
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 group(): # create a group, group_type, group_owner try: if request.method == 'POST': d = request.get_json() g = Group(d['name'], int(d['type_id']), d['domain']) engine.save(g) return json.dumps({"status": "ok", "message": "saved"}) elif request.method == 'GET': r = engine.scan(Group).all() return json.dumps(r, cls=AlchemyEncoder) #return render_template('group/{0}.html'.format('group')) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def joingroup(invite_code): try: # todo: user joins a group if invite id matches. for time being we keep this simple if request.method == 'POST': d = request.get_json() group_id = d['group_id'] user_id = d['group_id'] x = Member(group_id, user_id) engine.save(x) return json.dumps({"status": "ok", "message": "saved"}) else: return abort(404) except Exception as e: logging.exception(e) return render_template("oops.html")
def invite(): # create a group, group_type, group_owner try: if request.method == 'POST': d = request.get_json() gid = d['group_id'] emails = d['emails'] for email in emails: # todo: add random token number/string inv = Invite(email, gid, '1') engine.save(inv) return json.dumps({"status": "ok", "message": "saved"}) elif request.method == 'GET': return render_template('member/{0}.html'.format('invite')) 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 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 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 term(): try: # todo when a new term is set/updated, add dates in vikariedays table if request.method == 'POST': d = request.get_json() gid = d['group_id'] name = d['term_name'] start_dt = d['start_date'] end_dt = d['end_date'] family_spread = json.dumps(d['family_spread']) r = Term(gid, name, start_dt, end_dt, family_spread) id = r.id engine.save(r) return json.dumps({"status": "ok", "message": "saved", "id": id}) elif request.method == 'GET': return render_template('term/{0}.html'.format('term')) 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 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 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 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 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")