Exemplo n.º 1
0
def theories():
    aut = AuthorSearch()
    tit = TitleSearch()
    form = SubmitButton()

    theor = TheoryModel(db.get_connection())
    all_teories = theor.get_all()
    all_teories = all_teories[::-1]
    result = []
    note = 'Все теории:'

    # Если теория больше 100 символов, то она печатается в сокращенном виде
    for i in range(len(all_teories)):
        if len(all_teories[i][2]) > 100:
            all_teories[i] = (all_teories[i][0], all_teories[i][1],
                              all_teories[i][2][:100] + '...',
                              all_teories[i][3], all_teories[i][4])

    if form.validate_on_submit():
        author = request.form['author']
        title = request.form['title']

        if author and not title:
            note = 'По вашему запросу нашлось:'
            for elem in all_teories:
                if author.lower() in elem[3].lower():
                    result.append(elem)

        if not author and title:
            note = 'По вашему запросу нашлось:'
            for elem in all_teories:
                if title.lower() in elem[1].lower():
                    result.append(elem)

        if author and title:
            note = 'По вашему запросу нашлось:'
            for elem in all_teories:
                if title.lower() in elem[1].lower() and author.lower(
                ) in elem[3].lower():
                    result.append(elem)

        if len(result) == 0:
            note = 'По вашему запросу ничего не нашлось!'

        return render_template('theories.html',
                               aut=aut,
                               tit=tit,
                               form=form,
                               t=result,
                               n=note)
    else:
        return render_template('theories.html',
                               aut=aut,
                               tit=tit,
                               form=form,
                               t=all_teories,
                               n=note)
Exemplo n.º 2
0
def read_my_theories():
    if 'username' not in session:
        return redirect('/sign_in')

    theor = TheoryModel(db.get_connection())
    tit = TitleSearch()
    form = SubmitButton()
    result = []

    err = 'Все ваши теории'
    if UserModel(db.get_connection()).exists(session['username'])[0]:
        id = UserModel(db.get_connection()).exists(session['username'])[1]

    all_teories = theor.get_all(user_id=id)
    all_teories = all_teories[::-1]

    for i in range(len(all_teories)):
        if len(all_teories[i][2]) > 100:
            all_teories[i] = (all_teories[i][0], all_teories[i][1],
                              all_teories[i][2][:100] + '...',
                              all_teories[i][3], all_teories[i][4])

    if len(all_teories) == 0:
        err = 'Вы еще не добавили ни одной теории!'
        return render_template('read_my_theories.html',
                               tit=tit,
                               form=form,
                               t=result,
                               n=err)

    if form.validate_on_submit():
        # Поиск по автору и по содержанию
        title = request.form['title']

        if title:
            err = 'По вашему запросу нашлось:'
            for elem in all_teories:
                if title.lower() in elem[1].lower():
                    result.append(elem)

        if len(result) == 0:
            err = 'По вашему запросу ничего не нашлось!'

        return render_template('read_my_theories.html',
                               tit=tit,
                               form=form,
                               t=result,
                               n=err)

    else:
        return render_template('read_my_theories.html',
                               n=err,
                               t=all_teories,
                               tit=tit,
                               form=form)
Exemplo n.º 3
0
def cabinet_other(user_id):
    if 'username' not in session:
        return redirect('/sign_in')

    if user_id == session['id']:
        return redirect('/cabinet')

    um = UserModel(db.get_connection())
    all = um.get(user_id)

    if all:
        tm = TheoryModel(db.get_connection())
        content = 'Всего теорий: ' + str(len(tm.get_all(user_id=all[0])))

        return render_template('cabinet_other.html',
                               n=all[1],
                               i=all[0],
                               d=all[4],
                               l=content,
                               q=None,
                               s=all[5],
                               p=all[6])
    else:
        return render_template('cabinet_other.html', q='1')
Exemplo n.º 4
0
def cabinet():
    if 'username' not in session:
        return redirect('/sign_in')

    um = UserModel(db.get_connection())
    all = []
    err = None

    if um.exists(session['username'])[0]:
        all = um.get(um.exists(session['username'])[1])

    tm = TheoryModel(db.get_connection())
    content = 'Всего теорий: ' + str(len(tm.get_all(user_id=all[0])))

    form = LoadPhotoForm()

    # Загрузка фотографии
    if form.validate_on_submit():
        f = form.file.data

        if f:
            name = f.filename
            session['photo'] = name
            result = open(name, 'wb')
            result.write(f.read())
            result.close()

            way = 'static/img/' + name

            # Если фотография уже существует в нашей папке, то удаляем ее
            move_photo(way, name)

            # Проверка того, является ли файл фотографией
            if not imghdr.what(way):
                err = 'Файл недопустимого формата'
                os.remove(way)
                return render_template('cabinet.html',
                                       n=session['username'],
                                       i=all[0],
                                       d=all[4],
                                       l=content,
                                       s=all[5],
                                       p=all[6],
                                       err=err)
            else:
                um = UserModel(db.get_connection())
                if um.exists(session['username'])[0]:
                    id = um.exists(session['username'])[1]
                um.change_photo(name, id)

        return redirect('/cabinet')

    else:
        return render_template('cabinet.html',
                               n=session['username'],
                               i=all[0],
                               d=all[4],
                               l=content,
                               s=all[5],
                               p=all[6],
                               err=err,
                               form=form)