Exemplo n.º 1
0
def new_entry():
    form = forms.NewEntry()
    if form.validate_on_submit():
        if models.DoesNotExist():
            try:
                models.Entry.create_entry(
                    username=g.username._get_current_object(),
                    title=form.title.data.strip(),
                    date=form.date.data,
                    time_spent=form.time_spent.data,
                    what_i_learned=form.what_i_learned.data.strip(),
                    resources_to_remember=form.resources_to_remember.data.strip(),
                )
                tag_list = form.tags.data.replace(","," ").split()
                for tag in tag_list:
                    if models.DoesNotExist():
                        try:
                            models.Tag.create_tag(tag)
                        except models.IntegrityError:
                            pass
                    if models.DoesNotExist():
                        try:
                            models.TagEntry_Relationship.create_relationship(
                            tag=tag,
                            entry=form.title.data.strip()
                            )
                        except models.IntegrityError:
                            pass
                flash("Entry has been successfully posted.", "success")
            except models.IntegrityError:
                flash("Entry was not posted.", "failure")
        return redirect(url_for('index'))
    return render_template('new.html', form=form)
Exemplo n.º 2
0
def delete(id=0):
    """delete an entry"""
    try:
        entry = models.Entry.get(models.Entry.id == id)
        entry.delete_instance()
        flash('Entry deleted!','success')
        return redirect(url_for('index'))
    except models.DoesNotExist():
        flash('Error deleting entry','error')
        return redirect(url_for('index'))
Exemplo n.º 3
0
def edit_entry(id):
    entry = models.Entry.get(models.Entry.id == id)
    tags = entry.get_tag_names()
    tag_list = []
    for tag in tags:
        tag_list.append(tag.tag_name)
    tag_string = ", ".join(tag_list)

    form = forms.EditEntry(title=entry.title,date=entry.date,
                           time_spent=entry.time_spent,
                           what_i_learned=entry.what_i_learned,
                           resources_to_remember=entry.resources_to_remember,
                           tags=tag_string
                           )

    if form.validate_on_submit():
        if models.DoesNotExist():
            try:
                tag_list_new = form.tags.data.replace(",", " ").split()

                #Create tags and tag-entry relationships if they do not exist
                for tag in tag_list_new:
                    if tag not in tag_list:
                        if models.DoesNotExist():
                            try:
                                models.Tag.create_tag(tag)
                            except models.IntegrityError:
                                pass
                        if models.DoesNotExist():
                            try:
                                models.TagEntry_Relationship.create_relationship(
                                    tag=tag,
                                    entry=entry.title
                                )
                            except models.IntegrityError:
                                pass

                #Delete tags that have been removed
                for tag in tag_list:
                    if tag in tag_list_new:
                        pass
                    else:
                        tag_old = models.Tag.get(models.Tag.tag_name == tag)
                        relationship = models.TagEntry_Relationship.get(
                            tagged_entries=models.Entry.
                                get(models.Entry.id == entry.id),
                            entry_tags=models.Tag.
                                get(models.Tag.id == tag_old.id)
                        )
                        if len(tag_old.get_tagged_entries()) == 1:
                            tag_old.delete_instance()
                        relationship.delete_instance()

                #Update all other fields
                entry.username = g.username._get_current_object()
                entry.title = form.title.data.strip()
                entry.date = form.date.data
                entry.time_spent = form.time_spent.data
                entry.what_i_learned = form.what_i_learned.data.strip()
                entry.resources_to_remember = form.resources_to_remember.data.strip()
                entry.tags = form.tags.data.strip()
                entry.last_updated = datetime.datetime.now()
                entry.save()
                flash("Entry has been successfully updated.", "success")
            except models.IntegrityError:
                flash("Entry was not updated.", "failure")
        return redirect(url_for('index'))
    return render_template('edit.html', entry=entry, form=form)
Exemplo n.º 4
0
def load_user(userId):
    try:
        return models.User.get(models.User.id == userId)
    except models.DoesNotExist():
        return None