示例#1
0
def threads_create():

    categories = [(c.id, c.name) for c in Category.query.all()]

    if request.method == "GET":
        form = ThreadForm()
        form.categories.choices = categories
        return render_template("threads/new.html", form=form)

    form = ThreadForm(request.form)
    form.categories.choices = categories

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

    t = Thread(form.title.data)
    t.account_id = current_user.id

    categories = form.categories.data
    for c_id in categories:
        c = Category.query.get(c_id)
        t.categories.append(c)

    db.session().add(t)
    db.session().flush()

    m = Message(form.content.data)
    m.thread_id = t.id
    m.account_id = current_user.id

    db.session().add(m)
    db.session().commit()

    return redirect(url_for("threads_index"))
示例#2
0
def threads_create():
    form = ThreadForm(request.form)

    if not form.validate():
        return render_template("threads/new_thread.html", form=form)

    try:
        # luodaan uusi lanka ja sille ID
        thread = Thread(form.title.data)
        thread.owner_id = current_user.id
        db.session.add(thread)
        db.session.flush()
        db.session.refresh(thread)

        # luodaan uusi postaus
        posted = Post(form.content.data)
        posted.account_id = current_user.id
        posted.thread_id = thread.id
        db.session().add(posted)
        db.session().commit()

        flash("Your new post was saved", "alert alert-info")
        return redirect(url_for("posts_index"))
    except:
        db.session.rollback()
        flash("Error occurred, could not save post", "alert alert-danger")
        return render_template("threads/new_thread.html", form=form)
示例#3
0
def threads_create(board):
    if request.method == "GET":
        return render_template("threads/threadform.html",
                               form=ThreadForm(),
                               board=board)

    form = ThreadForm(request.form)

    if not form.validate():
        return render_template("threads/threadform.html",
                               form=form,
                               board=board)

    t = Thread(form.title.data, form.text.data, board)

    if current_user.is_authenticated:
        t.moderator_id = current_user.id
        t.name = current_user.username
        current_user.actions_taken += 1

    if Thread.query.filter_by(board_id=board).count() >= 20:
        least_active_thread = Thread.query.filter_by(board_id=board).order_by(
            Thread.activity).first()
        db.session.delete(least_active_thread)

    db.session().add(t)
    db.session().commit()

    return redirect(url_for("messages_index", thread=t.id))
示例#4
0
def delete_message(message_id, form=None):
    search = False
    q = request.args.get('q')
    if q:
        search = True

    if form is None:
        form = MessageForm(request.form)

    m = Message.query.get(message_id)
    u = current_user

    if m.user_id != u.id and not u.is_admin():
        return redirect(url_for("threads_view", thread_id=m.thread_id))

    if m.original_post:
        Thread.delete_thread(m.thread_id)
        return redirect(url_for("threads_index"))

    Thread.delete_message(message_id)
    page = request.args.get(get_page_parameter(), type=int, default=1)
    (t, messages) = Thread.get_thread(m.thread_id)

    pagination = Pagination(page=page,
                            total=messages.total,
                            search=search,
                            record_name='messages',
                            css_framework='bootstrap4')

    return redirect(url_for("threads_view", thread_id=m.thread_id))
示例#5
0
def threads_search():
    form = SearchForm(request.form)

    if form.select.data:
        threads = Thread.search_threads_by_user(form.search.data)
        return render_template("threads/search.html", threads=threads)
    else:
        threads = Thread.search_threads_by_thread(form.search.data)
        return render_template("threads/search.html", threads=threads)
示例#6
0
def delete_category(category_id):
    cat = Category.query.get(category_id)
    threads = db.session.query(Thread).filter(
        Thread.category_id == category_id)
    for thread in threads:
        Thread.delete_thread(thread.id)
    db.session.delete(cat)
    db.session.commit()
    return redirect(url_for("categories_index"))
示例#7
0
def threads_create():
    form = ThreadForm(request.form)
    form.thread_category.choices = current_user.get_category_names()

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

    Thread.create_thread(title=form.title.data,
                         message_text=form.message_text.data,
                         user_id=current_user.id,
                         category_id=form.thread_category.data)

    return redirect(url_for("threads_index"))
