コード例 #1
0
def recipes_create():

    form = RecipeForm(request.form)
    form.recipe_id = -1
    # Checking that the form passes validations
    if not form.validate():
        return render_template("recipes/new.html", form=form)

    # Adding the new recipe
    name = form.name.data.strip()
    name = (name[0].upper() + name[1:])
    newRecipe = Recipe(name)
    newRecipe.instruction = form.instruction.data
    newRecipe.preptime = form.preptime.data
    newRecipe.account_id = current_user.id

    # Separating and adding tags
    tagsString = form.tags.data.strip()
    tags = tagsString.split(',')
    Tag.add_tags(tags, newRecipe)

    # Commiting changes
    db.session().add(newRecipe)
    db.session().commit()

    # Ingredients need recipe ID,
    # so they are added only after the recipe is added
    addedRecipe = Recipe.query.filter(Recipe.name == newRecipe.name).first()
    ingredients = form.ingredients.data.splitlines()
    Ingredient.add_ingredients(ingredients, addedRecipe)

    return redirect(url_for("recipes_index"))
コード例 #2
0
def tags_create(user_id):
    user = User.query.get(user_id)

    if not user:
        return render_template("error.html",
                               error="Käyttäjää ei  ole olemassa")
    if current_user.id != user.id and not current_user.has_role("ADMIN"):
        return render_template(
            "error.html",
            error="Sinulla ei ole oikeuksia lisätä tägejä toiselle käyttäjälle"
        )

    form = TagCreationForm(request.form)
    if not form.validate():
        tags_info = Tag.find_all_tags_and_numbers_of_tagged_games(user_id)
        return render_template("tags/list.html",
                               tag_owner=user,
                               tags_info=tags_info,
                               form=form)

    tag = Tag(form.name.data)
    tag.account_id = user_id
    db.session.add(tag)
    db.session.commit()
    return redirect(url_for("tags_index", user_id=user_id))
コード例 #3
0
ファイル: views.py プロジェクト: Mirex97/Pelifoorumi
def add_tag():
    if not current_user.role == 'Admin':
        redirect(url_for("tags_index"))
    form = TagForm(request.form)
    tags = Tag.find_with_tag(form.name.data)
    if tags:
        return redirect(request.referrer)
    if not form.validate():
        return redirect(request.referrer)
    t = Tag(form.name.data)
    db.session().add(t)
    db.session().commit()
    return redirect(url_for("tags_index"))
コード例 #4
0
def tips_create():
    form = TipForm(request.form)

    if (form.add_tag.data):
        if (len(form.tag.data) < 1 or len(form.tag.data) > 20):
            return render_template("tips/new.html", form = form, tagError = 'Tag must be between 1 and 20 characters long.')
        form.tags.append(form.tag.data)
        form.tag.data = ''
        return render_template("tips/new.html", form = form)

    if (form.add_link.data):
        data = form.link.data
        if not (data.startswith("http://") or data.startswith("https://") or data.startswith("www.")):
            return render_template("tips/new.html", form = form, linkError = "A valid URL is required.")
        form.links.append(form.link.data)
        form.link.data = ''
        return render_template("tips/new.html", form = form)

    if not form.validate():
        return render_template("tips/new.html", form = form)

    tip = Tip(form.content.data)
    tip.account_id = current_user.id

    for tag in form.tags:
        dbTag = Tag.query.filter_by(content=tag).first()
        if not dbTag:
            t = Tag(tag)
            t.account_id = current_user.id
            test = db.session().add(t)
            dbTag = Tag.query.filter_by(content=tag).first()
        tip.tags.append(dbTag)

    db.session().add(tip)
    db.session().commit()
    
    dbTip = Tip.query.filter_by(content=tip.content, account_id=current_user.id).first()

    for link in form.links:
        l = Link(link)
        l.account_id = current_user.id
        l.tip_id = dbTip.id
        db.session().add(l)

    db.session().commit()

    TipForm.tags = []
    TipForm.links = []
  
    return redirect(url_for('list_tips'))
コード例 #5
0
ファイル: views.py プロジェクト: joonaspartanen/tsoha-forum
def create_topic():
    form = TopicForm(request.form)

    form.tags.choices = [form.tags.data]

    if not form.validate():
        return render_template("topics/new.html", form=form)

    subject = form.subject.data
    body = form.body.data
    author_id = current_user.id

    initial_post = Post(body)
    initial_post.author_id = author_id
    topic = Topic(subject, author_id)
    topic.posts.append(initial_post)

    tag_names = form.tags.data[0].split(",")
    for tag_name in tag_names:
        tag = Tag.query.filter_by(name=tag_name).first()
        if not tag:
            tag = Tag(tag_name)
        topic.tags.append(tag)

    db.session().add(topic)
    db.session().commit()

    return redirect(url_for("topics_index"))
