def project_edit(project_id): """ Renders form to edit specific project or process the form """ if not 'CURR_USER_KEY' in session: flash("Unauthorized access. Account needed to edit.", "danger") return redirect(f"/project/{project_id}") project = Project.lookup_project(project_id) project = Project.lookup_project(project_id) if g.user.id != project.user.id: flash("Unauthorized user. Correct user account needed.", "danger") return redirect(f"/project/{project_id}") form = ProjectForm(obj=project, prefix='form-project-edit-') tags_full_list = Tag.list_all() project_tags = [tag.name for tag in project.tags] tags_list_str = "|".join(project_tags) if form.validate_on_submit(): project.name = form.name.data project.description = form.description.data project.user_id = g.user.id project.contact_info_type = form.contact_info_type.data project.contact_info = form.contact_info.data project.lat = form.lat.data project.long = form.long.data project.pic_url1 = request.form.get('pic_url1') project.pic_url2 = request.form.get('pic_url2') tags = request.form.get('tags') if (tags): tags = tags.split('|') tag_objs = [] if (tags and len(tags) > 0): for name in tags: tag_obj = Tag.lookup_tag(name) if not tag_obj: tag_obj = Tag.create_tag(name) tag_objs.append(tag_obj) project.tags = tag_objs optional_date_keys = ['inquiry_deadline', 'work_start', 'work_end'] optional_date_values = [] for each in optional_date_keys: try: y, m, d = request.form.get(each).split('-') print(y, m, d, 'out of if') if y and m and d: date = datetime(int(y), int(m), int(d)) optional_date_values.append(date) print(date, 'in if', optional_date_values) except ValueError: pass optional_date_values.append(None) print('caught value error', optional_date_values) except AttributeError: optional_date_values.append(None) print('caught value error', optional_date_values) project.inquiry_deadline, project.work_start, project.work_end = optional_date_values db.session.commit() flash("Project edited successfully.", "success") return redirect(f"/project/{project_id}") return render_template("project-edit.html", form=form, project=project, tags_list_str=tags_list_str, tags_full_list=tags_full_list)
def project_new(): """ Renders form to create new project or process the form """ if not 'CURR_USER_KEY' in session: flash("Unauthorized access. Account needed to create.", "danger") if 'GUEST_GEOCODE' in session: return redirect("/search") else: return redirect("/") form = ProjectForm(prefix='form-project-new-') tags_full_list = Tag.list_all() if form.validate_on_submit(): optional_date_keys = ['inquiry_deadline', 'work_start', 'work_end'] optional_date_values = [] for each in optional_date_keys: try: y, m, d = request.form.get(each).split('-') print(y, m, d, 'out of if') if y and m and d: date = datetime(int(y), int(m), int(d)) optional_date_values.append(date) print(date, 'in if', optional_date_values) except ValueError: optional_date_values.append(None) print('caught value error', optional_date_values) except AttributeError: optional_date_values.append(None) print('caught value error', optional_date_values) inquiry_deadline_date, work_start_date, work_end_date = optional_date_values project_obj = { 'name': form.name.data, 'description': form.description.data, 'user_id': g.user.id, 'contact_info_type': form.contact_info_type.data, 'contact_info': form.contact_info.data, 'lat': form.lat.data, 'long': form.long.data, 'inquiry_deadline': inquiry_deadline_date, 'work_start': work_start_date, 'work_end': work_end_date, 'pic_url1': request.form.get('pic_url1'), 'pic_url2': request.form.get('pic_url2'), } project_final = Project.create_project(project_obj) tags = request.form.get('tags') if tags: tags = tags.split('|') tag_objs = [] if (len(tags) > 0): for name in tags: tag_obj = Tag.lookup_tag(name) if not tag_obj: tag_obj = Tag.create_tag(name) tag_objs.append(tag_obj) project_final.tags = tag_objs db.session.commit() flash("Project created successfully", "success") return redirect(f"/project/{project_final.id}") return render_template("project-new.html", form=form, tags_full_list=tags_full_list)
def test_create_duplicate_tag(self): """Make sure duplicate tags cannot be created""" with test_database(TEST_DB, (Tag, )): Tag.create_tag(tag='test_tag') with self.assertRaises(ValueError): Tag.create_tag(tag='test_tag')
def test_many_to_many_relationships(self): """Make sure JournalEntries and Tags can have many to many relationships""" journal_entry_data_1 = { 'title': 'entry 1', 'date': datetime.datetime.now().strftime('%Y-%m-%d'), 'time_spent': '111', 'what_i_learned': 'many to many relationships', 'resources_to_remember': 'docs.peewee-orm.com' } journal_entry_data_2 = { 'title': 'entry 2', 'date': datetime.datetime.now().strftime('%Y-%m-%d'), 'time_spent': '222', 'what_i_learned': 'many to many relationships', 'resources_to_remember': 'docs.peewee-orm.com' } with test_database(TEST_DB, (User, JournalEntry, Tag, JournalEntryTag)): # create the user UserModelTestCase.create_users(1) # create the journal entries journal_entry_data_1['user'] = User.select().get() journal_entry_data_2['user'] = User.select().get() JournalEntry.create(**journal_entry_data_1) JournalEntry.create(**journal_entry_data_2) entry1 = JournalEntry.get(JournalEntry.title == 'entry 1') entry2 = JournalEntry.get(JournalEntry.title == 'entry 2') # create the tags Tag.create_tag(tag='first') Tag.create_tag(tag='both') Tag.create_tag(tag='second') tag1 = Tag.get(Tag.tag == 'first') tag2 = Tag.get(Tag.tag == 'both') tag3 = Tag.get(Tag.tag == 'second') # tie tags to entries entry1.tags.add( [Tag.get(Tag.tag == 'first'), Tag.get(Tag.tag == 'both')]) entry2.tags.add( [Tag.get(Tag.tag == 'second'), Tag.get(Tag.tag == 'both')]) # assertions self.assertIn(tag1, entry1.tags) self.assertIn(tag2, entry1.tags) self.assertNotIn(tag3, entry1.tags) self.assertNotIn(tag1, entry2.tags) self.assertIn(tag2, entry2.tags) self.assertIn(tag3, entry2.tags) self.assertIn(entry1, tag1.journal_entries) self.assertNotIn(entry2, tag1.journal_entries) self.assertIn(entry1, tag2.journal_entries) self.assertIn(entry2, tag2.journal_entries) self.assertNotIn(entry1, tag3.journal_entries) self.assertIn(entry2, tag3.journal_entries)
def init_tags(self): language_list = [('en', 'tag'), ('ko', '태그'), ('ja', 'タグ')] for i in range(30): tag = Tag.create_tag(self.session) for language, name in language_list: tag.append_tag_name(self.session, language, f'{name}_{i + 1}')