示例#8
0
def threads_index():
    #List of lists(3 elements): [0] Thread.id, [1] Thread.topic, [2] List of Category names
    threadList = Thread.get_default_threadList()

    # List of list (2 elements): [0] Thread.id, [1] COUNT(posts in thread)
    postCount = Thread.thread_posts_count()

    # Join postCount.COUNT(posts) into threadList:([0] Thread.id, [1] Thread.topic, [2] List of Category names, COUNT(posts))
    if len(postCount) != 0:
        for i in range(len(threadList)):
            threadList[i].append(postCount[i][1])

    return render_template("threads/list.html", threadList=threadList)
示例#9
0
def threads_view(thread_id, form=None):
    search = False
    q = request.args.get('q')
    if q:
        search = True

    if form is None:
        form = MessageForm(request.form)

    page = request.args.get(get_page_parameter(), type=int, default=1)
    (t, messages) = Thread.get_thread(thread_id, page)

    if Category.query.get(t.category_id) not in current_user.categories:
        return redirect(url_for("threads_index"))

    pagination = Pagination(page=page,
                            total=messages.total,
                            search=search,
                            record_name='messages',
                            css_framework='bootstrap4')

    return render_template("threads/view.html",
                           thread=t,
                           messages=messages.items,
                           form=form,
                           pagination=pagination)
示例#10
0
def threads_reply(thread_id):
    form = MessageForm(request.form)

    if not form.validate():
        return threads_view(thread_id, form=form)

    thread = Thread.query.get(thread_id)

    if Category.query.get(thread.category_id) not in current_user.categories:
        return redirect(url_for("threads_index"))

    Thread.post_reply(thread_id=thread_id,
                      message_text=form.message_text.data,
                      user_id=current_user.id)

    return redirect(url_for("threads_view", thread_id=thread_id))
示例#11
0
def cables_create():
    if request.method == "GET":
        f = CableForm()
        f.setupChoices()
        return render_template("cables/new.html", form=f)

    form = CableForm(request.form)
    #Validoidaan CableFormin sisältämien kenttien datat, jos
    #niissä on häikkää, niin palautetaan cables/new.html sivu
    #virheviestin kera.
    form.setupChoices()
    if not form.validate():
        return render_template("cables/new.html", form=form)
    if not form.validate2(False):
        return render_template("cables/new.html", form=form)

    c = Cable(form.controller_a_id.data, form.controller_b_id.data,
              form.size.data, form.name.data, form.note.data)

    #Kerätään kaapelin size kentästä (esim 5x4) kaapelin säikeiden
    #lukumäärä ja luodaan sen mukaan kaapelin sisällä kulkevat
    #säikeet tietokantaan.
    tmp = form.size.data.split('x')
    n = int(tmp[0]) * int(tmp[1])
    for i in range(n):
        c.threads.append(Thread(i + 1, i + 1, i + 1, i + 1, "", ""))

    db.session().add(c)
    db.session().commit()

    log = Changelog(current_user.id, "Cable", "", c.id, "Create", "", "")
    db.session().add(log)
    db.session().commit()

    return redirect(url_for("cables_index"))
示例#12
0
def new_thread(id):
    form = ThreadForm(request.form)
    
    if request.method == "POST" and form.validate():
        t = Thread(form.title.data)
        t.user = current_user.id
        t.category = id
        c = Comment(form.text.data)
        db.session().add(t)
        db.session().commit()
        c.thread_id = t.id
        c.user = current_user.id
        db.session().add(c)
        db.session().commit()
        return redirect(url_for("read_thread", id=id, thread_id=t.id))
    
    return render_template("threads/new.html", form=ThreadForm())
示例#13
0
def threads_create():
    form = ThreadForm(request.form)

    if not form.validate():
        return render_template("threads/newthread.html", form=ThreadForm())

    t = Thread(form.header.data)

    db.session().add(t)
    db.session().commit()

    p = Post(form.content.data)
    p.account_id = current_user.id
    p.thread_id = Thread.find_by_name(t.header)
    db.session().add(p)
    db.session().commit()
    return redirect(url_for("threads_index"))
示例#14
0
def threads_create():
    try:
        form = ThreadForm(CombinedMultiDict((request.files, request.form)))

        if not form.validate():
            return render_template(
                "threads/newthread.html",
                form=form,
                error=
                "Invalid input. Make sure you have content in your comment and that any attachments are in correct formats."
            )

        image = form.image.data
        image_id = None
        if image is not None:
            image_id = add_image(image)

        t = Thread(form.title.data)
        t.account_id = current_user.id

        db.session().add(t)
        db.session().commit()

        c = Comment(form.comment.data)
        c.account_id = current_user.id
        c.thread_id = t.id

        if image_id:
            c.image_id = image_id

        db.session().add(c)
        db.session().commit()

        delete_extra_threads()

        return redirect(url_for("main"))
    except:
        print("Something went wrong.")
        db.session().rollback()

        if t:
            db.session().delete(t)
        if image:
            db.session().delete(image)

    return redirect(url_for("page_404"))
