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 test_update_timestamp(self, app, client): from app.event.helpers import update_timestamp update_timestamp(self.event1.id) update_timestamp(self.event2.id) update_timestamp(self.event3.id) update_timestamp(self.event4.id) update_timestamp(self.event5.id) update_timestamp(self.event6.id) # rhs calculated by hand self.assertEqual(self.event1.timestamp, 1) self.assertEqual(self.event2.timestamp, 100 + 10 + 2) self.assertEqual(self.event3.timestamp, 2 * 100 + 10 + 20 + 3) self.assertEqual(self.event4.timestamp, 100 * 100 + 100 * 100 + 10 + 20 + 30 + 10) self.assertEqual(self.event5.timestamp, 100 * 100 + 101 * 100 + 10 + 20 + 30 + 11) self.assertEqual(self.event6.timestamp, 200 * 100 + 100 * 100 + 300 * 100 + 10 + 20 + 4) # test noop / no raise when using invalid id update_timestamp(100)
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))
def test_end_date(self, app, client): # TODO: test end dates, take care for borders of months, years and epochs self.assertEqual(self.event1.end_date(use_abbr=False), "2. Month 1 1, Epoch 1") self.assertEqual(self.event2.end_date(use_abbr=False), "4. Month 2 2, Epoch 1") self.assertEqual(self.event3.end_date(use_abbr=False), "6. Month 3 3, Epoch 1") self.assertEqual(self.event4.end_date(use_abbr=False), "11. Month 4 101, Epoch 2") self.assertEqual(self.event5.end_date(use_abbr=False), "12. Month 4 102, Epoch 2") self.assertEqual(self.event6.end_date(use_abbr=False), "5. Month 3 301, Epoch 3") # test permutations of params self.assertEqual(self.event4.end_date(use_abbr=True), "11. M4 101, E2") self.assertEqual(self.event4.end_date(use_abbr=True, use_epoch=False), "11. M4 101") self.assertEqual(self.event4.end_date(use_abbr=False, use_epoch=False), "11. Month 4 101") self.assertEqual(self.event4.end_date(use_abbr=False, use_epoch=False, use_year=False), "11. Month 4") self.assertEqual(self.event4.end_date(use_abbr=True, use_epoch=False, with_weekday=True), "D1, 11. M4 101") self.assertEqual(self.event4.end_date(use_abbr=False, use_epoch=False, with_weekday=True), "Day 1, 11. Month 4 101") self.assertEqual(self.event4.end_date(use_abbr=False, use_epoch=False, use_year=False, with_weekday=True), "Day 1, 11. Month 4") # set event6.day so that end-date falls on Day 4 (which has no abbreviation) self.event6.day = 3 self.commit() from app.event.helpers import update_timestamp update_timestamp(self.event6.id) # tests with no abbreviations available self.assertEqual(self.event6.end_date(use_abbr=True), "4. Month 3 301, Epoch 3") self.assertEqual(self.event6.end_date(use_abbr=True, use_epoch=False), "4. Month 3 301") self.assertEqual(self.event6.end_date(use_abbr=True, use_epoch=False, with_weekday=True), "Day 4, 4. Month 3 301") self.assertEqual(self.event6.end_date(use_abbr=True, use_epoch=False, use_year=False, with_weekday=True), "Day 4, 4. Month 3") # test transitions between months, years and epochs from app.event.models import Event # event ends in next month (10 days for first month + 5 days) event_month = Event(epoch_id=self.epochs[0].id, year=1, month_id=self.months[0].id, day=1, duration=15) # event ends in next year (100 days_per_year + 5 days) event_year = Event(epoch_id=self.epochs[0].id, year=1, month_id=self.months[0].id, day=1, duration=105) # event ends in next year and next month (100 days_per_year + 10 days first month + 5 days) event_month_year = Event(epoch_id=self.epochs[0].id, year=1, month_id=self.months[0].id, day=1, duration=115) # event ends in the next epoch (100 days_per_year * 100 years + 5 days) event_epoch = Event(epoch_id=self.epochs[0].id, year=1, month_id=self.months[0].id, day=1, duration=100*100+5) # event ends in the next epoch and next month (100 days_per_year * 100 years + 10 days first month + 5 days) event_month_epoch = Event(epoch_id=self.epochs[0].id, year=1, month_id=self.months[0].id, day=1, duration=100*100+15) # event ends in next epoch, month and year (100 days_per_year * 101 years + 10 days first month + 5 days) event_month_year_epoch = Event(epoch_id=self.epochs[0].id, year=1, month_id=self.months[0].id, day=1, duration=100*101+15) # event that starts in epoch 1 and ends in epoch 3, skips another year and a month as well event_skip_two_epochs = Event(epoch_id=self.epochs[0].id, year=1, month_id=self.months[0].id, day=1, duration=100*301+15) self.add_all([event_month, event_year, event_month_year, event_epoch, event_month_epoch, event_month_year_epoch, event_skip_two_epochs]) self.commit() update_timestamp(event_month.id) update_timestamp(event_year.id) update_timestamp(event_month_year.id) update_timestamp(event_epoch.id) update_timestamp(event_month_epoch.id) update_timestamp(event_month_year_epoch.id) update_timestamp(event_skip_two_epochs.id) self.assertEqual(event_month.end_date(use_abbr=False), "6. Month 2 1, Epoch 1") self.assertEqual(event_year.end_date(use_abbr=False), "6. Month 1 2, Epoch 1") self.assertEqual(event_month_year.end_date(use_abbr=False), "6. Month 2 2, Epoch 1") self.assertEqual(event_epoch.end_date(use_abbr=False), "6. Month 1 1, Epoch 2") self.assertEqual(event_month_epoch.end_date(use_abbr=False), "6. Month 2 1, Epoch 2") self.assertEqual(event_month_year_epoch.end_date(use_abbr=False), "6. Month 2 2, Epoch 2") self.assertEqual(event_skip_two_epochs.end_date(use_abbr=False), "6. Month 2 2, Epoch 3")
def set_up_events(self, timestamps=True): self.event_cat1 = EventCategory(name="Event Category 1") self.event_cat2 = EventCategory(name="Event Category 2") self.event_cat3 = EventCategory(name="Event Category 3") self.add_all([self.event_cat1, self.event_cat2, self.event_cat3]) self.commit() self.event1 = Event(name="Event 1", epoch_id=self.epochs[0].id, year=1, month_id=self.months[0].id, day=1, duration=1, created_by_id=self.admin.id, category_id=self.event_cat1.id) self.event2 = Event(name="Event 2", epoch_id=self.epochs[0].id, year=2, month_id=self.months[1].id, day=2, duration=2, created_by_id=self.admin.id, is_visible=False, category_id=self.event_cat1.id) self.event3 = Event(name="Event 3", epoch_id=self.epochs[0].id, year=3, month_id=self.months[2].id, day=3, duration=3, created_by_id=self.moderator.id, category_id=self.event_cat1.id) self.event4 = Event(name="Event 4", epoch_id=self.epochs[1].id, year=101, month_id=self.months[3].id, day=10, duration=1, created_by_id=self.moderator.id, is_visible=False, category_id=self.event_cat2.id) self.event5 = Event(name="Event 5", epoch_id=self.epochs[1].id, year=102, month_id=self.months[3].id, day=11, duration=1, created_by_id=self.user.id, category_id=self.event_cat2.id) self.event6 = Event(name="Event 6", epoch_id=self.epochs[2].id, year=301, month_id=self.months[2].id, day=4, duration=1, created_by_id=self.user.id, is_visible=False, category_id=self.event_cat3.id) self.add_all([self.event1, self.event2, self.event3, self.event4, self.event5, self.event6]) self.commit() if timestamps: from app.event.helpers import update_timestamp update_timestamp(self.event1.id) update_timestamp(self.event2.id) update_timestamp(self.event3.id) update_timestamp(self.event4.id) update_timestamp(self.event5.id) update_timestamp(self.event6.id)