Пример #1
0
 def post(self, request):
     form = AuthorForm(request.POST)
     if form.is_valid():
         form.save()
         res = {'code': 0, 'result': '添加作者成功'}
     else:
         res = {'code': 1, 'errmsg': form.errors}
     return JsonResponse(res, safe=True)
Пример #2
0
 def post(self, request, *args, **kwargs):
     pk = kwargs.get('pk')
     p = self.model.objects.get(pk=pk)
     form = AuthorForm(request.POST, instance=p)
     if form.is_valid():
         form.save()
         res = {"code": 0, "result": "success", 'next_url': self.next_url}
     else:
         res = {"code": 1, "errmsg": form.errors, 'next_url': self.next_url}
     return render(request, settings.JUMP_PAGE, res)
Пример #3
0
 def post(self, request):
     form = AuthorForm(request.POST)
     if form.is_valid():
         form.save()
         res = {'code': 0, 'result': '添加作者成功'}
     else:
         # form.errors会把验证不通过的信息以对象的形式传到前端,前端直接渲染即可
         res = {'code': 1, 'errmsg': form.errors}
         print form.errors
     return JsonResponse(res, safe=True)
Пример #4
0
def author_form_test(request):
    form = AuthorForm()
    return render(
        request,
        'accounts/login.html',
        context = {'form': form}
    )
Пример #5
0
def books_add_view(request):
    """Render form for adding books to database. Show list of added books."""
    first_author_form = AuthorForm(data=request.POST or None)
    authors_fs = AuthorFormSet(data=request.POST or None)
    book_form = BookForm(data=request.POST or None)
    language_form = LanguageForm(data=request.POST or None)

    if (first_author_form.is_valid() and language_form.is_valid()
            and book_form.is_valid() and authors_fs.is_valid()):

        first_author_name = first_author_form.cleaned_data['name']
        first_author, _ = Author.objects.get_or_create(name=first_author_name)

        more_authors = []
        for form in authors_fs:
            if form.is_valid():
                try:
                    author_name = form.cleaned_data['name']
                    author, _ = Author.objects.get_or_create(name=author_name)
                    more_authors.append(author)
                except KeyError:
                    pass

        language_code = language_form.cleaned_data['code']
        language, _ = Language.objects.get_or_create(code=language_code)

        book = book_form.save(commit=False)
        book.language = language
        book = book_form.save()
        book.authors.add(first_author)
        if more_authors:
            book.authors.add(*list(more_authors))

        date = book_form.cleaned_data['pub_date']
        book.pub_date = date
        book.save()

        messages.info(request, f'New book has been added!')
        return redirect('add')

    context = {
        'first_author_form': first_author_form,
        'authors_fs': authors_fs,
        'book_form': book_form,
        'language_form': language_form,
    }
    return render(request, 'books_add.html', context)
Пример #6
0
def new_item(request, *args, **kwargs):
    errors = []
    type = kwargs.pop('type')
    if type == 'author':
        form = AuthorForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            author = Author(first_name=cd.get('first_name'),
                            last_name=cd['last_name'],
                            email=cd['email'])
            author.save()

    elif type == 'publisher':
        form = PubForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            print cd
            pub = Publisher(name=cd['name'],
                            address=cd['address'],
                            city=cd['city'],
                            state_province=cd['state_province'],
                            country=cd['country'],
                            website=cd['website'])
            pub.save()

    elif type == 'book':
        form = BookForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            print cd
            names = cd['authors'].split(',')
            pub_name = cd['publisher']
            pub = ''
            try:
                pub = Publisher.objects.get(name=pub_name.strip())
            except Exception:
                print 'Publisher[%s] not exist.' % pub_name

            book = Book(title=cd['title'],
                        publisher=pub,
                        publication_date=cd['pub_date'])
            book.save()
            for name in names:
                try:
                    author = Author.objects.get(first_name=name.strip())
                    print author
                    book.authors.add(author)
                except Exception as e:
                    print e
                    print 'Author[%s] not exist.' % name

#                     errors.append('%s not exist.' %name)
#             book = Book(title=cd['title'], )

    return form
Пример #7
0
def addAuthor(request):
    new_author = Author()
    if request.method == 'POST':
        form = AuthorForm(request.POST)
        if form.is_valid():
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            email = form.cleaned_data['email']
            age = form.cleaned_data['age']

            new_author.first_name = first_name
            new_author.last_name = last_name
            new_author.email = email
            new_author.age = age
            new_author.save()

            #return HttpResponse("Author Added")
            #return render(request, 'auth_users.html')
            return redirect('/books/home/')
    else:
        form = AuthorForm()
    return render(request, 'author.html', {'form': form})
Пример #8
0
def add_author(request):
    if request.method == 'POST':
        form = AuthorForm(request.POST)
        if form.is_valid():
            new_author = form.save()
            return HttpResponse("{author} is added in our database!".format(author=new_author))
    else:
        form = AuthorForm()
        
    return render (
        request,
        'add-author.html',
        {'form': form}
    )
Пример #9
0
def new_item(request, *args, **kwargs):
    errors=[]
    type = kwargs.pop('type')
    if type == 'author':
        form = AuthorForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            author = Author(first_name=cd.get('first_name'), last_name=cd['last_name'], email=cd['email'])
            author.save()
            
    elif type == 'publisher':
        form = PubForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            print cd
            pub = Publisher(name=cd['name'], address=cd['address'], city=cd['city'], state_province=cd['state_province'], country=cd['country'], website=cd['website'])
            pub.save()
        
    elif type == 'book':
        form = BookForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            print cd
            names = cd['authors'].split(',')
            pub_name = cd['publisher']
            pub = ''
            try:
                pub = Publisher.objects.get(name=pub_name.strip())
            except Exception:
                print 'Publisher[%s] not exist.' %pub_name
                
            book = Book(title=cd['title'], publisher=pub, publication_date=cd['pub_date'])
            book.save()
            for name in names:
                try:
                    author = Author.objects.get(first_name=name.strip())
                    print author
                    book.authors.add(author)
                except Exception as e:
                    print e
                    print 'Author[%s] not exist.' %name
            
#                     errors.append('%s not exist.' %name)
#             book = Book(title=cd['title'], )

    return form
        
        
Пример #10
0
def newauthor(request):
    if request.POST:
        author = Author()
        aauthor = AuthorForm(request.POST, instance = author)
        
        if aauthor.is_valid():
            aauthor.save()
            return render_to_response('newauthor.html',
                    {
                        'aauthor': aauthor,
                        'msg': u'作者添加成功',
                    })
        else:
            aauthor = AuthorForm()
            return render_to_response('newauthor.html', {'aauthor': aauthor,'msg': u'作者添加失败,请检查信息是否正确'})
    else:
        aauthor = AuthorForm()
        return render_to_response('newauthor.html', {'aauthor': aauthor,})
Пример #11
0
    def test_author_form_is_invalid(self):
        form = AuthorForm(data={})

        self.assertFalse(form.is_valid())
Пример #12
0
    def test_author_form_is_valid(self):
        form = AuthorForm(data={'fullName': 'Author'})

        self.assertTrue(form.is_valid())
Пример #13
0
def author_form_test(request):
    form = AuthorForm()
    return render(request, "accounts/login.html", context={"form": form})