def save_niceties(): niceties_to_save = json.loads(request.form.get("niceties", "[]")) for n in niceties_to_save: nicety = ( Nicety .query # Query is always about getting Nicety objects from the database .filter_by( end_date=datetime.strptime(n.get("end_date"), "%Y-%m-%d").date(), target_id=n.get("target_id"), author_id=current_user().id) .one_or_none()) if nicety is None: nicety = Nicety( end_date=datetime.strptime(n.get("end_date"), "%Y-%m-%d").date(), target_id=n.get("target_id"), author_id=current_user().id) db.session.add(nicety) # We need to add for the new one, but we don't need to add where we have used .query # Now any change to the nicety object (variable) is tracked by the object # so it knows what it will have to update. # And then when we call db.session.commit() that knows about the object # (beacuse Nicety.query... uses the db.session behind the scenes). # So then db.session.commit() sends the update (or insert) for every object # in the session. This includes every object created by a [model].query and # everything added to the session with db.session.add(). nicety.anonymous = n.get("anonymous", current_user().anonymous_by_default) text = util.encode_str(n.get("text").strip()) if '' == text: text = None nicety.text = text nicety.faculty_reviewed = False nicety.no_read = n.get("no_read") nicety.date_updated = n.get("date_updated") db.session.commit() return jsonify({'status': 'OK'})
def display_people(): current = get_current_users() if current == []: return jsonify({'status': 'closed'}) people = partition_current_users(current) user_id = current_user().id current_user_leaving = False leaving = [] to_display = None for person in people['leaving']: if person['id'] == user_id: current_user_leaving = True else: leaving.append(person) staying = list(person for person in people['staying']) faculty = get_current_faculty() # there needs to be a better way to add special people to the current exiting batch special = [x for x in faculty if x['id'] == 601] random.seed(current_user().random_seed) random.shuffle(staying) random.shuffle(leaving) random.shuffle(special) if current_user_leaving == True: to_display = { 'staying': staying, 'leaving': leaving, 'special': special } else: to_display = {'leaving': leaving, 'special': special} return jsonify(to_display)
def display_people(): current = get_current_users() if current == []: return jsonify({'status': 'closed'}) people = partition_current_users(current) user_id = current_user().id current_user_leaving = False leaving = [] to_display = None for person in people['leaving']: if person['id'] == user_id: current_user_leaving = True else: leaving.append(person) staying = list(person for person in people['staying']) faculty = get_current_faculty() # there needs to be a better way to add special people to the current exiting batch special = [ x for x in faculty if x['id'] == 601] random.seed(current_user().random_seed) random.shuffle(staying) random.shuffle(leaving) random.shuffle(special) if current_user_leaving == True: to_display = { 'staying': staying, 'leaving': leaving, 'special': special } else: to_display = { 'leaving': leaving, 'special': special } return jsonify(to_display)
def niceties_for_me(): ret = [] whoami = current_user().id two_weeks_from_now = datetime.now() - timedelta(days=14) valid_niceties = (Nicety.query .filter(Nicety.end_date + timedelta(days=1) < datetime.now()) # show niceties one day after the end date .filter(Nicety.target_id == whoami) .all()) for n in valid_niceties: if n.text != None: if n.anonymous == True: store = { 'end_date': n.end_date, 'anonymous': n.anonymous, 'text': util.decode_str(n.text), 'no_read': n.no_read, 'date_updated': n.date_updated } else: store = { 'avatar_url': cache_person_call(n.author_id)['avatar_url'], 'name': cache_person_call(n.author_id)['name'], 'author_id': n.author_id, 'end_date': n.end_date, 'anonymous': n.anonymous, 'text': util.decode_str(n.text), 'no_read': n.no_read, 'date_updated': n.date_updated } ret.append(store) return jsonify(ret)
def get(self): user = current_user() if user is None: return redirect(url_for('authorized')) if not user.faculty: return abort(403) return jsonify({c.key: config.to_frontend_value(c) for c in SiteConfiguration.query.all()})
def post(self): if current_user() is None: redirect(url_for('authorized')) user = current_user() if not user.faculty: return abort(403) key = request.form.get('key', None) value = request.form.get('value', None) try: try: config.set(key, config.from_frontend_value(key, json.loads(value))) return jsonify({'status': 'OK'}) except ValueError: return abort(404) except: return abort(400)
def get_self_info(): #self_info = rc.get('people/me').data admin = util.admin_access(current_user()) data = { 'admin': admin } return jsonify(data)
def niceties_for_me(): ret = [] whoami = current_user().id valid_niceties = (Nicety.query .filter(Nicety.end_date + timedelta(days=1) < datetime.now()) # show niceties one day after the end date .filter(Nicety.target_id == whoami) .all()) for n in valid_niceties: if n.text is not None: if n.anonymous is True: store = { 'end_date': n.end_date, 'anonymous': n.anonymous, 'text': util.decode_str(n.text), 'no_read': n.no_read, 'date_updated': n.date_updated } else: store = { 'avatar_url': cache_person_call(n.author_id)['avatar_url'], 'name': cache_person_call(n.author_id)['name'], 'author_id': n.author_id, 'end_date': n.end_date, 'anonymous': n.anonymous, 'text': util.decode_str(n.text), 'no_read': n.no_read, 'date_updated': n.date_updated } ret.append(store) return jsonify(ret)
def niceties_from_me(): user_id = current_user().id niceties = (Nicety.query.filter(Nicety.author_id == user_id).all()) ret = [{ 'target_id': n.target_id, 'text': util.decode_str(n.text), 'anonymous': n.anonymous, 'no_read': n.no_read, 'date_updated': n.date_updated } for n in niceties] return jsonify(ret)
def get_niceties_to_edit(): is_rachel = util.admin_access(current_user()) nicety_text = util.encode_str(request.form.get("text")) nicety_author = request.form.get("author_id") nicety_target = request.form.get("target_id") if is_rachel == True: (Nicety.query.filter(Nicety.author_id == nicety_author).filter( Nicety.target_id == nicety_target).update({'text': nicety_text})) db.session.commit() return jsonify({'status': 'OK'}) else: return jsonify({'authorized': "false"})
def print_niceties(): ret = {} # Mapping from target_id to a list of niceties for that person is_admin = admin_access(current_user()) three_weeks_ago = datetime.now() - timedelta(days=21) three_weeks_from_now = datetime.now() + timedelta(days=21) if is_admin is True: valid_niceties = (Nicety.query.filter( Nicety.end_date > three_weeks_ago).filter( Nicety.end_date < three_weeks_from_now).order_by( Nicety.target_id).all()) last_target = None for n in valid_niceties: target = cache_person_call(n.target_id)['full_name'] if target != last_target: # ... set up the test for the next one last_target = target ret[target] = [] # initialize the dictionary if n.text is not None and n.text.isspace() is False: if n.anonymous is False: ret[target].append({ 'author_id': n.author_id, 'anon': False, 'name': cache_person_call(n.author_id)['full_name'], 'text': decode_str(n.text), }) else: ret[target].append({ 'anon': True, 'name': "An Unknown Admirer", 'text': decode_str(n.text), }) ret = OrderedDict(sorted(ret.items(), key=lambda t: t[0])) names = ret.keys() data = [] for k, v in ret.items(): # sort by name, then sort by reverse author_id data.append({ 'to': k, 'niceties': # sorted(v, key=lambda k: k['name']) sorted(sorted(v, key=lambda k: k['name']), key=lambda k: k['anon']) }) return render_template('printniceties.html', data={ 'names': names, 'niceties': data }) else: return jsonify({'authorized': "false"})
def niceties_from_me(): user_id = current_user().id niceties = (Nicety.query .filter(Nicety.author_id == user_id) .all()) ret = [{ 'target_id': n.target_id, 'text': util.decode_str(n.text), 'anonymous': n.anonymous, 'no_read': n.no_read, 'date_updated': n.date_updated } for n in niceties] return jsonify(ret)
def get_niceties_to_edit(): is_rachel = util.admin_access(current_user()) nicety_text = util.encode_str(request.form.get("text")) nicety_author = request.form.get("author_id") nicety_target = request.form.get("target_id") if is_rachel == True: (Nicety.query .filter(Nicety.author_id==nicety_author) .filter(Nicety.target_id==nicety_target) .update({'text': nicety_text})) db.session.commit() return jsonify({'status': 'OK'}) else: return jsonify({'authorized': "false"})
def niceties_by_sender(): ret = {} # Mapping from target_id to a list of niceties for that person is_rachel = admin_access(current_user()) two_weeks_from_now = datetime.now() + timedelta(days=14) if is_rachel == True: valid_niceties = (Nicety.query.order_by(Nicety.author_id).all()) last_author = None for n in valid_niceties: author = cache_person_call(n.author_id)['full_name'] if author != last_author: # ... set up the test for the next one last_author = author ret[author] = [] # initialize the dictionary if n.text is not None and n.text.isspace() == False: if n.anonymous == False: ret[author].append({ 'target_id': n.target_id, 'anon': False, 'name': cache_person_call(n.target_id)['full_name'], 'text': decode_str(n.text), }) else: ret[author].append({ 'anon': True, 'name': "An Unknown Admirer", 'text': decode_str(n.text), }) ret = OrderedDict(sorted(ret.items(), key=lambda t: t[0])) names = ret.keys() data = [] for k, v in ret.items(): # sort by name, then sort by reverse author_id data.append({ 'to': k, 'niceties': sorted(sorted(v, key=lambda k: k['name']), key=lambda k: k['anon']) }) return render_template('nicetiesbyusers.html', data={ 'names': names, 'niceties': data }) else: return jsonify({'authorized': "false"})
def print_niceties(): ret = {} # Mapping from target_id to a list of niceties for that person is_rachel = admin_access(current_user()) three_weeks_ago = datetime.now() - timedelta(days=21) three_weeks_from_now = datetime.now() + timedelta(days=21) if is_rachel == True: valid_niceties = (Nicety.query .filter(Nicety.end_date > three_weeks_ago) .filter(Nicety.end_date < three_weeks_from_now) .order_by(Nicety.target_id) .all()) last_target = None for n in valid_niceties: target = cache_person_call(n.target_id)['full_name'] if target != last_target: # ... set up the test for the next one last_target = target ret[target] = [] # initialize the dictionary if n.text is not None and n.text.isspace() == False: if n.anonymous == False: ret[target].append({ 'author_id': n.author_id, 'anon': False, 'name': cache_person_call(n.author_id)['full_name'], 'text': decode_str(n.text), }) else: ret[target].append({ 'anon': True, 'name': "An Unknown Admirer", 'text': decode_str(n.text), }) ret = OrderedDict(sorted(ret.items(), key=lambda t: t[0])) names = ret.keys() data = [] for k, v in ret.items(): # sort by name, then sort by reverse author_id data.append({ 'to': k, 'niceties': #sorted(v, key=lambda k: k['name']) sorted(sorted(v, key=lambda k: k['name']), key=lambda k: k['anon']) }) return render_template('printniceties.html', data={ 'names': names, 'niceties': data }) else: return jsonify({'authorized': "false"})
def post_edited_niceties(): ret = {} # Mapping from target_id to a list of niceties for that person last_target = None is_rachel = util.admin_access(current_user()) three_weeks_ago = datetime.now() - timedelta(days=21) three_weeks_from_now = datetime.now() + timedelta(days=21) if is_rachel == True: valid_niceties = (Nicety.query .filter(Nicety.end_date > three_weeks_ago) .filter(Nicety.end_date < three_weeks_from_now) .order_by(Nicety.target_id) .all()) for n in valid_niceties: if n.target_id != last_target: # ... set up the test for the next one last_target = n.target_id ret[n.target_id] = [] # initialize the dictionary if n.anonymous == False: ret[n.target_id].append({ 'author_id': n.author_id, 'name': cache_person_call(n.author_id)['full_name'], 'no_read': n.no_read, 'text': util.decode_str(n.text), }) else: ret[n.target_id].append({ 'text': util.decode_str(n.text), 'no_read': n.no_read, }) return jsonify([ { 'to_name': cache_person_call(k)['full_name'], 'to_id': cache_person_call(k)['id'], 'niceties': v } for k, v in ret.items() ]) else: return jsonify({'authorized': "false"})
def post_edited_niceties(): ret = {} # Mapping from target_id to a list of niceties for that person last_target = None is_admin = util.admin_access(current_user()) three_weeks_ago = datetime.now() - timedelta(days=21) three_weeks_from_now = datetime.now() + timedelta(days=21) if is_admin is True: valid_niceties = (Nicety.query .filter(Nicety.end_date > three_weeks_ago) .filter(Nicety.end_date < three_weeks_from_now) .order_by(Nicety.target_id) .all()) for n in valid_niceties: if n.target_id != last_target: # ... set up the test for the next one last_target = n.target_id ret[n.target_id] = [] # initialize the dictionary if n.anonymous is False: ret[n.target_id].append({ 'author_id': n.author_id, 'name': cache_person_call(n.author_id)['full_name'], 'no_read': n.no_read, 'text': util.decode_str(n.text), }) else: ret[n.target_id].append({ 'text': util.decode_str(n.text), 'no_read': n.no_read, }) return jsonify([ { 'to_name': cache_person_call(k)['full_name'], 'to_id': cache_person_call(k)['id'], 'niceties': v } for k, v in ret.items() ]) else: return jsonify({'authorized': "false"})
def get_self_info(): #self_info = rc.get('people/me').data admin = util.admin_access(current_user()) data = {'admin': admin} return jsonify(data)
def get_self_info(): admin = util.admin_access(current_user()) data = { 'admin': admin } return jsonify(data)