示例#15
0
def index():
    countUsers = User.total_users()
    countThreads = Thread.total_threads()
    countPosts = Post.total_posts()

    return render_template("index.html",
                           countUsers=countUsers,
                           countThreads=countThreads,
                           countPosts=countPosts)
示例#16
0
def thread_create():
    user_choices = request.form.getlist("choices")
    topics = []
    for user_choice in user_choices:
        topics.append(Topic.query.get(int(user_choice)))

    new_thread = Thread(title="väliaikainen otsikko", time_of_opening=datetime.now(), author_id=current_user.id, topics=topics)
    db.session.add(new_thread)
    db.session.commit()

    form = MessageForm(request.form)
    if not form.validate():
        return render_template("message_opening_form.html", form = form)
    new_message = Message(title=form.title.data, content=form.content.data, time_of_sending=datetime.now(), author_id=current_user.id, thread_id=new_thread.id)
    new_thread.title = new_message.title

    db.session.add(new_message)
    db.session.commit()
    return render_template("thread_details.html", thread = new_thread)
示例#17
0
def create_thread():
    form = ThreadForm(request.form)
    if not form.validate():
        return render_template("threads/new.html", form=form)

    thread = Thread(form.title.data, form.content.data, current_user.id)
    db.session().add(thread)
    db.session().commit()

    return redirect(url_for("threads_index"))
示例#18
0
def boards_post_thread(board_id):
    board = Board.query.get(board_id)
    
    if request.method == "GET":
        return render_template("boards/new-thread.html", form = ThreadForm(), board = board)

    form = ThreadForm(request.form)

    if not form.validate():
        return render_template("boards/new-thread.html", form = form, board = board)

    thread = Thread(form.title.data)
    thread.user_id = current_user.id
    thread.board_id = board.id

    db.session().add(thread)
    db.session().commit()
  
    return redirect(url_for("boards_index"))
示例#19
0
def threads_open(threadId):

    # List(4 elements): [0] Thread.id, [1] Thread.topic, [2] Thread.created, [3] Thread.modified
    threadData = Thread.get_thread(threadId)

    # List of list(6 elements): [0] Account.id, [1] Account.username, [2] Post.id, [3] Post.message, [4] Post.created, [5] Post.modified
    postsData = Post.get_posts_in_thread(threadId)

    return render_template("threads/open.html",
                           threadData=threadData,
                           postsData=postsData)
示例#20
0
def read_thread(id, thread_id):
    form = CommentForm(request.form)
    thread = Thread.query.get(thread_id)
    
    comments = Thread.comment_thread(thread)

    if request.method == "POST" and form.validate():
        new_comment(thread_id, form)
        return redirect(url_for("read_thread", id=id, thread_id=thread_id))
    
    return render_template("threads/thread.html", category=id, thread=thread, comments=comments, form=form)
示例#21
0
def user_panel(user_id):
    try:
        user_id = int(user_id)
    except ValueError:
        flash("No such user")
        return redirect(url_for("category_index"))
    if not current_user.id == user_id and not current_user.has_role("admin"):
        flash("Authentication error")
        return redirect(url_for("category_index"))
    return render_template("control/user.html",
                           user=user_datastore.get_user(user_id),
                           recent_posts=Post.find_latest_posts(user_id),
                           recent_threads=Thread.find_latest_threads(user_id))
示例#22
0
def threads_create():
    if request.method == "GET":
        return render_template("threads/new.html", form=ThreadForm())

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

    # Create Thread
    dbThread = Thread(form.topic.data)
    db.session().add(dbThread)
    db.session().flush()

    # Add Thread category dependicies
    if (form.yleinen.data == True):
        dbCategory = Category.query.filter_by(name="Yleinen").first()
        dbThread.categories.append(dbCategory)

    if (form.retro.data == True):
        dbCategory = Category.query.filter_by(name="Retro").first()
        dbThread.categories.append(dbCategory)

    if (form.wii.data == True):
        dbCategory = Category.query.filter_by(name="Wii").first()
        dbThread.categories.append(dbCategory)

    if (form.wiiu.data == True):
        dbCategory = Category.query.filter_by(name="WiiU").first()
        dbThread.categories.append(dbCategory)

    if (form.switch.data == True):
        dbCategory = Category.query.filter_by(name="Switch").first()
        dbThread.categories.append(dbCategory)

    if (form.ds.data == True):
        dbCategory = Category.query.filter_by(name="DS").first()
        dbThread.categories.append(dbCategory)

    if (form.threeDs.data == True):
        dbCategory = Category.query.filter_by(name="3DS").first()
        dbThread.categories.append(dbCategory)

    # Create Post
    dbPost = Post(form.message.data, 1)
    dbPost.account_id = current_user.id
    dbPost.thread_id = dbThread.id
    db.session().add(dbPost)

    db.session().commit()
    return redirect(url_for("threads_index"))
