Exemple #1
0
def create_author():
    if request.method == 'GET':
        form = AuthorForm()
    else:  # POST
        form = AuthorForm(request.form)
        if form.validate():
            author = Author('')
            form.populate_obj(author)
            db.session.add(author)
            db.session.commit()
            return redirect(url_for('edit_authors'))
    return render_template('authors_edit_page.html', form=form)
Exemple #2
0
def addauthor(request, id=1):
    if request.POST:
        form = AuthorForm(request.POST)
        if form.is_valid():
            author = form.save(commit=False)
            form.save()
    return redirect('/authors/all', )
Exemple #3
0
def author_edit_page(author_id):
    searchform = SearchForm()
    db = current_app.config["db"]
    author = db.get_author_by_id(author_id)
    if author is None:
        abort(404)
    if not current_user.is_admin:
        abort(401)
    form = AuthorForm()
    if form.validate_on_submit():
        name = form.data["name"]
        description = form.data["description"]
        photo = form.data["photo"]
        author_ = Author(id=author_id,
                         name=name,
                         description=description,
                         photo=photo)
        db.update_author(author_)
        flash("Author updated successfully.")
        return redirect(url_for("author_page", author_id=author_id))
    form.name.data = author.name
    form.description.data = author.description if author.description else ""
    form.photo.data = author.photo if author.photo else ""
    return render_template("author_edit.html",
                           form=form,
                           searchform=searchform)
Exemple #4
0
def author_create(request):
    if request.user.author_profile.count() != 0:
        return HttpResponseRedirect(
            reverse('author_detail', args=[request.user.get_profile().id]))
    if request.method == 'POST':
        form = AuthorForm(request.POST)
        if form.is_valid():
            new_author = form.save(commit=False)
            new_author.user = request.user
            new_author.save()
            return HttpResponseRedirect(
                reverse('author_detail', args=[new_author.id]))
        else:
            form = AuthorForm(request.POST)
    else:
        form = AuthorForm()
    return render_to_response('author_create.html', {'form': form},
                              context_instance=RequestContext(request))
Exemple #5
0
def authors_add():
    form = AuthorForm(request.form)

    if request.method == "POST" and form.validate():
        new_author = Author(name=form.data.get('name'))
        db.session.add(new_author)
        db.session.commit()
        flash("Author added")
        return redirect(url_for('authors_view_all'))

    return render_template('authors/add.html', **locals())
Exemple #6
0
def edit_a_book(request, book_requested):
    if request.method == 'POST':
        formA = BookForm(request.POST, prefix='formA')
        formA.is_multipart()
        formB = AuthorForm(request.POST, prefix='formB')
        # Book Title Regex used before saving to DB --------------------------
        if formA.is_valid():
            title = formA.cleaned_data['title']
            m = re.match('(\w+\s)*(\w+)', title)
            # continue normal view as before -------------------------------------
            if all([formA.is_valid(), formB.is_valid()
                    ]) and m is not None and len(str(m.group())) == len(
                        str(title)):
                orig_book = uncode_display_url(book_requested)
                book = get_object_or_404(Book, title=orig_book)
                author = Author.objects.get(book__title=orig_book)
                book.title = formA.cleaned_data['title']
                author.first_name = formB.cleaned_data['first_name']
                author.last_name = formB.cleaned_data['last_name']
                book.description = formA.cleaned_data['description']
                author.save()
                book.save()
                return HttpResponseRedirect(reverse('books:profile'))
    else:
        orig_book = uncode_display_url(book_requested)
        book = get_object_or_404(Book, title=orig_book)
        author = Author.objects.get(book__title=orig_book)
        formA = BookForm(prefix='formA',
                         initial={
                             'title': book.title,
                             'description': book.description
                         })
        formB = AuthorForm(prefix='formB',
                           initial={
                               'first_name': author.first_name,
                               'last_name': author.last_name
                           })
    return render(request, 'books/edit_a_book.html', {
        'formA': formA,
        'formB': formB
    })
Exemple #7
0
def authors_edit(author_id):
    author = Author.query.get_or_404(author_id)

    form = AuthorForm(request.form, obj=author)

    if request.method == "POST" and form.validate():
        author.name = form.data.get('name')
        db.session.commit()
        flash("Author changed")
        return redirect(url_for('authors_view_all'))

    return render_template('authors/edit.html', **locals())
