Exemple #1
0
def create_db():
    db.drop_all()
    db.create_all()

    hash = hash_password('password')
    user_datastore.create_user(email='*****@*****.**',
                               password=hash,
                               username='******')
    admin = user_datastore.create_user(email='*****@*****.**',
                                       password=hash,
                                       username='******')
    role = user_datastore.create_role(
        name='admin', description='user with administrative privileges')
    admin.roles.append(role)

    bestsellers = Topic(name='Bestsellers',
                        description='Discussions about best-selling books')
    new_releases = Topic(
        name='New Releases',
        description='Discussions about upcoming book releases')
    what_to_read = Topic(name='What Should I Read Next?',
                         description='Discussions about reading suggestions')
    authors = Topic(name='Authors',
                    description='Discussions about book authors')

    db.session.add_all([bestsellers, new_releases, what_to_read, authors])
    db.session.commit()
Exemple #2
0
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"))
Exemple #3
0
def topics_create():
    form = TopicForm(request.form)
    if form.title.validate(form) and form.message.validate(form):
        place = db.session.query(Place).get(form.place.data)
        if form.place.data == 0:
            place_id = 0
        else:
            place_id = place.id
          
        topic = Topic(form.title.data, place_id)
        db.session().add(topic)
        db.session().commit()
        topic_timefix = Topic.query.get(topic.id)
        topic_timefix.date_created = datetime.now().replace(microsecond=0, second = 0)
        db.session().commit

        message = Message(form.message.data, topic.id, current_user.id)
        db.session().add(message)
        db.session().commit()
        message_timefix = Message.query.get(message.id)
        message_timefix.date_created = datetime.now().replace(microsecond=0, second = 0)
        db.session().commit()

        newTopicAccount = topicAccount.insert().values(topic_id=topic.id, account_id=current_user.id)
        db.session().execute(newTopicAccount)
        db.session.commit()

        return redirect(url_for("topics_index"))
    
    places = db.session.query(Place)
    form=TopicForm()
    form.place.choices = [("0", "---")] + [(i.id, i.name) for i in places]

    return render_template("topics/new.html", form = form, error="topic name must be between 1-100 characters and message between 1-1000 characters")
Exemple #4
0
def topics_create():
    form = TopicForm(request.form)

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

    topic = Topic(form.name.data)

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

    return redirect(url_for("topics_index"))
Exemple #5
0
def topics_create():
    form = TopicForm(request.form)

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

    t = Topic(form.title.data)
    t.creator = current_user.name

    if current_user.admin:
        db.session.add(t)
        db.session.commit()

    return redirect(url_for("topics_index"))
Exemple #6
0
def rename_topic(topic_id):
    form = TopicForm(request.form)

    topic = Topic.query.get(topic_id)
    topicname = Topic(form.name.data)

    if not form.validate():
        return render_template("topics/list.html",
                               topics=Topic.query.all(),
                               form=form)

    topic.name = topicname.name
    db.session().commit()

    return redirect(url_for("topics_index"))
Exemple #7
0
def topic_create():
    form = TopicForm(request.form)
    name = form.name.data

    # validoidaan syöte
    if not form.validate():
        return render_template("topic_creation_form.html", form=form)

    # tarkistetaan löytyykö aihe jo tietokannasta, estetään duplikaatti
    already_exists = Topic.topic_already_exists(name)
    if already_exists == True:
        return render_template("topic_list.html", topics=Topic.query.all())

    # lisätään tietokantaan
    new_topic = Topic(name=form.name.data)
    db.session.add(new_topic)
    db.session.commit()

    # paluu aihelistaan
    return render_template("topic_list.html", topics=Topic.query.all())