コード例 #6
0
def discussions_create():
    form = DiscussionForm(request.form)
    if not form.validate():
        return render_template(
            "discussions/new.html",
            form=DiscussionForm(),
            error=
            "Title length must be between 5 and 100 characters. Content must be between 5 and 2000 characters."
        )
    discussion = Discussion(form.title.data)
    discussion.set_account_id(current_user.id)
    #Tags are separated by whitespace, split them into array and append them to discussion separately.
    discussion_tags = form.tags.data.split(' ')
    for tagname in discussion_tags:
        # Check for empty strings
        if tagname and tagname.strip():
            tag = Tag(tagname)
            discussion.tags.append(tag)
    db.session().add(discussion)
    db.session().commit()

    message = Message(form.content.data)
    message.set_account_id(current_user.id)
    message.set_discussion_id(discussion.id)

    db.session().add(message)
    db.session().commit()

    return redirect(url_for("discussions_show", discussion_id=discussion.id))
コード例 #7
0
ファイル: views.py プロジェクト: viljamiLatvala/reseptivihko
def tag_create():
    if current_user.get_role() != 'admin':
        return abort(401)

    form = TagForm(request.form)
    if not form.validate():
        return render_template("tags/new.html", form=form)

    name = form.name.data.strip()
    newTag = Tag(name.capitalize())
    newTag.description = form.description.data

    db.session().add(newTag)
    db.session().commit()

    return redirect(url_for("tags_index"))
コード例 #8
0
def tags_index(user_id):
    tag_owner = User.query.get(user_id)
    tags_info = Tag.find_all_tags_and_numbers_of_tagged_games(user_id)
    return render_template("tags/list.html",
                           tag_owner=tag_owner,
                           tags_info=tags_info,
                           form=TagCreationForm())
コード例 #9
0
ファイル: views.py プロジェクト: Mirex97/Pelifoorumi
def show_thread(thread_id):
    thread = Thread.query.get(thread_id)
    if thread.hidden and not current_user.is_authenticated:
        print("NOT ALLOWED")
        return redirect(request.referrer)
    form = CommentForm()
    form.thread_id.data = thread.id
    return render_template(
        "threads/thread.html",
        thread=Thread.query.get(thread_id),
        owner=User.query.get(thread.account_id),
        comments=Comment.find_comments_with_thread(thread_id),
        form=form,
        tags=Tag.find_tags(),
        users=User.query.all(),
        threadtags=Tag.find_tags_with_thread(thread_id),
        section=Section.query.get(thread.section_id))
コード例 #10
0
def recipe_edit(recipe_id):

    # POST is not accepted if current user is not the creator of the recipe
    # or an administrator
    recipe = Recipe.query.get(recipe_id)
    if ((recipe.account_id is not current_user.get_id())
            and (current_user.get_role() != 'admin')):

        return abort(401)

    form = RecipeEditForm(request.form)

    # If form does not pass validations,
    # a new faulty form is created to be shown along with error messages,
    # but never put to the database
    if not form.validate():
        faultyRecipe = Recipe(request.form['name'])
        faultyRecipe.id = recipe_id
        faultyRecipe.instruction = request.form['instruction']
        faultyRecipe.preptime = request.form.get("preptime")
        faultyIngredients = request.form.get("ingredients")
        faultyTags = request.form.get("tags")
        return render_template("recipes/edit.html",
                               recipe=faultyRecipe,
                               form=form,
                               tags=faultyTags,
                               ingredients=faultyIngredients)

    # Fetching and editing the recipe
    changedRecipe = Recipe.query.get(recipe_id)
    name = request.form.get("name").strip()
    name = name[0].upper() + name[1:]
    changedRecipe.name = name
    changedRecipe.instruction = request.form.get("instruction")
    changedRecipe.preptime = request.form.get("preptime")

    # Add tags for the recipe
    tags = form.tags.data.split(',')
    Tag.add_tags(tags, changedRecipe)

    db.session().commit()

    ingredients = request.form.get("ingredients").splitlines()
    Ingredient.add_ingredients(ingredients, changedRecipe)

    return redirect(url_for("recipes_index"))
コード例 #11
0
ファイル: views.py プロジェクト: Raincoon/OddRealmBotany
def tag_new():

    # forms
    if request.method == "GET":
        return render_template("tags/tag_new.html", form=TagForm())

    form = TagForm(request.form)
    if not form.validate():
        return render_template("tags/tag_new.html", form=form)

    name = form.name.data

    # creating the tag
    t = Tag(name)
    t.owner_id = current_user.id

    db.session().add(t)
    db.session().commit()
    return redirect(url_for("index"))
コード例 #12
0
ファイル: views.py プロジェクト: viljamiLatvala/reseptivihko
def tag_edit(tag_id):
    # POST is not accepted if current user is not an administrator
    if current_user.get_role() != 'admin':
        return abort(401)

    editedTag = Tag.query.get(tag_id)
    form = TagForm(request.form)

    if not form.validate():
        faultyTag = Tag(request.form['name'])
        faultyTag.id = tag_id
        faultyTag.description = Tag(request.form['description'])
        return render_template("tags/edit.html", form=TagForm(), tag=faultyTag)

    name = form.name.data.strip()
    editedTag.name = name.capitalize()
    editedTag.description = form.description.data

    db.session().commit()

    return redirect(url_for("tags_index"))
