def save_institution(): raw_data = request.form shortname = raw_data["shortname"] new = raw_data.get("new") == "yes" resp, institution = can_edit_institution(shortname, new) if resp is not None: return resp data = {} data["timezone"] = tz = raw_data.get("timezone", "UTC") tz = pytz.timezone(tz) for col in db.institutions.search_cols: if col in data: continue try: val = raw_data.get(col) if not val: data[col] = None else: data[col] = process_user_input(val, db.institutions.col_type[col], tz=tz) if col == "admin": userdata = db.users.lookup(val) if userdata is None: raise ValueError("%s must have account on this site" % val) if col == "homepage" and val and not val.startswith("http"): data[col] = "http://" + data[col] if col == "access" and val not in ["open", "users", "endorsed"]: raise ValueError("Invalid access type") except Exception as err: # TODO: this probably needs to be a redirect to change the URL? We want to save the data the user entered. flash_error("Error processing %s: %s" % (col, err)) institution = WebInstitution(shortname, data=raw_data) return render_template( "edit_institution.html", institution=institution, institution_types=institution_types, timezones=timezones, title="Edit institution error", section="Manage", subsection="editinst", ) new_version = WebInstitution(shortname, data=data) if new_version == institution: flash("No changes made to institution.") else: new_version.save() edittype = "created" if new else "edited" flash("Institution %s successfully!" % edittype) return redirect(url_for(".edit_institution", shortname=shortname), 301)
def show_institution(shortname): institution = db.institutions.lookup(shortname) if institution is None: return abort(404, "Institution not found") institution = WebInstitution(shortname, data=institution) section = "Manage" if current_user.is_creator else None query = {"institutions": {"$contains": shortname}} if not current_user.is_admin: query["display"] = True events = list( seminars_search( query, sort=["weekday", "start_time", "name"], organizer_dict=all_organizers(), ) ) seminars = [S for S in events if not S.is_conference] conferences = [S for S in events if S.is_conference] conferences.sort(key=lambda S: (S.start_date, S.name)) return render_template( "institution.html", seminars=seminars, conferences=conferences, title="View institution", institution=institution, section=section, subsection="viewinst", )
def show_institution(shortname): institution = db.institutions.lookup(shortname) if institution is None: return abort(404, "Institution not found") institution = WebInstitution(shortname, data=institution) section = "Manage" if current_user.is_creator else None query = {"institutions": {"$contains": shortname}} if not current_user.is_admin: query["display"] = True # Find other institutions that are joint with seminars here others = list(set(sum(seminars_search(query, "institutions"), []))) idict = all_institutions({"shortname": {"$in": others}}) events = next_talk_sorted( list( seminars_search(query, organizer_dict=all_organizers(), institution_dict=idict))) seminars = [S for S in events if not S.is_conference] conferences = [S for S in events if S.is_conference] conferences.sort(key=lambda S: (S.start_date, S.name)) return render_template( "institution.html", seminars=seminars, conferences=conferences, title="View institution", institution=institution, section=section, subsection="viewinst", )
def save_institution(): raw_data = request.form shortname = raw_data["shortname"] new = (raw_data.get("new") == "yes") resp, institution = can_edit_institution(shortname, new) if resp is not None: return resp data = {} for col in db.institutions.search_cols: if col in data: continue try: val = raw_data.get(col) if not val: data[col] = None else: data[col] = process_user_input(val, db.institutions.col_type[col]) if col == 'admin': userdata = db.users.lookup(val) if userdata is None: raise ValueError("%s must have account on this site" % val) if not userdata['phd']: raise ValueError( "%s must have a PhD to administer an institution" % val) if col == 'homepage' and val and not val.startswith("http"): data[col] = "http://" + data[col] if col == "access" and val not in ["open", "users", "endorsed"]: raise ValueError("Invalid access type") except Exception as err: # TODO: this probably needs to be a redirect to change the URL? We want to save the data the user entered. flash_error("Error processing %s: %s" % (col, err)) institution = WebInstitution(shortname, data=raw_data) return render_template("edit_institution.html", institution=institution, institution_types=institution_types, timezones=timezones, title="Edit institution error", top_menu=basic_top_menu()) new_version = WebInstitution(shortname, data=data) new_version.save() edittype = "created" if new else "edited" flash("Institution successfully %s!" % edittype) return redirect(url_for("show_institution", shortname=shortname), 301)
def show_institution(shortname): institution = db.institutions.lookup(shortname) if institution is None: return render_template("404.html", title="Institution not found") institution = WebInstitution(shortname, data=institution) return render_template( "institution.html", title="View institution", institution=institution, top_menu=basic_top_menu(), )
def save_institution(): raw_data = request.form shortname = raw_data["shortname"] new = raw_data.get("new") == "yes" resp, institution = can_edit_institution(shortname, new) if resp is not None: return resp data = {} data["timezone"] = tz = raw_data.get("timezone", "UTC") tz = pytz.timezone(tz) errmsgs = [] for col in db.institutions.search_cols: if col in data: continue typ = db.institutions.col_type[col] try: val = raw_data.get(col, "") data[ col] = None # make sure col is present even if process_user_input fails data[col] = process_user_input(val, col, typ, tz) if col == "admin": userdata = userdb.lookup(data[col]) if userdata is None: if not data[col]: errmsgs.append( "You must specify the email address of the maintainer." ) continue else: errmsgs.append( format_errmsg( "User %s does not have an account on this site", data[col])) continue elif not userdata["creator"]: errmsgs.append( format_errmsg("User %s has not been endorsed", data[col])) continue if not userdata["homepage"]: if current_user.email == userdata["email"]: flash_warning( "Your email address will become public if you do not set your homepage in your user profile." ) else: flash_warning( "The email address %s of maintainer %s will be publicly visible.<br>%s", userdata["email"], userdata["name"], "The homepage on the maintainer's user account should be set prevent this.", ) except Exception as err: # should only be ValueError's but let's be cautious errmsgs.append(format_input_errmsg(err, val, col)) if not data["name"]: errmsgs.append("Institution name cannot be blank.") if not errmsgs and not data["homepage"]: errmsgs.append("Institution homepage cannot be blank.") # Don't try to create new_version using invalid input if errmsgs: return show_input_errors(errmsgs) new_version = WebInstitution(shortname, data=data) if new_version == institution: flash("No changes made to institution.") else: new_version.save() edittype = "created" if new else "edited" flash("Institution %s successfully!" % edittype) return redirect(url_for(".edit_institution", shortname=shortname), 302)
def save_seminar(): raw_data = request.form shortname = raw_data["shortname"] new = raw_data.get("new") == "yes" resp, seminar = can_edit_seminar(shortname, new) if resp is not None: return resp errmsgs = [] if seminar.new: data = { "shortname": shortname, "display": current_user.is_creator, "owner": current_user.email, } else: data = { "shortname": shortname, "display": seminar.display, "owner": seminar.owner, } # Have to get time zone first data["timezone"] = tz = raw_data.get("timezone") tz = pytz.timezone(tz) for col in db.seminars.search_cols: if col in data: continue typ = db.seminars.col_type[col] ### Hack to be removed ### if col.endswith("time") and typ == "timestamp with time zone": typ = "time" try: val = raw_data.get(col, "") data[ col] = None # make sure col is present even if process_user_input fails data[col] = process_user_input(val, col, typ, tz) except Exception as err: # should only be ValueError's but let's be cautious errmsgs.append(format_input_errmsg(err, val, col)) if not data["name"]: errmsgs.append("The name cannot be blank") if data["is_conference"] and data["start_date"] and data[ "end_date"] and data["end_date"] < data["start_date"]: errmsgs.append("End date cannot precede start date") if data["per_day"] is not None and data["per_day"] < 1: errmsgs.append( format_input_errmsg("integer must be positive", data["per_day"], "per_day")) if data["is_conference"] and (not data["start_date"] or not data["end_date"]): errmsgs.append( "Please specify the start and end dates of your conference (you can change these later if needed)." ) if data["is_conference"] and not data["per_day"]: flash_warning( "It will be easier to edit the conference schedule if you specify talks per day (an upper bound is fine)." ) data["institutions"] = clean_institutions(data.get("institutions")) data["topics"] = clean_topics(data.get("topics")) data["language"] = clean_language(data.get("language")) data["subjects"] = clean_subjects(data.get("subjects")) if not data["subjects"]: errmsgs.append(format_errmsg("Please select at least one subject.")) if not data["timezone"] and data["institutions"]: # Set time zone from institution data["timezone"] = WebInstitution(data["institutions"][0]).timezone data["weekdays"] = [] data["time_slots"] = [] for i in range(MAX_SLOTS): weekday = daytimes = None try: col = "weekday" + str(i) val = raw_data.get(col, "") weekday = process_user_input(val, col, "weekday_number", tz) col = "time_slot" + str(i) val = raw_data.get(col, "") daytimes = process_user_input(val, col, "daytimes", tz) except Exception as err: # should only be ValueError's but let's be cautious errmsgs.append(format_input_errmsg(err, val, col)) if weekday is not None and daytimes is not None: data["weekdays"].append(weekday) data["time_slots"].append(daytimes) if daytimes_early(daytimes): flash_warning( "Time slot %s includes early AM hours, please correct if this is not intended (use 24-hour time format).", daytimes, ) elif daytimes_long(daytimes): flash_warning( "Time slot %s is longer than 8 hours, please correct if this is not intended.", daytimes, ) if data["frequency"] and not data["weekdays"]: errmsgs.append( 'You must specify at least one time slot (or set periodicty to "no fixed schedule").' ) if len(data["weekdays"]) > 1: x = sorted( list(zip(data["weekdays"], data["time_slots"])), key=lambda t: t[0] * 24 * 60 + daytime_minutes(t[1].split("-")[0]), ) data["weekdays"], data["time_slots"] = [t[0] for t in x], [t[1] for t in x] organizer_data = [] contact_count = 0 for i in range(10): D = {"seminar_id": seminar.shortname} for col in db.seminar_organizers.search_cols: if col in D: continue name = "org_%s%s" % (col, i) typ = db.seminar_organizers.col_type[col] try: val = raw_data.get(name, "") D[col] = None # make sure col is present even if process_user_input fails D[col] = process_user_input(val, col, typ, tz) except Exception as err: # should only be ValueError's but let's be cautious errmsgs.append(format_input_errmsg(err, val, col)) if D["homepage"] or D["email"] or D["full_name"]: if not D["full_name"]: errmsgs.append( format_errmsg("Organizer name cannot be left blank")) D["order"] = len(organizer_data) # WARNING the header on the template says organizer # but it sets the database column curator, so the # boolean needs to be inverted D["curator"] = not D["curator"] if not errmsgs and D["display"] and D[ "email"] and not D["homepage"]: flash_warning( "The email address %s of organizer %s will be publicly visible.<br>%s", D["email"], D["full_name"], "Set homepage or disable display to prevent this.", ), if D["email"]: r = db.users.lookup(D["email"]) if r and r["email_confirmed"]: if D["full_name"] != r["name"]: errmsgs.append( format_errmsg( "Organizer name %s does not match the name %s of the account with email address %s", D["full_name"], r["name"], D["email"], )) else: if D["homepage"] and r[ "homepage"] and D["homepage"] != r["homepage"]: flash_warning( "The homepage %s does not match the homepage %s of the account with email address %s, please correct if unintended.", D["homepage"], r["homepage"], D["email"], ) if D["display"]: contact_count += 1 organizer_data.append(D) if contact_count == 0: errmsgs.append( format_errmsg( "There must be at least one displayed organizer or curator with a %s so that there is a contact for this listing.<br>%s<br>%s", "confirmed email", "This email will not be visible if homepage is set or display is not checked, it is used only to identify the organizer's account.", "If none of the organizers has a confirmed account, add yourself and leave the organizer box unchecked.", )) # Don't try to create new_version using invalid input if errmsgs: return show_input_errors(errmsgs) else: # to make it obvious that these two statements should be together new_version = WebSeminar(shortname, data=data, organizer_data=organizer_data) # Warnings sanity_check_times(new_version.start_time, new_version.end_time) if not data["topics"]: flash_warning( "This series has no topics selected; don't forget to set the topics for each new talk individually." ) if seminar.new or new_version != seminar: new_version.save() edittype = "created" if new else "edited" flash("Series %s successfully!" % edittype) elif seminar.organizer_data == new_version.organizer_data: flash("No changes made to series.") if seminar.new or seminar.organizer_data != new_version.organizer_data: new_version.save_organizers() if not seminar.new: flash("Series organizers updated!") return redirect(url_for(".edit_seminar", shortname=shortname), 302)
def save_seminar(): raw_data = request.form shortname = raw_data["shortname"] new = raw_data.get("new") == "yes" resp, seminar = can_edit_seminar(shortname, new) if resp is not None: return resp def make_error(shortname, col=None, err=None): if err is not None: flash_error("Error processing %s: {0}".format(err), col) seminar = WebSeminar(shortname, data=raw_data) manage = "Manage" if current_user.is_organizer else "Create" return render_template( "edit_seminar.html", seminar=seminar, title="Edit seminar error", section=manage, institutions=institutions(), lock=None, ) if seminar.new: data = { "shortname": shortname, "display": current_user.is_creator, "owner": current_user.email, "archived": False, } else: data = { "shortname": shortname, "display": seminar.display, "owner": seminar.owner, } # Have to get time zone first data["timezone"] = tz = raw_data.get("timezone") tz = pytz.timezone(tz) def replace(a): if a == "timestamp with time zone": return "time" return a for col in db.seminars.search_cols: if col in data: continue try: val = raw_data.get(col) if not val: data[col] = None else: data[col] = process_user_input(val, replace( db.seminars.col_type[col]), tz=tz) except Exception as err: return make_error(shortname, col, err) data["institutions"] = clean_institutions(data.get("institutions")) data["topics"] = clean_topics(data.get("topics")) data["language"] = clean_language(data.get("language")) if not data["timezone"] and data["institutions"]: # Set time zone from institution data["timezone"] = WebInstitution(data["institutions"][0]).timezone organizer_data = [] for i in range(10): D = {"seminar_id": seminar.shortname} for col in db.seminar_organizers.search_cols: if col in D: continue name = "org_%s%s" % (col, i) try: val = raw_data.get(name) if val == "": D[col] = None elif val is None: D[col] = False # checkboxes else: D[col] = process_user_input( val, db.seminar_organizers.col_type[col], tz=tz) # if col == 'homepage' and val and not val.startswith("http"): # D[col] = "http://" + data[col] except Exception as err: return make_error(shortname, col, err) if D.get("email") or D.get("full_name"): D["order"] = len(organizer_data) ####### HOT FIX #################### # WARNING the header on the template # says organizer and we have agreed # that one is either an organizer or # a curator D["curator"] = not D["curator"] organizer_data.append(D) new_version = WebSeminar(shortname, data=data, organizer_data=organizer_data) if check_time(new_version.start_time, new_version.end_time): return make_error(shortname) if seminar.new or new_version != seminar: new_version.save() edittype = "created" if new else "edited" flash("Seminar %s successfully!" % edittype) elif seminar.organizer_data == new_version.organizer_data: flash("No changes made to seminar.") if seminar.new or seminar.organizer_data != new_version.organizer_data: new_version.save_organizers() if not seminar.new: flash("Seminar organizers updated!") return redirect(url_for(".edit_seminar", shortname=shortname), 301)
def save_seminar(): raw_data = request.form shortname = raw_data["shortname"] new = (raw_data.get("new") == "yes") resp, seminar = can_edit_seminar(shortname, new) if resp is not None: return resp def make_error(err): flash_error("Error processing %s: %s" % (col, err)) seminar = WebSeminar(shortname, data=raw_data) return render_template("edit_seminar.html", seminar=seminar, title="Edit seminar error", top_menu=basic_top_menu(), categories=categories(), institutions=institutions(), lock=None) if seminar.new: data = { 'shortname': shortname, 'display': current_user.is_creator(), 'owner': current_user.email, 'archived': False } else: data = { 'shortname': shortname, 'display': seminar.display or current_user.is_creator(), 'owner': seminar.owner } for col in db.seminars.search_cols: if col in data: continue try: val = raw_data.get(col) if not val: data[col] = None else: data[col] = process_user_input(val, db.seminars.col_type[col]) except Exception as err: return make_error(err) if not data['timezone'] and data['institutions']: # Set time zone from institution data['timezone'] = WebInstitution(data['institutions'][0]).timezone organizer_data = [] for i in range(6): D = {'seminar_id': seminar.shortname} for col in db.seminar_organizers.search_cols: if col in D: continue name = "org_%s%s" % (col, i) try: val = raw_data.get(name) if val == '': D[col] = None elif val is None: D[col] = False # checkboxes else: D[col] = process_user_input( val, db.seminar_organizers.col_type[col]) if col == 'homepage' and val and not val.startswith("http"): data[col] = "http://" + data[col] except Exception as err: return make_error(err) if D.get('email') or D.get('full_name'): D['order'] = len(organizer_data) organizer_data.append(D) new_version = WebSeminar(shortname, data=data, organizer_data=organizer_data) if seminar.new or new_version != seminar: new_version.save() if seminar.organizer_data != new_version.organizer_data: new_version.save_organizers() edittype = "created" if new else "edited" flash("Seminar successfully %s!" % edittype) return redirect(url_for("show_seminar", shortname=shortname), 301)