示例#23
0
def threads_index():

    categories = [(c.id, c.name) for c in Category.query.all()]

    if request.method == "GET":
      form = SelectCategoryForm()
      form.category.choices = categories
      return render_template("threads/list.html", threads=Thread.query.order_by(Thread.date_modified.desc()).all(), form=form)
    
    form = SelectCategoryForm(request.form)
    form.category.choices = categories
    category_id = form.category.data

    thread_ids = Thread.get_list_of_threads_by_category_id(category_id)
    return render_template("threads/list.html", threads=Thread.query.filter(Thread.id.in_(thread_ids)).order_by(Thread.date_modified.desc()).all(),
    form = form)
示例#24
0
def threads_index():
    search = False
    q = request.args.get('q')
    if q:
        search = True

    page = request.args.get(get_page_parameter(), type=int, default=1)
    threads = Thread.get_threads(page)

    pagination = Pagination(page=page,
                            total=threads.total,
                            search=search,
                            record_name='threads',
                            css_framework='bootstrap4')
    return render_template("threads/list.html",
                           threads=threads.items,
                           pagination=pagination)
示例#25
0
def statistics():
    # käyttäjien kokonaismäärä
    number_of_users = User.total_number_of_users()
   
    # viestejä lähettäneiden kokonaismäärä
    number_of_posters = User.total_number_of_posting_users()

    # viestien määrä
    number_of_messages = Message.total_number_of_messages()

    # ketjujen määrä
    number_of_threads = Thread.total_number_of_threads()

    return render_template("statistics.html", 
                            number_of_users = number_of_users,
                            number_of_posters = number_of_posters,
                            number_of_messages = number_of_messages,
                            number_of_threads = number_of_threads)
示例#26
0
def threads_create():
    form = ThreadForm(request.form)
    form.name.data = form.name.data.strip()

    if not form.validate():
        return redirect(request.referrer)

    t = Thread(form.name.data)
    t.desc = form.desc.data
    t.account_id = current_user.id
    t.section_id = form.section_id.data
    t.hidden = form.hidden.data

    db.session().add(t)
    db.session().commit()

    return redirect(url_for("show_thread", thread_id=t.id))
示例#27
0
文件: views.py 项目: OlliJ5/Shneakers
def threads_create():
    form = ThreadForm()
    form.category.choices = [(c.id, c.name) for c in Category.query.all()]

    if form.validate_on_submit():
        t = Thread(form.title.data, form.text.data)
        t.creator = current_user.username
        t.account_id = current_user.id
        t.category_id = form.category.data
        t.category_name = Category.query.filter_by(
            id=form.category.data).first().name

        db.session().add(t)
        db.session().commit()

        return redirect(url_for("threads_index", page_num=1))

    return render_template("threads/new.html", form=form)
示例#28
0
def thread_new(cat_id):
    if request.method == "GET":
        return render_template("thread/new_thread.html",
                               user_id=current_user.id,
                               cat_id=cat_id,
                               form=ThreadForm())
    form = ThreadForm(request.form)

    form.title.data = escape(form.title.data)
    form.content.data = escape(form.content.data)

    if not form.validate():
        return render_template("thread/new_thread.html",
                               cat_id=cat_id,
                               form=form)

    thread = Thread(title=form.title.data,
                    content=form.content.data,
                    category_id=cat_id,
                    user_id=current_user.id)
    db.session().add(thread)
    db.session().commit()
    flash("Thread created")
    return redirect(url_for("category_list", category_id=cat_id))
示例#29
0
def routes_view_one(route):
    r = Thread.get_route(route)
    return render_template("routes/view.html", route = r)
示例#30
0
def routes_index():
    routes = Thread.find_routes()
    return render_template("routes/list.html", routes = routes)