Esempio n. 1
0
def add_book(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("/login/")
    user=request.user.username
    if request.POST:
        post = request.POST
        try:
            author = Author.objects.get(Name = post["AuthorName"])
            try:
                BOOK = Book.objects.get(ISBN = post["ISBN"])
                right="The ISBN is exists!"
                return render_to_response("add_book.html",Context({"count":Book.objects.filter().count(),"right":right}))
            except:
                new_book = Book(
                    user = request.user.username,
                    ISBN = post["ISBN"],
                    Title = post["Title"],
                    Author = author,
                    Publisher = post["Publisher"],
                    PublishDate = post["PublishDate"],
                    Price = post["Price"])     
                new_book.save()
        except:
            right="create author first"
            return render_to_response("add_book.html",Context({"count":Book.objects.filter().count(),"right":right}))
    return render_to_response("add_book.html",Context({"count":Book.objects.filter().count()}))
Esempio n. 2
0
def add_book2(request):
    word = ""
    name = request.GET["authorname"]
    if request.POST:
        post = request.POST
        post = request.POST
        isbn = post["isbn"]
        title = post["title"]
        publisher = post["publisher"]
        publishdate = post["publishdate"]
        price = post["price"]
        le = Book.objects.filter(ISBN = isbn).count()
        if le == 0:
            try:
                author = Author.objects.get(Name = name)
                new_book = Book(
                        ISBN = isbn,
                        Title = title,
                        Author = author,
                        Publisher = publisher,
                        PublishDate = publishdate,
                        Price = price
                        )
                new_book.save()
                return HttpResponseRedirect("/add_book_ok/")
            except:
                return HttpResponseRedirect("/author_not_exist/")
        else:
            word = "this book has already existed, please check the ISBN code"
    return render_to_response('add_book2.html',{"name":name, "word":word})
Esempio n. 3
0
def book_update(request):
    if request.GET:
        book_name = request.GET["u_name"]
        it = Book.objects.get(name = book_name)
        m_date = ["2015","10","30"]
        m_date= it.pub_date.split("-")
        if request.POST:
            try:
                post = request.POST
                pattern = re.compile('[\d]+\.[\d]+')
                error_1 = (post['name']=='')
                error_2 = (post['owner']=='')
                error_3 = (post['price']=='') or not pattern.match(post['price'])
                error_4 = (post['ISBN']=='')
                pub_date=post["selYear"]+'-'+post["selMonth"]+'-'+post["selDay"]
                owner = Author.objects.filter(name=post['owner'])
                try:
                    own = owner[0]
                    error_5 = False
                except:
                    error_5 = True
                error = Context({"error_1":error_1,
                                 "error_2":error_2,
                                 "error_3":error_3,
                                 "error_4":error_4,
                                 "error_5":error_5,
                                 "success":not(error_1 or error_2 or error_3 or error_4 or error_5),
                                 "it":it,"it_year":m_date[0],"it_month":m_date[1],"it_day":m_date[2],})
                if error_1 or error_2 or error_3 or error_4 or error_5:
                    return render_to_response('book_update.html',error)
                else:
                    new_book = Book(name = post['name'],
                                    owner = own,
                                    price = post['price'],
                                    pub_house = post['pub_house'],
                                    pub_date = pub_date,
                                    ISBN = post['ISBN'])
                    new_book.save()
                    it.delete()
                    return render_to_response('book_update.html',error)
            except:
                error = Context({"error_1":True,
                                 "error_2":True,
                                 "error_3":False,
                                 "error_4":False,
                                 "error_5":False,
                                 "success":False,
                                 "it":it,"it_year":m_date[0],"it_month":m_date[1],"it_day":m_date[2],})
                return render_to_response('book_update.html',error)
        error = Context({"error_1":False,
                         "error_2":False,
                         "error_3":False,
                         "error_4":False,
                         "error_5":False,
                         "success":False,
                         "it":it,"it_year":m_date[0],"it_month":m_date[1],"it_day":m_date[2],})
        return render_to_response('book_update.html',error)
        
            
Esempio n. 4
0
def newbook(post):
    """to get the room for book to store"""
    new_book = Book(isbn=post["isbn"],
                    title=post["title"],
                    publisher=post["publisher"],
                    publishdate=post["publishdate"],
                    price=post["price"])
    return new_book
Esempio n. 5
0
def book_add(request):
    if request.POST:
        try:
            post = request.POST
            pattern = re.compile('[\d]+\.[\d]+')
            error_1 = (post['name']=='')
            error_2 = (post['owner']=='')
            error_3 = (post['price']=='') or not pattern.match(post['price'])
            error_4 = (post['ISBN']=='')
            pub_date=post["selYear"]+'-'+post["selMonth"]+'-'+post["selDay"]
            owner = Author.objects.filter(name=post['owner'])
            try:
                own = owner[0]
                error_5 = False
            except:
                error_5 = True
            error = Context({"error_1":error_1,
                             "error_2":error_2,
                             "error_3":error_3,
                             "error_4":error_4,
                             "error_5":error_5,})
            if error_1 or error_2 or error_3 or error_4 or error_5:
                return render_to_response('book_add.html',error)
            else:
                new_book = Book(name = post['name'],
                                owner = own,
                                price = post['price'],
                                pub_house = post['pub_house'],
                                pub_date = pub_date,
                                ISBN = post['ISBN'])
                new_book.save()
                return render_to_response('book_add.html',error)
        except:
            error = Context({"error_1":True,
                             "error_2":True,
                             "error_3":False,
                             "error_4":False,
                             "error_5":False,})
            return render_to_response('book_add.html',error)
    error = Context({"error_1":False,
                     "error_2":False,
                     "error_3":False,
                     "error_4":False,
                     "error_5":False,})
    return render_to_response('book_add.html',error)
Esempio n. 6
0
def renew(request):
    ID=request.GET["id"]
    book=Book.objects.get(ISBN=ID)
    c={"book":book,}
    post = request.POST
    if post:
        author=Author.objects.filter(AuthorID=post["authorid"],)
        if author:
            new_book=Book(
            ISBN=post["isbn"],
            Title=post["title"],
            AuthorID=Author.objects.get(Name=post["authorid"]),
            Publisher=post["publisher"],
            PublishDate=post["publishdate"],
            Price=post["price"],)
            new_book.save()
            book.delete()
            c={"error":"success!","href":"/"}
        else:
            c={"error":"you hace to insert a new author!","href":"/add_author"}
    return render(request, 'renew.html',c,context_instance=RequestContext(request))
Esempio n. 7
0
def add(request):
    author_list = Author.objects.all()
    if request.POST:
        post = request.POST
        havenoword=True
        if("AuthorID" in request.POST):
        	if((len(post["ISBN"])!=0) and (len(post["Title"])!=0)):
                    havenoword=False
                    author=Author.objects.get(AuthorID=post["AuthorID"])
                    new_book = Book(
                            ISBN = post["ISBN"],
                            Title = post["Title"],
                            AuthorID = author,
                            Publisher = post["Publisher"],
                            PublishDate = post["PublishDate"],
                            Price = post["Price"])           
                    new_book.save()  
                    return HttpResponseRedirect("/")
        return render_to_response("add.html",{"havenoword":havenoword,"author_list":author_list})
    else:
        return render_to_response("add.html",{"author_list":author_list})
Esempio n. 8
0
def add(request):
    post=request.POST
    c={}
    if post:
        author=Author.objects.filter(AuthorID=post["authorid"],)
        book=Book.objects.filter(ISBN=post["isbn"],)
        if author and not book:
            c={"error":"",}
            author=Author.objects.get(AuthorID=post["authorid"],)
            new_book=Book(
                ISBN=post["isbn"],
                Title=post["title"],
                AuthorID=author,
                Publisher=post["publisher"],
                PublishDate=post["publishdate"],
                Price=post["price"],
              )
            new_book.save()
        elif not author:
            c={"error":"press there to insert a new author","href":"/add_author"}  
        else:
            c={"error":"this isbn is existed!","href":"#"} 
    return render(request,'add.html',c,context_instance=RequestContext(request))