コード例 #13
0
def tags_create():
    form = TagForm(request.form)

    if not form.validate():
        return render_template("tags/new.html", form=form)

    tag = Tag(form.name.data)

    db.session().add(tag)
    db.session().commit()

    return redirect(url_for("tags_index"))
コード例 #14
0
def tags_editor(tag_id):

    form = EditTagForm(request.form)
    tag = Tag.query.get(tag_id)

    if not form.validate:
        return render_template("tags/new.html", form=form)

    if form.name.data:
        if not Tag.unique_name(form.name.data):
            tag.name = form.name.data

    db.session().commit()

    return redirect(url_for("tags_edit", tag_id=tag_id))
コード例 #15
0
ファイル: views.py プロジェクト: gitblast/coding-tips
def index():
    return render_template("index.html",
                           tips=Tip.count(),
                           tags=Tag.count(),
                           users=User.count())
コード例 #16
0
def index():
    return render_template(
        "index.html",
        unstarted_tasks=User.find_users_with_unstarted_tasks(),
        tasks_roles=User.find_number_of_tasks_by_user_roles(),
        used_tags=Tag.find_used_tags())
コード例 #17
0
ファイル: views.py プロジェクト: viljamiLatvala/reseptivihko
def detach_recipe(tag_id, recipe_id):
    if current_user.get_role() != 'admin':
        return abort(401)
    Tag.detach_recipe(tag_id, recipe_id)
    return redirect(url_for("tags_index"))
コード例 #18
0
ファイル: views.py プロジェクト: viljamiLatvala/reseptivihko
def tags_index():
    return render_template("tags/list.html", tags=Tag.tags_summary())
コード例 #19
0
ファイル: views.py プロジェクト: viljamiLatvala/reseptivihko
def tag_info(tag_id):
    tag = Tag.query.get(tag_id)
    recipes = Tag.find_recipes_with_tag(tag)
    return render_template("tags/tag.html", tag=tag, recipes=recipes)
コード例 #20
0
ファイル: views.py プロジェクト: Mirex97/Pelifoorumi
def find_threads(tag):
    threads = Tag.find_threads_by_tag(tag)
    return render_template("threads/search.html", threads=threads)
コード例 #21
0
def update_tip_property(id, what):
    tip = Tip.query.get(id)
    form = TipForm(request.form)
    
    if what == 'text':
        tip.content = form.content.data
        db.session().commit()
        updated = Tip.query.get(id)
        form.tags = list(map(lambda tag: tag.content, tip.tags))
        form.links = list(map(lambda link: link.url, tip.links))
        return render_template('tips/update.html', tip = updated, form = form, text = updated.content, text_msg = "Tip content updated succesfully")

    if what == 'links':
        for tag in tip.tags:
            if tag.content not in form.tags:
                form.tags.append(tag.content)
        for link in tip.links:
            if link.url not in form.links:
                form.links.append(link.url)
        if (form.add_link.data):
            data = form.link.data
            if not (data.startswith("http://") or data.startswith("https://") or data.startswith("www.")):
                return render_template("tips/update.html", tip = tip, form = form, text = tip.content, linkError = "A valid URL is required.")
            if data in form.links:
                return render_template("tips/update.html", tip = tip, form = form, text = tip.content, linkError = "No duplicate links allowed.")
            form.links.append(form.link.data)
            form.link.data = ''
            return render_template("tips/update.html", form = form, tip = tip, text = tip.content)

        for link in form.links:
            l = Link(link)
            l.account_id = current_user.id
            l.tip_id = tip.id
            db.session().add(l)

        db.session().commit()

        updated = Tip.query.get(id)

        return render_template('tips/update.html', tip = updated, form = form, text = tip.content, link_msg = "Links updated successfully")

    if what == 'tags':
        for link in tip.links:
            if link.url not in form.links:
                form.links.append(link.url)
        for tag in tip.tags:
            if tag.content not in form.tags:
                form.tags.append(tag.content)
        if (form.add_tag.data):
            if (len(form.tag.data) < 1 or len(form.tag.data) > 20):
                return render_template("tips/update.html", tip = tip, text = tip.content, form = form, tagError = 'Tag must be between 1 and 20 characters long.')
            if tag in form.tags:
                return render_template("tips/update.html", tip = tip, text = tip.content, form = form, tagError = 'No duplicate tags allowed')
            
            form.tags.append(form.tag.data)
            form.tag.data = ''
            return render_template('tips/update.html', tip = tip, form = form, text = tip.content)

        for tag in form.tags:
            if tag not in list(map(lambda t: t.content, tip.tags)):
                dbTag = Tag.query.filter_by(content=tag).first()
                if not dbTag:
                    t = Tag(tag)
                    t.account_id = current_user.id
                    test = db.session().add(t)
                    dbTag = Tag.query.filter_by(content=tag).first()
                tip.tags.append(dbTag)

        db.session().add(tip)
        db.session().commit()

        updated = Tip.query.get(id)
            
        return render_template('tips/update.html', tip = updated, form = form, text = updated.content, tag_msg = "Tags updated succesfully")