Exemple #8
0
def login():
    af = AuthorForm()
    if af.validate_on_submit():
        user =  models.Author.query.filter_by(name=af.name.data, passwd=af.passwd.data).first()
        if not user:
            user = models.Author(af.name.data, af.passwd.data)
            db.session.add(user)
            db.session.commit()
        login_user(user)
        flash("User successful login")
        return redirect(request.args.get('next') or url_for('index'))
    else:
        return render_template('login.html', aform = af)
Exemple #9
0
def edit_authors(id_=None):
    if id_ is None:
        authors = Author.query.all()
        return render_template('authors_list_page.html', authors=authors)
    else:
        author = Author.query.get_or_404(id_)
        if request.method == 'GET':
            form = AuthorForm(obj=author)
        elif request.method == 'POST':
            form = AuthorForm(request.form)
            if form.validate():
                form.populate_obj(author)
                db.session.add(author)
                db.session.commit()
                return redirect(url_for('edit_authors'))
        else:  # request.method == 'DELETE'
            db.session.delete(author)
            db.session.commit()
            return '', 200
    return render_template('authors_edit_page.html',
                           form=form,
                           obj_id=author.id)
Exemple #10
0
def author_edit(request, id):
    author = get_object_or_404(Author, pk=id)
    if request.user != author.user:
        resp = render_to_response('403.html',
                                  context_instance=RequestContext(request))
        resp.status_code = 403
        return resp
    if request.method == 'POST':
        form = AuthorForm(request.POST, instance=author)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(
                reverse('author_detail', args=[author.id]))
        else:
            form = AuthorForm(request.POST, instance=author)
    else:
        form = AuthorForm(instance=author)
    return render_to_response('author_edit.html', {
        'form': form,
        'author': author
    },
                              context_instance=RequestContext(request))
Exemple #11
0
def add_a_book(request):
    if request.method == 'POST':
        formA = AddBookForm(request.POST, request.FILES, prefix='formA')
        formA.is_multipart()
        formB = AuthorForm(request.POST, prefix='formB')
        # Book Title Regex used before saving to DB --------------------------
        if formA.is_valid():
            title = formA.cleaned_data['title']
            m = re.match('(\w+\s)*(\w+)', title)
            # continue normal view as before -------------------------------------
            if all([formA.is_valid(), formB.is_valid()
                    ]) and m is not None and len(str(m.group())) == len(
                        str(title)):
                title = formA.cleaned_data['title']
                first_name = formB.cleaned_data['first_name']
                last_name = formB.cleaned_data['last_name']
                description = formA.cleaned_data['description']
                date = datetime.datetime.now()
                a_book = formA
                a_author = formB
                new_book = a_book.save(commit=False)
                new_author, created_author = Author.objects.get_or_create(
                    first_name=first_name, last_name=last_name)
                new_book.file.name = content_file_name('content',
                                                       formA['file'].name)
                new_book.user = request.user
                new_book.save()
                new_book.authors.add(new_author.id)
                new_book.save()
                return HttpResponseRedirect(reverse('books:index'))
    else:
        formA = AddBookForm(prefix='formA')
        formB = AuthorForm(prefix='formB')
    return render(request, 'books/add_a_book.html', {
        'formA': formA,
        'formB': formB
    })
Exemple #12
0
def addauthor():
    """ Добавление писателя (только админ) """
    form = AuthorForm()
    session = db_session.create_session()
    if form.validate_on_submit():
        if session:
            author = Author(name=form.name.data,
                            surname=form.surname.data,
                            years=form.years.data,
                            list_of_books=form.list_of_books.data)
            session.add(author)
            session.commit()
            return redirect("/")
        return redirect('/logout')
    return render_template('addauthor.html',
                           title='Добавление писателя',
                           form=form)
Exemple #13
0
def author_manager(request, author):
    BookInlineFormSet = inlineformset_factory(Author,
                                              Book,
                                              extra=1,
                                              formfield_callback=add_category)

    form = AuthorForm(request.POST or None, instance=author)
    formset = BookInlineFormSet(request.POST or None, instance=author)

    if form.is_valid() and formset.is_valid():
        form.save()
        formset.save()
        return HttpResponseRedirect('/inlines/')

    return render_to_response("manage_authors.html", {
        "formset": formset,
        "form": form
    }, RequestContext(request))
