def index(): cset = CalendarSetting.query.get(1) calendar = None if cset.finalized is True: calendar = gen_calendar_stats() elif current_user.is_admin(): flash( "The calendar has not been finalized, you have been redirected to the calendar setup.", "warning") return redirect(url_for('calendar.settings')) epochs = get_epochs() years = {} for e in epochs: years[e.id] = get_years_in_epoch(e.id) categories = get_event_categories() return render_template("calendar/index.html", settings=cset, calendar=calendar, epochs=epochs, years=years, categories=categories, title=page_title("Calendar"))
def edit(id, name=None): event = Event.query.filter_by(id=id).first_or_404() if not event.is_editable_by_user(): return deny_access(no_perm_url) form = EventForm() form.submit.label.text = "Save Event" form.category.choices = gen_event_category_choices() form.epoch.choices = gen_epoch_choices() form.month.choices = gen_month_choices() if request.method == "POST": form.day.choices = gen_day_choices(form.month.data) else: form.day.choices = gen_day_choices(event.month_id) if not event.is_hideable_by_user(): del form.is_visible if form.validate_on_submit(): event.name = form.name.data event.category_id = form.category.data event.description = form.description.data event.epoch_id = form.epoch.data event.year = form.year.data event.month_id = form.month.data event.day = form.day.data event.duration = form.duration.data if event.is_hideable_by_user(): event.is_visible = form.is_visible.data db.session.commit() update_timestamp(event.id) flash("Event was edited.", "success") return redirect(event.view_url()) elif request.method == "GET": form.name.data = event.name form.category.data = event.category_id form.description.data = event.description form.epoch.data = event.epoch_id form.year.data = event.year form.month.data = event.month_id form.day.data = event.day form.duration.data = event.duration if event.is_hideable_by_user(): form.is_visible.data = event.is_visible calendar_helper = gen_calendar_stats() return render_template("event/edit.html", form=form, calendar=calendar_helper, title=page_title(f"Edit Event '{event.name}'"))
def view(): cset = CalendarSetting.query.get(1) stats = None if cset.finalized is True: stats = gen_calendar_stats() return render_template("calendar/view.html", settings=cset, stats=stats, title=page_title("View Calendar"))
def update_timestamp(event_id): timestamp = 0 ev = Event.query.filter_by(id=event_id).first() stats = gen_calendar_stats() if ev is None: return years = ev.epoch.years_before + (ev.year - 1) days_into_year = ev.month.days_before + ev.day timestamp = years * stats["days_per_year"] + days_into_year ev.timestamp = timestamp db.session.commit()
def test_gen_calendar_stats(self, app, client): from app.calendar.helpers import gen_calendar_stats self.set_up_calendar(finalized=True) info = gen_calendar_stats() self.assertEqual(len(info["epochs"]), len(self.epochs)) self.assertEqual(len(info["months"]), len(self.months)) self.assertEqual(len(info["days"]), len(self.days)) self.assertEqual(len(info["moons"]), len(self.moons)) self.assertEqual(info["days_per_week"], len(self.days)) self.assertEqual(info["days_per_year"], 100) self.assertEqual(info["months_per_year"], len(self.months)) # TODO: maybe revisit after adding event categories to self self.assertEqual(len(info["categories"]), 0)
def create(): # noqa: C901 settings = EventSetting.query.get(1) form = EventForm() form.submit.label.text = "Create Event" form.category.choices = gen_event_category_choices() form.epoch.choices = gen_epoch_choices() form.month.choices = gen_month_choices() if request.method == "POST": form.day.choices = gen_day_choices(form.month.data) else: form.day.choices = gen_day_choices(1) form.category.data = settings.default_category form.is_visible.data = True if settings.default_epoch: form.epoch.data = settings.default_epoch if settings.default_year: form.year.data = settings.default_year if form.validate_on_submit(): new_event = Event(name=form.name.data, category_id=form.category.data, description=form.description.data, epoch_id=form.epoch.data, year=form.year.data, month_id=form.month.data, day=form.day.data, duration=form.duration.data) new_event.is_visible = form.is_visible.data db.session.add(new_event) db.session.commit() update_timestamp(new_event.id) flash("Event was created.", "success") return redirect(new_event.view_url()) elif request.method == "GET": # pre-select fields if get-params were passed epoch_id = request.args.get("epoch") year = request.args.get("year") category_id = request.args.get("category") # will do nothing if var is not an int or not in choices if epoch_id: try: form.epoch.data = int(epoch_id) except ValueError: pass # will do nothing if var is not an int or not in choices if year: try: form.year.data = int(year) except ValueError: pass # will do nothing if var is not an int or not in choices if category_id: try: form.category.data = int(category_id) except ValueError: pass calendar_helper = gen_calendar_stats() return render_template("event/create.html", form=form, calendar=calendar_helper, title=page_title("Add Event"))
def edit(id): event = Event.query.filter_by(id=id).first_or_404() form = EventForm() form.submit.label.text = "Save Event" form.category.choices = gen_event_category_choices() form.epoch.choices = gen_epoch_choices() form.month.choices = gen_month_choices() if request.method == "POST": form.day.choices = gen_day_choices(form.month.data) else: form.day.choices = gen_day_choices(event.month_id) # TODO: write custom decorator for this? if not current_user.has_admin_role() and current_user.has_event_role( ) and event.is_visible == False and event.created_by.has_admin_role(): flash_no_permission() return redirect(url_for(no_perm_url)) if not current_user.is_event_admin( ) and event.is_visible == False and not event.created_by == current_user: flash_no_permission() return redirect(url_for(no_perm_url)) if not current_user.is_event_admin(): del form.is_visible if form.validate_on_submit(): event.name = form.name.data event.category_id = form.category.data event.description = form.description.data event.epoch_id = form.epoch.data event.year = form.year.data event.month_id = form.month.data event.day = form.day.data event.duration = form.duration.data if current_user.is_event_admin(): event.is_visible = form.is_visible.data db.session.commit() update_timestamp(event.id) flash("Event was edited.", "success") return redirect(url_for("event.view", id=id)) elif request.method == "GET": form.name.data = event.name form.category.data = event.category_id form.description.data = event.description form.epoch.data = event.epoch_id form.year.data = event.year form.month.data = event.month_id form.day.data = event.day form.duration.data = event.duration if current_user.is_event_admin(): form.is_visible.data = event.is_visible calendar_helper = gen_calendar_stats() return render_template("event/edit.html", form=form, calendar=calendar_helper, title=page_title("Edit Event '%s'" % event.name))