def add(): """ Add an entry to database """ # if the form is submitted form = forms.EntryForm() tag_form = forms.TagForm() if form.validate_on_submit(): entry = models.Entry.create(title=form.title.data, date=form.date.data, duration=form.duration.data, learned=form.learned.data, resources=form.resources.data, slug=slugify(form.title.data)) if tag_form.validate_on_submit(): tags = tag_form.tags.data split_tags = [tag.strip() for tag in tags.split(',')] for split_tag in split_tags: if len(split_tag) > 0: models.Tag.create(tag=split_tag, entry=entry, slug=slugify(split_tag)) # tell them the entry was created flash('Entry created') return redirect(url_for('index')) # forward to the details page return render_template('new.html', form=form, tag_form=tag_form)
def tag(): """Create a tag""" form = forms.TagForm() if form.validate_on_submit(): models.Tag.create(name=form.name.data) flash("Tag created!", "success") return redirect(url_for('index')) return render_template('tag.html', form=form)
def createtag(): """Create Tag for Journal""" form = forms.TagForm() if form.validate_on_submit(): models.Tag.create(title=form.title.data,) flash("Tag Created! Thanks!", "success") return redirect(url_for('index')) return render_template('addtag.html', form=form)
def add_tag(): form = forms.TagForm() if form.validate_on_submit(): tag = models.Tag.create(tag=form.tag.data, description=form.description.data) flash("Tag created") return redirect(url_for('view_tag', tag_id=tag.id)) return render_template('tag_add.html', form=form)
def create_tag(): form = forms.TagForm() if form.validate_on_submit(): models.Tag.create(tag=form.tag.data.strip()) flash("Tag created.", "success") models.EntryTag.tag_current_entries( models.Tag.get(tag=form.tag.data.strip())) return redirect(url_for('index')) return render_template('create_tag.html', form=form)
def create_tag(): """create a tag to organize your journal entries.""" form = forms.TagForm() if form.validate_on_submit(): models.Tags.create(tag=form.tag.data.strip()) flash('Tag Created', 'success') models.PostTags.tag_current_posts( models.Tags.get(tag=form.tag.data.strip())) return redirect(url_for('view_posts')) return render_template('create_tag.html', form=form)
def edit_tag(tag_id): tag = None try: tag = models.Tag.get(models.Tag.id == tag_id) except models.DoesNotExist: abort(404) form = forms.TagForm(obj=tag) if form.validate_on_submit(): form.populate_obj(tag) tag.save() flash("Tag updated") return redirect(url_for('list_tags')) return render_template('tag_edit.html', form=form)
def tag(): """Tag view. DECORATION: route from Flask class login_required from flask_login :input: tags/create with GET and POST methods RETURNS: redirect to home page if form submit is valid else render_template from flask - renders tag.html with form flag """ form = forms.TagForm() if form.validate_on_submit(): models.Tag.create(name=form.name.data) flash("Tag created!", "success") return redirect(url_for('index')) return render_template('tag.html', form=form)
def entry(): """ handles logic of Entry and Tag creation renders EntryForm if inputs validate @classmethod models.Entry is called which instantiates a Entry Object and stores Entry to models.Entry also handles tag creation through Form1 which if Form1 validates Tags are processed and classmethods models.Tag.get_or_create_tags() is called will create tag and store in table unless Tag exists then will get related tag using db query. ManytoMany relationship created for Entry andTags using call models.JournalTags.create_relations() relationship is stored in through table JournalTags and user redirected to index @var form: obj forms.Entry @var form1: obj forms.Tag @return: flask template obj with contexts passed form,x` form1 @rtype: Union[str, werkzeug.wrappers.Response] """ form = forms.EntryForm() form1 = forms.TagForm() if form.validate_on_submit(): models.Entry.create_entry( user=g.user._get_current_object(), title=form.title.data, date=form.date.data, time_spent=timedelta(minutes=float(form.time_spent.data)), knowledge=form.knowledge.data, resources=form.resources.data.splitlines()) journal_entry = models.Entry.get(title=form.title.data) if form1.validate_on_submit(): tag_data = form1.name.data.split(',') tags = [] for item in tag_data: tag = models.Tag.get_or_create_tags(name=item.lstrip()) tags.append(tag) models.JournalTags.create_relations(journal_entry, tags) flash('journal entry published', category='success') return redirect(url_for('index')) return render_template('new.html', form=form, form1=form1)
def edit(slug): """ Add the ability to edit an entry """ try: entry = models.Entry.select().where(models.Entry.slug == slug).get() tags = models.Tag.select().where(models.Tag.entry == entry) tag_list = [] for tag in tags: tag_list.append(tag.tag) entry_tags = ", ".join(tag_list) except models.DoesNotExist: abort(404) else: form = forms.EntryForm(obj=entry) tag_form = forms.TagForm(tags=entry_tags) if request.method == 'POST': if form.validate_on_submit(): entry.title = form.title.data entry.date = form.date.data entry.duration = form.duration.data entry.learned = form.learned.data entry.resources = form.resources.data entry.slug = slugify(entry.title) entry.save() if tag_form.validate_on_submit(): q = models.Tag.delete().where(models.Tag.entry == entry) q.execute() tags = tag_form.tags.data split_tags = [tag.strip() for tag in tags.split(',')] for split_tag in split_tags: if len(split_tag) > 0: models.Tag.create(tag=split_tag, entry=entry, slug=slugify(split_tag)) flash('Entry updated', "success") return redirect(url_for('index')) # forward to the details page return render_template('new.html', form=form, tag_form=tag_form, entry=entry, entry_tags=entry_tags)
def node_read(request, id): mode = request.COOKIES.get('node_mode', 0) #print 'mode:', mode tpl = ('node_path.html', 'node_tree.html', 'node_graph.html')[int(mode)] node = models.Node.objects.get(pk=int(id)) if (node.isfacet()): form = forms.TagForm(initial={'parent': node}) else: form = forms.FacetForm(initial={'parent': node}) response = render_to_response( tpl, context_instance=RequestContext( request, { 'object': node, 'form': form, 'mode': mode, #'next': reverse('da.views.node_read', args=[id]), })) # FIXME: don't set existant cookie response.set_cookie('node_mode', mode) return response
def node_add(request): next = request.REQUEST.get('next', request.META['HTTP_REFERER']) #return redirect(next) if request.method == 'POST': parent = models.Node.objects.get(pk=int(request.POST['parent'])) if (parent.isfacet()): form = forms.TagForm(request.POST) else: form = forms.FacetForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(next) else: # FIXME return render_to_response(next, context_instance=RequestContext( request, { 'object': parent, 'form': form, 'next': next, })) else: return HttpResponseRedirect(next)
def edit_tags(slug): """Create, attach, remove, or delete tags.""" form = forms.TagForm() # Form for creating a tag tags = models.Tag.select() attach_tags = models.get_attach_tags(slug, tags) remove_tags = models.get_remove_tags(slug, tags) delete_tags = models.get_delete_tags(slug, tags) if form.validate_on_submit(): # If you decide to create a tag try: models.Tag.create(tag=form.tag.data) new_tag = models.Tag.get(models.Tag.tag == form.tag.data) return redirect(url_for('attach_tag', slug=slug, tag=form.tag.data)) except models.IntegrityError: flash("That tag already exists") pass return render_template('tags.html', form=form, slug=slug, attach_tags=attach_tags, remove_tags=remove_tags, delete_tags=delete_tags)
def edit_entries(entry_id): """ handles logic for updating both Entry and Tags The entry_id is passed to the view through the URL then the that id corresponding entry is queried: entry_to_edit corresponding tags queried: entry_tags entry_to_edit attr are processed before sending data to form to ensure correct formatting of data. data is populated into form1 ann form2 if form validates on submit the form data is assigned to each attribute of entry_to_edit then update query called upon the instance corresponding to entry_to_edit and rows are updated new data. if form2 is updated and validates on submit data is preprocessed into list lists items are checked for duplication and membership: classmethods create_or_get_tags is called if new tags are present and classmethod Journaltags.create_relationship is called to instantiate many to many relationship between entry_to_edit and tags. if tags are deleted by user JournalTags.destroy_retionship classmethod is called severing relationship between tags and entry @var:form @type:<forms.EditForm obj> @var:tags @type: list[str] @var:parsed_tags @type: str() @param entry_id: @type entry_id: int @return: template render and response edit.html @rtype: Union[str, None, werkzeug.wrappers.Response] """ # store value of query Entry with id that matches value passed # queries entry_to_edit = models.Entry.select().where( models.Entry.id == entry_id).get() entry_tags = (models.Tag.select().join(models.JournalTags).where( models.JournalTags.entry == entry_to_edit).order_by(models.Tag.name)) # all_tags = models.Tag.select() # variables tags = [tag.name for tag in entry_tags] parsed_tags = ','.join(tags) entry_owner = entry_to_edit.user entry_to_edit.date = datetime.strptime(entry_to_edit.date, '%Y-%m-%d') entry_to_edit.resources = str.join('\n', entry_to_edit.resources) entry_to_edit.time_spent = entry_to_edit.time_spent.hour * 60 + entry_to_edit.time_spent.minute form = forms.EditForm(obj=entry_to_edit) form1 = forms.TagForm(name=parsed_tags) # constraints = if owner of the Entry is not the current user do not allow edit if current_user == entry_owner: # populate the entry form with data from select query # upon the submit button update the contents of the entry if form.validate_on_submit(): entry_to_edit.title = form.title.data entry_to_edit.date = form.date.data entry_to_edit.time_spent = timedelta( minutes=float(form.time_spent.data)) entry_to_edit.knowledge = form.knowledge.data entry_to_edit.resources = form.resources.data.splitlines() models.Entry.save(entry_to_edit) if form1.validate_on_submit(): # first convert form data into a list tag_data = form1.name.data.split(',') # iterate through unedited tags for tag_obj in entry_tags: # if the tag is not found in form data the relationship to the entry is broken if tag_obj.name not in tag_data: # break relations is a class method performs delete query at the through table models.JournalTags.break_relations(tag_obj, entry_to_edit) # iterate through the form tag list for item in tag_data: if item not in tags: tag_data[:].remove(item) tag = [models.Tag.get_or_create_tags(item)] models.JournalTags.create_relations(entry_to_edit, tag) # alert the user that the update has taken place flash('hey we updated your entry', category='success') return redirect(url_for('index')) else: flash('you need to be the entries owner to edit this', category='error') return redirect(url_for('index')) return render_template('edit.html', form=form, form1=form1)