Exemple #14
0
def add_author(author_id):
    a = Author.query.filter(Author.id == author_id).first()
    form = AuthorForm(user=g.user, author=a)
    msg = "Success!"
    if not form.validate_on_submit():
        msg = '; '.join(form.errors.items())

    # update feeds
    for feed in Feed.query.filter(Feed.id.in_(form.feeds.data)):
        for sa_id, sa_name in form.similar_authors.choices:
            author = Author.query.filter(Author.id == int(sa_id)).first()
            if author in feed.authors:
                feed.authors.remove(author)
        for sa_id in form.similar_authors.data:
            author = Author.query.filter(Author.id == int(sa_id)).first()
            feed.authors.append(author)
        db.session.add(feed)
    db.session.commit()

    return msg
def add_author():
    if not current_user.is_admin:
        return abort(401)
    db = current_app.config["db"]
    form = AuthorForm()
    if form.validate_on_submit():
        values = author_take_info_from_form(form)

        person_id = db.person.add(*values[0])
        db.author.add(person_id, values[1])

        flash("Author is added successfully", "success")
        next_page = request.args.get("next", url_for("home_page"))
        return redirect(next_page)

    empty_person = PersonObj()
    empty_author = AuthorObj("", "", "")
    return render_template("author/author_form.html",
                           form=form,
                           person=empty_person,
                           author=empty_author)
def author_edit_page(author_id):
    if not current_user.is_admin:
        return abort(401)
    db = current_app.config["db"]
    form = AuthorForm()
    author_obj = db.author.get_row("*", "AUTHOR_ID", author_id)
    person_obj = db.person.get_row("*", "PERSON_ID", author_obj.person_id)
    if form.validate_on_submit():
        values = author_take_info_from_form(form)
        db.person.update([
            "PERSON_NAME", "SURNAME", "GENDER", "DATE_OF_BIRTH", "NATIONALITY"
        ], values[0], "PERSON_ID", author_obj.person_id)
        db.author.update("BIOGRAPHY", values[1], "AUTHOR_ID", author_id)

        flash("Author is updated successfully", "success")
        next_page = request.args.get("next", url_for("home_page"))
        return redirect(next_page)

    return render_template("author/author_form.html",
                           form=form,
                           person=person_obj,
                           author=author_obj)
def author_add():
    # form = LoginForm()
    author_form = AuthorForm(request.form)
    author_form.books.choices = [
        (p.id, p.title) for p in db_session.query(Book).order_by('id')
    ]

    if author_form.validate_on_submit():
        author = Author()
        author.name = author_form.name.data
        author.books = [
            db_session.query(Book).get(o) for o in author_form.books.data
        ]
        db_session.add(author)
        db_session.commit()
        flash('Successfully added.', 'success')
        return redirect(url_for('index'))

    return render_template("new_author.html",
                           bform=author_form,
                           user=current_user,
                           is_authenticated=True)
def author_edit(id):
    # form = LoginForm()
    author = db_session.query(Author).get(id)
    author_form = AuthorForm(request.form, obj=author)
    author_form.books.choices = [
        (p.id, p.title) for p in db_session.query(Book).order_by('id')
    ]

    if author_form.validate_on_submit():
        author = db_session.query(Author).get(id)
        author.name = author_form.name.data
        author.books = [
            db_session.query(Book).get(o) for o in author_form.books.data
        ]
        db_session.commit()
        flash('Saved.', 'info')
        return redirect(url_for('index'))

    author_form.books.data = [p.id for p in author.books]
    return render_template("author.html",
                           bform=author_form,
                           author=author,
                           user=current_user,
                           is_authenticated=True)
Exemple #19
0
def add_feed_author():
    similar_authors = None
    author = None

    if 'query' in request.values:
        similar_authors = Author.auto_complete_authors(
            request.values['query']).all()
        names = request.values['query'].split(' ')
        author = Author.query.filter(Author.lastname == names[-1]).filter(
            Author.forenames == ' '.join(names[:-1])).first()

    if 'author_id' in request.values:
        author = Author.query.filter(
            Author.id == request.values['author_id']).first()
        if author is not None:
            similar_authors = author.similar_authors()

    if 'feed_id' in request.values:
        feed_id = int(request.values['feed_id'])
    else:
        feed_id = None

    if author is None:
        author = Author.query.filter(
            Author.id == similar_authors[0].id).first()

    if similar_authors is None:
        return no_found_error("Error: author not found!")

    author_form = AuthorForm(user=g.user, author=author, feed_id=feed_id)

    return render_template('add_feed_author.html',
                           author=author,
                           similar_authors=similar_authors,
                           author_form=author_form,
                           endpoint=request.values['endpoint'])