def likedUsrBook(request, id): uId = request.user.id book = get_object_or_404(Books, id=id) new_book = Books(id=None, title = book.title, author= book.author, date_added = book.date_added, image=book.image,user_id = uId) new_book.save() return HttpResponseRedirect(reverse('books:results'))
def addbook(request): books = Books.objects.all() if request.user.is_staff: if request.method == "POST": name = request.POST['book'] author = request.POST['author'] copies = request.POST['copies'] status = request.POST['status'] book = Books(name=name, author=author, copies=copies, status=status) book.save() return redirect("/lend/addbook") else: return render(request, "lend/addbook.html", {'books': books})
def book_add(msg = ""): book_form = BookForm(request.form) user = auth.get_logged_in_user() if request.method == "POST": books = Books( ownership = user, title = book_form.title.data, author = book_form.author.data, isbn = book_form.isbn.data, ) books.save() flash("Book successfully saved") msg = "Book successfully saved" book_form = BookForm() return render_template("book_add.html", book_form=book_form, msg=msg) else: return render_template("book_add.html", book_form=book_form, msg=msg)
def Checkdb(dbname): prgrm = 1 #Default value for the program to run if checkdb( dbname ) is True: #Check the existance of the database (if exists or not) """ The database does not exist: we need to create it from scratch and populate it """ createdb(dbname) #Create the database session = connect(dbname) #Connect to the database createtables(dbname) #Create the tables populate(dbname, rawdata) #Populate the database finishTime = datetime.now() #End of the set up timeDetla = finishTime - startTime print('\n----------------------------------------------------') print("Setup is finished. Your database is now available.") print("The process was completed in : " + str(timeDetla.total_seconds()) + "s.") #Total time for the database's setup print('----------------------------------------------------\n') return prgrm else: print("Your database already exists.\n") print('Here are the tables and their columns that exists : ') engine = db.create_engine( f'mysql+pymysql://{username}:{password}@{host}/{dbname}') connection = engine.connect() #Connect session = connect(dbname) #Connect to the database metadata = MetaData(bind=engine) m = MetaData() m.reflect(engine) listtable = [] for table in m.tables.values(): #Create a list listtable.append(table.name) print("\nTable name: ", table.name, "\n") for column in table.c: print("Column name: ", column.name) print('\nDo you want to see what is inside a specific table ? ') print('1 = You want to see inside a table.') print('Other integer : do not show anything\n') try: choice = int(input('Your choice : ')) except ValueError: print( '\nThe input is not right. We will consider you do not want to see anything\n' ) choice = 0 if choice == 1: print( '\nYou asked to see what is inside a table. Which one do you want to see ? \n' ) try: tablechosen = choosetable() except ValueError: print('\nPlease write a valid input (string)') tablechosen = choosetable() try: existingtable = listtable.index(tablechosen) except ValueError: print("You did not entered a valid table name...") print("Please write a valid one now\n") tablechosen = choosetable() else: print( pd.DataFrame( connection.execute( "SELECT * FROM {}".format(tablechosen)))) else: pass print( '\nDo you want to add the data that does not exist (without deleting the previously existing one ?' ) print('1 = You want to add the data and run the program') print('Other integer : Do not do anything. The program will stop\n') try: Choice = int(input('Your choice : ')) except ValueError: print( "The input is not right... We will consider you do not want the program to run.\n" ) prgrm = 0 return prgrm if Choice == 1: # Create a code that add the data from the jason file # if it does not exist in the table with open('data.json') as f: data_json = json.load(f) for data in data_json: if not Books.objects.filter(Title=data['Title']).exists: data = Books(Title=data['Title'], Author=data['Author'], ReadOrNot='0') data.save() print( f"Added to the database: {data.Title} \ (TITLE {data.Title}). " ) else: print( f'This book already exists: \ {Books.objects.get(Title = data["Title"]).Title} (TITLE: {data["Title"]}).' ) return prgrm #The program will run properly else: prgrm = 0 print('The program will not run') return prgrm