def delete_author_everywhere(self, author): # Deleting the author in the ComicBook model comics = ComicBook.query().fetch() aux = list() if len(comics) > 0: for comic in comics: if len(comic.authors) > 0: for comic_author in comic.authors: if author.name != comic_author.author.name: aux.append(comic_author) del comic_author comic.authors = aux comic.put() aux = list() del comic # Deleting the author in the ComicBook_Author model comic_authors = ComicBook_Author.query().fetch() if len(comic_authors) > 0: for comic_author in comic_authors: if author.name == comic_author.author.name: comic_author.key.delete() del comic_author del comics, comic_authors, aux
def get_statistics(self): statistics = dict() statistics["read"] = 0 statistics["unread"] = 0 statistics["comic"] = 0 statistics["manga"] = 0 statistics["anthology"] = 0 statistics["american"] = 0 statistics["european"] = 0 statistics["other"] = 0 statistics["value"] = 0 comics = ComicBook.query( ComicBook.users.username == self.session.get("session_name")) for comic in comics: for user_comic in comic.users: if user_comic.username == self.session.get('session_name'): # Already read or not if user_comic.state == "read": statistics["read"] += 1 else: statistics["unread"] += +1 # Comics by type if comic.type == "comic": statistics["comic"] += 1 elif comic.type == "manga": statistics["manga"] += 1 else: statistics["anthology"] += 1 # Comics by origin if comic.origin == "american": statistics["american"] += 1 elif comic.origin == "european": statistics["european"] += 1 else: statistics["other"] += 1 del user_comic statistics["value"] += comic.value del comic del comics # Delete variables to free memory return statistics
def modify_author_everywhere(self, old_name, new_name): # Modify the author in the ComicBook model comics = ComicBook.query().fetch() if len(comics) > 0: for comic in comics: if len(comic.authors) > 0: for comic_author in comic.authors: if old_name == comic_author.author.name: comic_author.author.name = new_name comic.put() del comic_author del comic # Modify the author in the ComicBook_Author model comic_authors = ComicBook_Author.query().fetch() if len(comic_authors) > 0: for comic_author in comic_authors: if old_name == comic_author.author.name: comic_author.author.name = new_name comic_author.put() del comic_author del comics, comic_authors
def post(self): jinja = jinja2.get_jinja2(app=self.app) # If an client is logged in delete the comic if self.session.get('session_role') == 'client': # Retrieving the comic key key = self.request.get("key", "") key = ndb.Key(urlsafe=key) shelving = key.get() # Get the db shelving with that key keys_page_list = self.request.get( "keys_page_list", "") # Comic keys (only comics in the current page) aux_all_keys = self.request.get( "all_keys", "") # All the comic keys (for the order field) # Initialize variables all_keys = list() # All the comic keys (for the order field) aux = list() # Support variable aux3 = list() # Support variable shelving_name = list() # Shelving name # Transform the HTML string in a list aux_all_keys = self.transform_keys(aux_all_keys) # Get the shelvings shelvings = Shelving.query( Shelving.username == self.session.get('session_name')).order( Shelving.name).fetch() all_comics = ComicBook.query().fetch() # Get all comics that belongs to the current user if not self.session.get("shelving"): comics = ComicBook.query( ComicBook.users.username == self.session.get('session_name')).order( ComicBook.users.addition_date ) # All comics ordered by the addition date all_comics_user = copy.copy( comics) # ALL comics (for the search field) all_comics_user = all_comics_user.fetch() else: key = ndb.Key(urlsafe=self.session.get("shelving")) comics = ComicBook.query( ComicBook.users.username == self.session.get( 'session_name'), ComicBook.users.shelving == key).order( ComicBook.users.addition_date ) # Comics in the shelving ordered by the addition date shelving2 = key.get() shelving_name = shelving2.name del shelving2 all_comics_user = ComicBook.query( ComicBook.users.username == self.session.get( 'session_name')).fetch( ) # ALL comics (for the search field) for comic2 in comics: # Get ALL the keys aux.append(comic2.key.urlsafe()) del comic2 for key3 in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key3 == str(key2): key2 = ndb.Key(urlsafe=key2) aux3.append(key2) all_keys.append(key2.urlsafe()) break del key2 del key3 # Get db comics offset = (self.session.get('current_number_page') - 1) * self.session.get('num_elems_page') if len(aux3) > 0: comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # Get the comics (if --> default, else --> see shelving) if not self.session.get('shelving'): comics = self.get_comics_read_and_without_shelving( comics ) # Get read comics and the ones that aren't in a shelving else: self.get_comics_read(comics) # Get read comics else: comiss = list() # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get('current_number_page'), # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comic keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics": all_comics, # ALL comic (for the user search field) "all_comics_user": all_comics_user, # All user comics (for the search field) "authors": self.get_authors(), # Authors for adding them to the comics "shelvings": shelvings, # Shelvings list "shelving_name": shelving_name # Shelving name } # If the comic exists if shelving and shelving is not None: # Delete all shelves of the shelving and quit all comics self.delete_shelving(shelving, all_comics_user) # Delete db comic shelving.key.delete() values["ok_message"] = lang[ "shelving_deleted_successfully"] # Ok message (shelving deleted successfully) # Get the shelvings shelvings = Shelving.query( Shelving.username == self.session.get( 'session_name')).order(Shelving.name).fetch() values["shelvings"] = shelvings # Setting pagination ############################################################################################## pages_to_show = self.session.get( 'pages_to_show' ) # Number of pages that shows the pagination pages = list( ) # Pages for the pagination (it's a list because of django for templates) # Get all db comics (Limited to the number given by the session variable [10 by default]) num_total = len(aux3) # Total number of elements if len(aux3) > 0: if not self.session.get("shelving"): comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # List not empty else: comics = ComicBook.query( ComicBook.users.username == self.session.get( "session_name")) # List not empty num_total = len(aux3) aux3 = list() for comic in comics: aux3.append(comic.key) del comic comics = comics.fetch( self.session.get('num_elems_page')) else: if not self.session.get("shelving"): comics = list() else: comics = ComicBook.query( ComicBook.users.username == self.session.get( "session_name")) # List not empty num_total = len(aux3) aux3 = list() for comic in comics: aux3.append(comic.key) del comic comics = comics.fetch( self.session.get('num_elems_page')) # If the total number of elements is above 0 do the pagination if num_total > 0 and len(comics) > 0: # Get the number of pages for the pagination num_elems_page = self.session.get( 'num_elems_page' ) # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages # If there are less elements than session["num_elems_page"] if num_pages == 0: num_pages = 1 # At least one page # Else else: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page self.session['last_page'] = num_pages # Last page # Set the pages for the pagination for i in range(1, num_pages + 1): pages.append(i) # Set number of pages for the pagination (pagination only shows 3 pages) current_number_page = self.session.get( 'current_number_page') # If the current page is 1 if current_number_page == 1: pages = pages[0:pages_to_show] # Shows pages 1,2,3 # If the current page is the final page elif current_number_page == num_pages: # Shows the last three pages if (num_pages - pages_to_show) >= 0: pages = pages[(num_pages - pages_to_show):] else: pages = pages[0:pages_to_show] # Else else: pages = pages[(current_number_page - 2):( current_number_page + 1)] # Show the current, last and next pages self.session['pages'] = pages # Pages for the pagination del num_elems_page, num_pages, current_number_page # Delete variables to free memory # If page is empty do the same, but return to the to the last page next to the current page elif num_total > 0 and len(comics) == 0: # Get all db comics (Limited to the number given by the session variable [10 by default]) offset = (self.session.get('current_number_page') - 2) * self.session.get('num_elems_page') comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # Get the number of pages for the pagination num_elems_page = self.session.get( 'num_elems_page' ) # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages # If there are less elements than session["num_elems_page"] if num_pages == 0: num_pages = 1 # At least one page # Else else: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page current_number_page = num_pages # The previous last page doesn't already exist, so it has to be the last page of the new list self.session['last_page'] = num_pages # Last page # Set the pages for the pagination for i in range(1, num_pages + 1): pages.append(i) # Set number of pages for the pagination (pagination only shows 3 pages) # If the current page is 1 if current_number_page == 1: pages = pages[0:pages_to_show] # Shows pages 1,2,3 # If the current page is the final page elif current_number_page == num_pages: # Shows the last three pages if (num_pages - pages_to_show) >= 0: pages = pages[(num_pages - pages_to_show):] else: pages = pages[0:pages_to_show] # Else else: pages = pages[(current_number_page - 2):( current_number_page + 1)] # Show the current, last and next pages self.session['pages'] = pages # Pages for the pagination self.session[ 'current_number_page'] = current_number_page # Current number page del num_elems_page, num_pages, current_number_page # Delete variables to free memory ########################################################################################### # Variables to be sent to the html if not self.session.get("shelving"): values[ "comics"] = self.get_comics_read_and_without_shelving( comics) else: self.session["shelving"] = None values["shelving_name"] = list() values[ "comics"] = self.get_comics_read_and_without_shelving( comics) values["all_comics_user"] = copy.copy(comics) values["pages"] = self.session.get( 'pages') # Pages for the pagination values["last_page"] = self.session.get( 'last_page') # Last page number values["all_keys"] = all_keys # All comic keys values["current_number_page"] = self.session.get( 'current_number_page') # Current number page # Setting the comic keys list that are currently in the page keys_page_list = list() for comic in comics: keys_page_list.append(comic.key.urlsafe()) values["keys_page_list"] = keys_page_list del pages_to_show, pages, num_total # Delete variables to free memory # Else show an error message else: values["error_message"] = lang[ "shelving_not_deleted"] # Error message (Shelving couldn't be deleted) del key, shelving, keys_page_list, aux_all_keys, all_keys, aux, aux3, all_comics, offset, comics,\ lang, shelving_name # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/library/default.html", **values)) # Go to the comics home page # Else redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the admin is logged in if self.session.get('session_role') == 'client': # If it's logged in, get the session variables, show the home page # Get the shelving attributes name = self.request.get("name", "") # Shelving name name = name.encode("utf8") picture = self.request.get("picture", "") # Shelving picture keys_page_list = self.request.get("keys_page_list", "") # Comic keys (only comics in the current page) aux_all_keys = self.request.get("all_keys", "") # All the comic keys (for the order field) # Initialize variables all_keys = list() # All the comic keys (for the order field) aux = list() # Support list aux3 = list() # Support list shelving_name = list() # Shelving name all_comics = ComicBook.query().fetch() # Get the shelvings shelvings = Shelving.query(Shelving.username == self.session.get('session_name')).order(Shelving.name).fetch() offset = (self.session.get('current_number_page') - 1) * self.session.get('num_elems_page') # Number of elements to ignore in the query # Transform the HTML string in a list aux_all_keys = self.transform_keys(aux_all_keys) # Get all comics that belongs to the current user if not self.session.get("shelving"): comics = ComicBook.query(ComicBook.users.username == self.session.get('session_name')).order(ComicBook.users.addition_date) # All comics ordered by the addition date all_comics_user = copy.copy(comics) # ALL comics (for the search field) all_comics_user = all_comics_user.fetch() else: key = ndb.Key(urlsafe=self.session.get("shelving")) comics = ComicBook.query(ComicBook.users.username == self.session.get('session_name'), ComicBook.users.shelving == key).order(ComicBook.users.addition_date) # Comics in the shelving ordered by the addition date shelving = key.get() shelving_name = shelving.name del shelving all_comics_user = ComicBook.query(ComicBook.users.username == self.session.get('session_name')).fetch() # ALL comics (for the search field) for comic in comics: # Get ALL the keys aux.append(comic.key.urlsafe()) del comic for key in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key == str(key2): key2 = ndb.Key(urlsafe=key) aux3.append(key2) all_keys.append(key2.urlsafe()) break del key2 del key # Get db comics if len(aux3) > 0: comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch(self.session.get('num_elems_page'), offset=offset) # Get the comics (if --> default, else --> see shelving) if not self.session.get('shelving'): comics = self.get_comics_read_and_without_shelving(comics) # Get read comics and the ones that aren't in a shelving else: self.get_comics_read(comics) # Get read comics else: comics = list() # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english keys_page_list = list() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image(self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get('current_number_page'), # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comic keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics": all_comics, # ALL comic (for the user search field) "all_comics_user": all_comics_user, # All user comics (for the search field) "authors": self.get_authors(), # Authors for adding them to the comics "shelvings": shelvings, # Shelvings list "shelving_name": shelving_name # Shelving name } # If the user enters all the attributes (the database can have more than one shelving with the same name) if len(name) > 0: # Check the comic attributes if picture != "": picture = images.resize(picture, 300, 250) else: picture = None # Add the new shelving to the home page shelving = Shelving(name=name, picture=picture, username=self.session.get('session_name')) aux2 = shelving.put() time.sleep(1) # If the shelving was successfully added if aux2 is not None: # Adding the new shelving to the list shelvings.append(shelving) # Setting pagination ############################################################################################## pages_to_show = self.session.get('pages_to_show') # Number of pages that shows the pagination pages = list() # Pages for the pagination (it's a list because of django for templates) # Get all db comics (Limited to the number given by the session variable [10 by default]) if len(aux3) > 0: comics = ComicBook.query(ComicBook.key.IN(aux3)).order(-ComicBook.save_date).fetch(self.session.get('num_elems_page'), offset=offset) else: comics = ComicBook.query().fetch() # Setting the new keys list for the comics currently in the page (allows to use the "Delete page" button) for comic in comics: keys_page_list.append(comic.key.urlsafe()) del comic num_total = len(all_comics_user) # Total number of elements # If the total number of elements is above 0 do the pagination if num_total > 0: # Get the number of pages for the pagination num_elems_page = self.session.get('num_elems_page') # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages # If there are less elements than session["num_elems_page"] if num_pages == 0: num_pages = 1 # At least one page # Else else: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page) # It doesn't need other page self.session['last_page'] = num_pages # Last page # Set the pages for the pagination for i in range(1, num_pages + 1): pages.append(i) # Set number of pages for the pagination (pagination only shows 3 pages) current_number_page = self.session.get('current_number_page') # If the current page is 1 if current_number_page == 1: pages = pages[0:pages_to_show] # Shows pages 1,2,3 # If the current page is the final page elif current_number_page == num_pages: # Shows the last three pages if (num_pages - pages_to_show) >= 0: pages = pages[(num_pages - pages_to_show):] else: pages = pages[0:pages_to_show] # Else else: pages = pages[(current_number_page - 2):(current_number_page + 1)] # Show the current, last and next pages self.session['pages'] = pages # Pages for the pagination # Variables to be sent to the HTML page # Get the comics (if --> default, else --> see shelving) if not self.session.get('shelving'): values["comics"] = self.get_comics_read_and_without_shelving(comics) # Get read comics and the ones that aren't in a shelving else: self.get_comics_read(comics) # Get read comics values["comics"] = comics values["pages"] = self.session.get('pages') # Pages for the pagination values["last_page"] = self.session.get('last_page') # Last page number del num_elems_page, num_pages, current_number_page # Delete variables to free memory ########################################################################################### # Variables to be sent to the HTML page values["ok_message"] = lang["shelving_added_successfully"] # Ok message (shelving added successfully) values["keys_page_list"] = keys_page_list # Comics keys that are currently in the page values["all_keys"] = all_keys # All comic keys values["shelvings"] = shelvings # Shelvings list del pages_to_show, pages, comics, num_total, shelving # Delete variables to free memory # If wasn't successfully added else: del shelving, aux2 # Delete variables to free memory # Variables to be sent to the HTML page values["error_message"] = lang["error_add"] # Error message (The addition failed) # Else show an error message else: # Values to be sent to the HTML page values["error_message"] = lang["must_insert_all_shelving_attributes"] # Error message (You must enter all shelving data) del lang, offset, keys_page_list, aux_all_keys, all_keys, all_comics, aux, aux3, picture,\ name, shelvings, all_comics_user, shelving_name # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write(jinja.render_template("/library/default.html", **values)) # Go to the comicss home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def get(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in. If it's logged in, show the home page if self.session.get('session_role') == 'client': user = User.query(User.name == self.session.get( "session_name")).fetch() # Current user # If users exists, get friends and friends requests if user and len(user) > 0: self.session['page_name'] = "/friends" # Current page name user = user[0] # Get db friends and friend requests all = Friend.query().order(-Friend.addition_date) # Db friends friends = copy.copy(all) friends = friends.filter(Friend.is_friend == True) friends = friends.filter( ndb.OR(Friend.who_ask == user.key, Friend.who_answer == user.key)) friends = friends.order(-Friend.addition_date) aux = list() for friend in friends: if friend.who_answer != user.key: aux.append(friend.who_answer.get()) elif friend.who_ask != user.key: aux.append(friend.who_ask.get()) del friend friends = aux # Db friends requests requests = copy.copy(all) requests = requests.filter(Friend.is_friend == False) requests = requests.filter(Friend.who_answer == user.key) requests = requests.order(-Friend.addition_date) aux = list() for request in requests: aux.append(request.who_ask.get()) del request requests = aux # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english all_comics = ComicBook.query() comics = copy.copy(all_comics) comics = comics.filter(ComicBook.users.username == self. session.get("session_name")).fetch() all_comics = all_comics.fetch() all_users = User.query(User.role == "client").fetch() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "friends": friends, # User friends "requests": requests, # User friend requests "all_comics": all_comics, # ALL comic (for the users search field) "all_users": all_users, # ALL users (for the search field) "comics": comics, # Current user comics (for the borrowinngs) } del lang, user, all, friends, requests, aux, all_comics, all_users self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/friends/default.html", **values)) # Go to the friends home page # Else redirect to the login page else: del user # Delete variables to free memory self.redirect("/login") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the admin is logged in if self.session.get('session_role') == 'admin': # If it's logged in, get the session variables, show the home page # Get the comic attributes author_key = self.request.get("author", "") # Author key author_key = ndb.Key(urlsafe=author_key) role = self.request.get_all("role[]") # Author roles key = self.request.get("key", "") key = ndb.Key(urlsafe=key) comic = key.get() # Get the comic with that key keys_page_list = self.request.get( "keys_page_list", "") # Comic keys (only comics in the current page) aux_all_keys = self.request.get( "all_keys", "") # All the comic keys (for the order field) # Initialize variables aux = list() # Support variable aux3 = list() # Support variable all_comics = list() # List with all comics (for the search field) # Transform the HTML string in a list all_keys = copy.copy(aux_all_keys) aux_all_keys = self.transform_keys(aux_all_keys) comics = ComicBook.query() for comic2 in comics: # Get ALL the keys aux.append(comic2.key.urlsafe()) del comic2 for key3 in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key3 == str(key2): key2 = ndb.Key(urlsafe=key2) aux3.append(key2) break del key2 del key3 # Get all db comics offset = (self.session.get('current_number_page') - 1) * self.session.get('num_elems_page') comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get('current_number_page'), # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics": all_comics, # ALL comic (for the search field) "authors": self.get_authors() # Authors for adding them to the comics } # If the key is from an comic if comic and comic is not None and len( role) > 0 and author_key is not None: # Adding the author roles = "" if str(role).find("all") == -1: for i in range(0, len(role)): roles += lang[role[i]] + ", " roles = roles[:len(roles) - 2] roles = roles.capitalize() else: roles = lang["script"] + ", " + lang[ "drawing"] + ", " + lang["labels"] + ", " + lang[ "inked"] + ", " + lang["colors"] + ", " + lang[ "cover"] roles = roles.capitalize() id = ComicBook_Author.query().order( -ComicBook_Author.id_aux).fetch(1) if len(id) > 0: id = id[0].id_aux + 1 else: id = 1 comic_author = ComicBook_Author(author=author_key.get(), role=roles, id_aux=id) comic_author.put() if len(comic.authors) > 0: comic.authors.append(comic_author) else: comic.authors = [comic_author] aux2 = comic.put() time.sleep(1) # If the modification was successful if aux2 is not None: # Variables to be sent to the HTML page values["ok_message"] = lang[ "author_added_successfully"] # Ok message (Author added successfully) # Get all db Comics (Limited to the number given by the session variable [10 by default]) comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) values["comics"] = comics # Else show an error message else: # Variables to be sent to the HTML page values["error_message"] = lang[ "error_add"] # Error message (The addition couldn't be done) del aux2, comic_author, roles, id # Delete variables to free memory # Else show an error message else: # Values to be sent to the HTML page values["error_message"] = lang[ "must_insert_all_author_attributes"] # Error message (You must insert all author attributes) all_comics = ComicBook.query().fetch( ) # ALL comics (for the search field) values["all_comics"] = all_comics del lang, key, comic, keys_page_list, aux_all_keys, aux, aux3, \ all_comics, offset, all_keys, author_key, role, comics # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/comics/default.html", **values)) # Go to the comics home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def get(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the admin is logged in. If it's logged in, show the home page if self.session.get('session_role') == 'admin': # Initialize variables pages_to_show = self.session.get( 'pages_to_show') # Number of pages that shows the pagination pages = list( ) # Pages for the pagination (it's a list because of django for templates) self.session[ 'current_number_page'] = 1 # Session current number page all_keys = list() # All comic keys keys_page_list = list() # Comic keys currently in the page # Get all db comics (Limited to the number given by the session variable [10 by default]) comics = ComicBook.query().order( -ComicBook.save_date) # All comics ordered by save date all_comics = copy.copy(comics) # ALL comics (for the search field) all_comics = all_comics.fetch() num_total = len(all_comics) # Total number of elements self.session['page_name'] = "/comics" # Current page name self.session['num_elems_page'] = 10 # Amount of elements # Get ALL the comic keys (they are necessary to do the order and filter) for comic in comics: all_keys.append(comic.key.urlsafe()) del comic # If the total number of elements is above 0 do the pagination if num_total > 0: # Get the number of pages for the pagination num_elems_page = self.session.get( 'num_elems_page') # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages comics = comics.fetch( self.session.get('num_elems_page')) # Ordered by name # If there are less elements than session["num_elems_page"] if num_pages == 0: num_pages = 1 # At least one page # Else else: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page self.session['last_page'] = num_pages # Last page # Set the pages for the pagination for i in range(1, num_pages + 1): pages.append(i) # Set number of pages for the pagination (pagination only shows 3 pages) current_number_page = 1 # If the current page is 1 if current_number_page == 1: pages = pages[0:pages_to_show] # Shows pages 1,2,3 # If the current page is the final page elif current_number_page == num_pages: # Shows the last three pages if (num_pages - pages_to_show) >= 0: pages = pages[(num_pages - pages_to_show):] else: pages = pages[0:pages_to_show] # Else else: pages = pages[(current_number_page - 1):( current_number_page + 1)] # Show the current, last and next pages self.session['pages'] = pages # Pages for the pagination # Setting the keys list to allow the use of the "Delete page" button for comic in comics: keys_page_list.append(comic.key.urlsafe()) del comic del num_elems_page, num_pages, current_number_page, pages # Delete variables to free memory # If num_total < 0 else: comics = all_comics # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": 1, # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics": all_comics, # ALL comics (for the search field) "authors": self.get_authors() # Authors for adding them to the comics } del num_total, pages_to_show, keys_page_list, all_keys, lang, comics, all_comics # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/comics/default.html", **values)) # Go to the comics home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the admin is logged in if self.session.get('session_role') == 'admin': # Initialize variables all_keys = list() # New comic keys list comics = None # Comics list all_comics = list() # ALL comics (for the search field) pages = list() # Pages for the pagination # If it's logged in, get the session variables, show the home page # Get the order attributes order_by = self.request.get("order_by", "") # Sort option order_by = order_by.encode("utf8") aux_all_keys = self.request.get("all_keys", "") # All comic keys list if len(aux_all_keys) > 2: # Transform the HTML string in a list aux_all_keys = self.transform_keys(aux_all_keys) # It's necessary to compare the HTML with the query list in order to obtain the desired list, like I said comics = ComicBook.query() all_comics = copy.copy( comics) # ALL comics (for the search field) all_comics = all_comics.fetch() aux = list() for comic in comics: # Get ALL the keys aux.append(comic.key.urlsafe()) del comic for key in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key == str(key2): key2 = ndb.Key(urlsafe=key) all_keys.append(key2) break del key2 del key del aux # Delete variables to free memory # Get all db comics (Limited to the number given by the session variable [10 by default]) # Not ordered because it has to respect if the comic used some ordination or filter comics = ComicBook.query(ComicBook.key.IN(all_keys)) # If there is a list of comics and a sort option if order_by != "" and comics is not None: # Get the number of pages for the pagination num_total = len(all_keys) num_elems_page = self.session.get( 'num_elems_page') # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages if num_pages > 0: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page # Set the pages for the pagination if num_pages >= self.session.get('pages_to_show'): for i in range(1, self.session.get('pages_to_show') + 1): pages.append(i) else: for i in range(1, num_pages + 1): pages.append(i) # Order the comic list if order_by == "title": comics = comics.order(ComicBook.title) # Title A-Z # Setting the all comic keys list all_keys = list() for comic in comics: all_keys.append(comic.key.urlsafe()) del comic comics = comics.fetch(self.session.get('num_elems_page')) elif order_by == "-title": comics = comics.order(-ComicBook.title) # Title Z-A # Setting the all comic keys list all_keys = list() for comic in comics: all_keys.append(comic.key.urlsafe()) del comic comics = comics.fetch(self.session.get('num_elems_page')) elif order_by == "save_date": comics = comics.order(ComicBook.save_date) # Older first # Setting the all comic keys list all_keys = list() for comic in comics: all_keys.append(comic.key.urlsafe()) del comic comics = comics.fetch(self.session.get('num_elems_page')) elif order_by == "-save_date": comics = comics.order(-ComicBook.save_date) # Newest first # Setting the all comic keys list all_keys = list() for comic in comics: all_keys.append(comic.key.urlsafe()) del comic comics = comics.fetch(self.session.get('num_elems_page')) elif order_by == "value": comics = comics.order(ComicBook.value) # Value ascendant # Setting the all comic keys list all_keys = list() for comic in comics: all_keys.append(comic.key.urlsafe()) del comic comics = comics.fetch(self.session.get('num_elems_page')) elif order_by == "-value": comics = comics.order(-ComicBook.value) # Value descendant # Setting the all comic keys list all_keys = list() for comic in comics: all_keys.append(comic.key.urlsafe()) del comic comics = comics.fetch(self.session.get('num_elems_page')) else: comics = comics.order(-ComicBook.save_date) # Newest first # Setting the all comic keys list all_keys = list() for comic in comics: all_keys.append(comic.key.urlsafe()) del comic comics = comics.fetch( self.session.get('num_elems_page')) # Name ascendant # Setting the comic keys list (only on the current page) keys_page_list = list() for comic in comics: keys_page_list.append(comic.key.urlsafe()) del comic # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": 1, # Current number page "pages": pages, # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics": all_comics, # ALL comic (for the search field) "authors": self.get_authors() # Authors for adding them to the comics } del order_by, aux_all_keys, all_keys, comics, lang, keys_page_list, pages,\ num_total, num_pages, num_elems_page, all_comics # Delete variables to free memory self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/comics/default.html", **values)) # Go to the comics home page # Else redirect to the comics home page else: del order_by, aux_all_keys, all_keys, comics # Delete variables to free memory self.redirect("/comics") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the admin is logged in if self.session.get('session_role') == 'admin': # If it's logged in, get the session variables, show the home page # Get the comic attributes isbn = self.request.get("isbn", "") # Comic ISBN isbn = isbn.encode("utf8") title = self.request.get("title", "") # Comic title title = title.encode("utf8") publisher = self.request.get("publisher", "") # Comic publisher publisher = publisher.encode("utf8") edition = self.request.get("edition", "") # Comic edition edition = edition.encode("utf8") if edition == "": edition = 1 else: edition = int(edition) edition = int(edition) plot = self.request.get("plot", "") # Comic plot plot = plot.encode("utf8") type = self.request.get("type", "comic") # Comic type type = type.encode("utf8") origin = self.request.get("origin", "other") # Comic origin origin = origin.encode("utf8") value = self.request.get("value", "") # Comic value if value == "": value = 0 else: value = float(value) value = float(value) cover = self.request.get("cover", "") # Comic picture save_date = self.request.get("save_date", "") # Comic save date save_date = save_date.encode("utf8") keys_page_list = self.request.get( "keys_page_list", "") # Comic keys (only comics in the current page) aux_all_keys = self.request.get( "all_keys", "") # All the comic keys (for the order field) # Initialize variables all_keys = list() # All the comic keys (for the order field) all_comics = list() # All comics (for the search field) aux = list() # Support list aux3 = list() # Support list offset = ( self.session.get('current_number_page') - 1 ) * self.session.get( 'num_elems_page') # Number of elements to ignore in the query # If the current page isn't empty if len(keys_page_list) > 2: # Transform the HTML string in a list aux_all_keys = self.transform_keys(aux_all_keys) comics = ComicBook.query() all_comics = copy.copy( comics) # ALL comics (for the search field) all_comics = all_comics.fetch() for comic in comics: # Get ALL the keys aux.append(comic.key.urlsafe()) del comic for key in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key == str(key2): key2 = ndb.Key(urlsafe=key) aux3.append(key2) all_keys.append(key2.urlsafe()) break del key2 del key # Get db comics comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # If empty else: comics = list() # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english keys_page_list = list() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get('current_number_page'), # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comic keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics": all_comics, # ALL comic (for the search field) "authors": self.get_authors() # Authors for adding them to the comics } # If the user enters all the attributes (the database can have more than one comic with the same name) if len(title) > 0 and len(type) > 0: # Check the comic attributes if len(isbn) > 0: # Create the ISBN string isbn = isbn[0:3] + "-" + isbn[3] + "-" + isbn[ 4:6] + "-" + isbn[6:12] + "-" + isbn[12] if type != "comic" or type != "manga" or type != "anthology": type = "comic" if edition <= 0: edition = 1 if origin != "american" or origin != "european" or type != "other": origin = "other" if cover != "": cover = images.resize(cover, 300, 250) else: cover = None if value < 0: value = 0 # Add the new comic to the home page comic = None if save_date != "": save_date = datetime.strptime(save_date, '%Y-%m-%d') comic = ComicBook(isbn=isbn, title=title, publisher=publisher, edition=edition, plot=plot, type=type, origin=origin, value=value, cover=cover, save_date=save_date) else: comic = ComicBook(isbn=isbn, title=title, publisher=publisher, edition=edition, plot=plot, type=type, origin=origin, value=value, cover=cover) aux2 = comic.put() time.sleep(1) # If the comic was successfully added if aux2 is not None: # Adding the new comic and its key to the lists aux3.append(comic.key) all_keys.append(comic.key.urlsafe()) all_comics.append(comic) # Setting pagination ############################################################################################## pages_to_show = self.session.get( 'pages_to_show' ) # Number of pages that shows the pagination pages = list( ) # Pages for the pagination (it's a list because of django for templates) # Get all db comics (Limited to the number given by the session variable [10 by default]) comics = ComicBook.query(ComicBook.key.IN(aux3)).order( -ComicBook.save_date).fetch( self.session.get('num_elems_page'), offset=offset) # Setting the new keys list for the comics currently in the page (allows to use the "Delete page" button) for comic in comics: keys_page_list.append(comic.key.urlsafe()) del comic num_total = len(all_comics) # Total number of elements # If the total number of elements is above 0 do the pagination if num_total > 0: # Get the number of pages for the pagination num_elems_page = self.session.get( 'num_elems_page' ) # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages # If there are less elements than session["num_elems_page"] if num_pages == 0: num_pages = 1 # At least one page # Else else: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page self.session['last_page'] = num_pages # Last page # Set the pages for the pagination for i in range(1, num_pages + 1): pages.append(i) # Set number of pages for the pagination (pagination only shows 3 pages) current_number_page = self.session.get( 'current_number_page') # If the current page is 1 if current_number_page == 1: pages = pages[0:pages_to_show] # Shows pages 1,2,3 # If the current page is the final page elif current_number_page == num_pages: # Shows the last three pages if (num_pages - pages_to_show) >= 0: pages = pages[(num_pages - pages_to_show):] else: pages = pages[0:pages_to_show] # Else else: pages = pages[(current_number_page - 2):( current_number_page + 1)] # Show the current, last and next pages self.session[ 'pages'] = pages # Pages for the pagination # Variables to be sent to the HTML page values["comics"] = comics # Comics values["pages"] = self.session.get( 'pages') # Pages for the pagination values["last_page"] = self.session.get( 'last_page') # Last page number del num_elems_page, num_pages, current_number_page # Delete variables to free memory ########################################################################################### # Variables to be sent to the HTML page values["ok_message"] = lang[ "comic_added_successfully"] # Ok message (comic added successfully) values[ "keys_page_list"] = keys_page_list # Comics keys that are currently in the page values["all_keys"] = all_keys # All comic keys values[ "all_comics"] = all_comics # ALL comic (for the search field) del pages_to_show, pages, comics, num_total # Delete variables to free memory # If wasn't successfully added else: del comic, aux2 # Delete variables to free memory # Variables to be sent to the HTML page values["error_message"] = lang[ "error_add"] # Error message (The addition failed) # Else show an error message else: # Values to be sent to the HTML page values["error_message"] = lang[ "must_insert_all_comic_attributes"] # Error message (You must enter all comic data) del lang, offset, keys_page_list, aux_all_keys, all_keys, all_comics, aux, aux3, title, \ isbn, edition, publisher, type, origin, cover # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/comics/default.html", **values)) # Go to the comicss home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the admin is logged in if self.session.get('session_role') == 'admin' or self.session.get( 'session_role') == 'client': # If it's logged in, get the session variables # Get the user attributes username = self.request.get("username", "") # Username username = username.encode("utf8") key = self.request.get("key", "") # User key key = ndb.Key(urlsafe=key) user = key.get() # Get the user with that key # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "user_email": user.email, # User email "user_key": user.key.urlsafe(), # User key "user_genre": self.session.get('session_genre'), # User genre "statistics": self.get_statistics() # Client statistics } # If the user exists and there is an username to change if user is not None and len(username) > 0: repeated_user = User.query(User.name == username) # If it doesn't exist an user with the name provided if repeated_user.count() == 0: # Set the new username username_aux = copy.copy(user.name) user.name = username aux = user.put() time.sleep(1) # If the modification was successful if aux is not None: # Variables to be sent to the HTML page # Ok message (Username modified successfully) values["ok_message"] = lang[ "username_modified_successfully_" + str(self.session.get("session_genre"))] # Change the username in the comics comics = ComicBook.query( ComicBook.users.username == username_aux) for comic in comics: for user_comic in comic.users: if user_comic.username == username_aux: user_comic.username = user.name del user_comic comic.put() del comic # Change the username in the User_ComicBook rows user_comics = User_ComicBook.query( User_ComicBook.username == username_aux) for user_comic in user_comics: if user_comic.username == username_aux: user_comic.username = user.name user_comic.put() del user_comic # Change the username in the shelves and shelvings shelvings = Shelving.query( Shelving.username == username_aux) for shelving in shelvings: shelving.username = user.name shelving.put() del shelving del user_comics, shelvings # Delete variables to free memory self.session[ 'session_name'] = user.name # Setting the new session username values["session_name"] = user.name # Else show an error message else: # Variables to be sent to the HTML page values["error_message"] = lang[ "error_modify"] # Error message (The modification couldn't be done) del aux, username_aux # Delete variables to free memory # If already exists show an error message else: # Values to be sent to the HTML page values["error_message"] = lang[ "username_already_exists"] # Error message (Username already used by other) del repeated_user # Delete variables to free memory # Else show an error message else: # Values to be sent to the HTML page # Error message (Username couldn't be modified) values["error_message"] = lang[ "username_not_modified_" + str(self.session.get("session_genre"))] del lang, username, key, user # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/profile/default.html", **values)) # Go to the profile home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in. If it's logged in, show the home page if self.session.get('session_role') == 'client': user = User.query(User.name == self.session.get("session_name")).fetch() # Current user # Get the friend id who_ask = self.request.get("who_ask", "") # If users exists, get friends and friends requests if user and len(user) > 0: user = user[0] # Get db friends and friend requests all = Friend.query().order(-Friend.addition_date) # Db friends friends = copy.copy(all) friends = friends.filter(Friend.is_friend == True) friends = friends.filter(ndb.OR(Friend.who_ask == user.key, Friend.who_answer == user.key)) friends = friends.order(-Friend.addition_date) aux = list() for friend in friends: if friend.who_answer != user.key: aux.append(friend.who_answer.get()) elif friend.who_ask != user.key: aux.append(friend.who_ask.get()) del friend friends = aux # Db friends requests requests = copy.copy(all) requests = requests.filter(Friend.is_friend == False) requests = requests.filter(Friend.who_answer == user.key) requests = requests.order(-Friend.addition_date) aux = list() for request in requests: aux.append(request.who_ask.get()) del request requests = aux # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english all_comics = ComicBook.query() comics = copy.copy(all_comics) comics = comics.filter(ComicBook.users.username == self.session.get("session_name")).fetch() all_comics = all_comics.fetch() all_users = User.query(User.role == "client").fetch() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image(self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "friends": friends, # User friends "requests": requests, # User friend requests "all_comics": all_comics, # ALL comic (for the users search field) "all_users": all_users, # ALL users (for the search field) "comics": comics, # Current user comics (for the borrowinngs) } # If the key is valid if len(who_ask) > 0: who_ask = ndb.Key(urlsafe=who_ask) # Friend who's going to be deleted who_ask = who_ask.get() # If user exists if who_ask != None: # Delete the friend aux = True requests2 = copy.copy(all) requests2 = requests2.filter(ndb.OR(Friend.who_answer == user.key, Friend.who_ask == user.key)) for request in requests2: if request.who_ask == who_ask.key and request.who_answer == user.key and request.is_friend == True: request.key.delete() aux = False elif request.who_ask == who_ask.key and request.who_answer == user.key and request.is_friend == False: request.key.delete() if request.who_ask == user.key and request.who_answer == who_ask.key and request.is_friend == True: request.key.delete() aux = False elif request.who_ask == user.key and request.who_answer == who_ask.key and request.is_friend == False: request.key.delete() del request if who_ask in requests: requests.remove(who_ask) # Remove user from the request list if who_ask in friends: friends.remove(who_ask) # Add user to the friend list # Values to be sent to the HTML page values["friends"] = friends values["requests"] = requests if aux == True: values["ok_message"] = lang["request_deleted_successfully"] + " " + str(who_ask.name) # Request deleted successfully else: values["ok_message"] = lang["friend_deleted_successfully"] + " " + str(who_ask.name) # Friend deleted successfully del requests2 # Delete variables to free memory # Else show an error message else: values["error_message"] = lang["error_delete"] # The deleting couldn't be done # Else show an error message else: values["error_message"] = lang["friend_not_deleted"] # Friend couldn't be deleted del lang, user, all, friends, requests, aux, all_comics, all_users, who_ask self.session_store.save_sessions(self.response) # Save sessions self.response.write(jinja.render_template("/friends/default.html", **values)) # Go to the friends home page # Else redirect to the login page else: del user # Delete variables to free memory self.redirect("/login") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # If an admin is logged in delete the comic if self.session.get('session_role') == 'admin': # Retrieving the comic key id = self.request.get("id", "") comic_author = list() if id != "": id = int(id) comic_author = ComicBook_Author.query( ComicBook_Author.id_aux == id).fetch( 1) # Get the author row in the ComicBook_Author table key = self.request.get("key", "") key = ndb.Key(urlsafe=key) comic = key.get() # Get the db comic with that key keys_page_list = self.request.get( "keys_page_list", "") # Comic keys (only comics in the current page) aux_all_keys = self.request.get( "all_keys", "") # All the comic keys (for the order field) # Initialize variables all_keys = list() # All the comic keys (for the order field) aux = list() # Support variable aux3 = list() # Support variable # Transform the HTML string in a list aux_all_keys = self.transform_keys(aux_all_keys) comics = ComicBook.query() all_comics = copy.copy(comics) # ALL comics (for the search field) all_comics = all_comics.fetch() for comic2 in comics: # Get ALL the keys aux.append(comic2.key.urlsafe()) del comic2 for key3 in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key3 == str(key2): key2 = ndb.Key(urlsafe=key2) aux3.append(key2) all_keys.append(key2.urlsafe()) break del key2 del key3 # Get db comics offset = (self.session.get('current_number_page') - 1) * self.session.get('num_elems_page') comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get('current_number_page'), # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics": all_comics, # ALL comic (for the search field) "authors": self.get_authors() # Authors for adding them to the comics } # If the author exists if comic and comic is not None and len(comic_author) > 0: # Delete the association in the ComicBook class for i in range(0, len(comic.authors)): if comic.authors[i].id_aux == comic_author[0].id_aux: comic.authors.pop(i) break del i comic.put() # Delete the ComicBook_Author bd row comic_author[0].key.delete() values["ok_message"] = lang[ "author_deleted_successfully"] # Ok message (author deleted successfully) # Setting pagination ############################################################################################## pages_to_show = self.session.get( 'pages_to_show' ) # Number of pages that shows the pagination pages = list( ) # Pages for the pagination (it's a list because of django for templates) # Get all db comics (Limited to the number given by the session variable [10 by default]) num_total = len(aux3) # Total number of elements if len(aux3) > 0: comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # List not empty else: comics = list() # If the total number of elements is above 0 do the pagination if num_total > 0 and len(comics) > 0: # Get the number of pages for the pagination num_elems_page = self.session.get( 'num_elems_page' ) # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages # If there are less elements than session["num_elems_page"] if num_pages == 0: num_pages = 1 # At least one page # Else else: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page self.session['last_page'] = num_pages # Last page # Set the pages for the pagination for i in range(1, num_pages + 1): pages.append(i) # Set number of pages for the pagination (pagination only shows 3 pages) current_number_page = self.session.get( 'current_number_page') # If the current page is 1 if current_number_page == 1: pages = pages[0:pages_to_show] # Shows pages 1,2,3 # If the current page is the final page elif current_number_page == num_pages: # Shows the last three pages if (num_pages - pages_to_show) >= 0: pages = pages[(num_pages - pages_to_show):] else: pages = pages[0:pages_to_show] # Else else: pages = pages[(current_number_page - 2):( current_number_page + 1)] # Show the current, last and next pages self.session['pages'] = pages # Pages for the pagination del num_elems_page, num_pages, current_number_page # Delete variables to free memory # If page is empty do the same, but return to the to the last page next to the current page elif num_total > 0 and len(comics) == 0: # Get all db comics (Limited to the number given by the session variable [10 by default]) offset = (self.session.get('current_number_page') - 2) * self.session.get('num_elems_page') comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # Get the number of pages for the pagination num_elems_page = self.session.get( 'num_elems_page' ) # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages # If there are less elements than session["num_elems_page"] if num_pages == 0: num_pages = 1 # At least one page # Else else: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page current_number_page = num_pages # The previous last page doesn't already exist, so it has to be the last page of the new list self.session['last_page'] = num_pages # Last page # Set the pages for the pagination for i in range(1, num_pages + 1): pages.append(i) # Set number of pages for the pagination (pagination only shows 3 pages) # If the current page is 1 if current_number_page == 1: pages = pages[0:pages_to_show] # Shows pages 1,2,3 # If the current page is the final page elif current_number_page == num_pages: # Shows the last three pages if (num_pages - pages_to_show) >= 0: pages = pages[(num_pages - pages_to_show):] else: pages = pages[0:pages_to_show] # Else else: pages = pages[(current_number_page - 2):( current_number_page + 1)] # Show the current, last and next pages self.session['pages'] = pages # Pages for the pagination self.session[ 'current_number_page'] = current_number_page # Current number page del num_elems_page, num_pages, current_number_page # Delete variables to free memory ########################################################################################### # Variables to be sent to the html values["comics"] = comics # Comics values["pages"] = self.session.get( 'pages') # Pages for the pagination values["last_page"] = self.session.get( 'last_page') # Last page number values["all_keys"] = all_keys # All comic keys values["current_number_page"] = self.session.get( 'current_number_page') # Current number page # Setting the all comics list all_comics = ComicBook.query().fetch() values["all_comics"] = all_comics # Setting the comic keys list that are currently in the page keys_page_list = list() for comic in comics: keys_page_list.append(comic.key.urlsafe()) values["keys_page_list"] = keys_page_list del pages_to_show, pages, num_total # Delete variables to free memory # Else show an error message else: values["error_message"] = lang[ "author_not_deleted"] # Error message (Author couldn't be deleted) del id, keys_page_list, aux_all_keys, all_keys, aux, aux3, all_comics, offset, comics,\ lang, key, comic, comic_author # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/comics/default.html", **values)) # Go to the comics home page # Else redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in. If it's logged in, show the home page if self.session.get('session_role') == 'client': user = User.query(User.name == self.session.get("session_name")).fetch() # Current user # If users exists, get friends and friends requests if user and len(user) > 0: user = user[0] # Get db friends and friend requests all = Friend.query().order(-Friend.addition_date) # Db friends friends = copy.copy(all) friends = friends.filter(Friend.is_friend == True) friends = friends.filter(ndb.OR(Friend.who_ask == user.key, Friend.who_answer == user.key)) friends = friends.order(-Friend.addition_date) aux = list() for friend in friends: if friend.who_answer != user.key: aux.append(friend.who_answer.get()) elif friend.who_ask != user.key: aux.append(friend.who_ask.get()) del friend friends = aux # Db friends requests requests = copy.copy(all) requests = requests.filter(Friend.is_friend == False) requests = requests.filter(Friend.who_answer == user.key) requests = requests.order(-Friend.addition_date) aux = list() for request in requests: aux.append(request.who_ask.get()) del request requests = aux # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english all_comics = ComicBook.query() comics = copy.copy(all_comics) comics = comics.filter(ComicBook.users.username == self.session.get("session_name")).fetch() all_comics = all_comics.fetch() all_users = User.query(User.role == "client").fetch() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image(self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "friends": friends, # User friends "requests": requests, # User friend requests "all_comics": all_comics, # ALL comic (for the users search field) "all_users": all_users, # ALL users (for the search field) "comics": comics, # Current user comics (for the borrowinngs) } friend = self.request.get("friend", "") # Friend comic = self.request.get("comic_key", "") # Comic # If the friend is valid if len(friend) > 0 and len(comic) > 0: friend = ndb.Key(urlsafe=friend) comic = ndb.Key(urlsafe=comic) friend = friend.get() comic = comic.get() # If user exists if friend is not None and comic is not None: borrowing = Borrowing.query(Borrowing.who_want == friend.key, Borrowing.who_borrow == user.key, Borrowing.comic == comic.key).fetch() # If the comic wasn't already borrowed to this user if len(borrowing) == 0: # Add a new borrowing borrowing = Borrowing(who_want=friend.key, who_borrow=user.key, is_borrowed=True, comic=comic.key) borrowing = borrowing.put() # If the adding was successful if borrowing is not None: values["ok_message"] = lang["borrowing_added_successfully"] + " " + str(friend.name) # Borrowing added successfully # Else show an error message else: values["error_message"] = lang["error_add"] # Else do nothing else: values["ok_message"] = lang["borrowing_already_added"] + " " + str(friend.name) # Borrowing already added del borrowing # Delete variables to free memory # Else show an error message else: values["error_message"] = lang["error_add"] # The adding couldn't be done # Else show an error message else: values["error_message"] = lang["borrowing_not_added"] # Borrowing couldn't be added del lang, user, all, friends, requests, aux, all_comics, all_users, friend, comic self.session_store.save_sessions(self.response) # Save sessions self.response.write(jinja.render_template("/friends/default.html", **values)) # Go to the friends home page # Else redirect to the login page else: del user # Delete variables to free memory self.redirect("/login") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in. If it's logged in, show the home page if self.session.get('session_role') == 'client': user = User.query(User.name == self.session.get( "session_name")).fetch() # Current user # Get the friend id friend = self.request.get("friend", "") # Friend # If users exists, get friends and friends requests if user and len(user) > 0: user = user[0] all = Friend.query().order(-Friend.addition_date) # Db friends friends = copy.copy(all) friends = friends.filter(Friend.is_friend == True) friends = friends.filter( ndb.OR(Friend.who_ask == user.key, Friend.who_answer == user.key)) friends = friends.order(-Friend.addition_date) aux = list() for friend2 in friends: if friend2.who_answer != user.key: aux.append(friend2.who_answer.get()) elif friend2.who_ask != user.key: aux.append(friend2.who_ask.get()) del friend2 friends = aux # Db friends requests requests = copy.copy(all) requests = requests.filter(Friend.is_friend == False) requests = requests.filter(Friend.who_answer == user.key) requests = requests.order(-Friend.addition_date) aux = list() for request in requests: aux.append(request.who_ask.get()) del request requests = aux # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english all_comics = ComicBook.query() comics = copy.copy(all_comics) comics = comics.filter(ComicBook.users.username == self. session.get("session_name")).fetch() all_comics = all_comics.fetch() all_users = User.query(User.role == "client").fetch() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "friends": friends, # User friends "requests": requests, # User friend requests "all_comics": all_comics, # ALL comic (for the users search field) "all_users": all_users, # ALL users (for the search field) "comics": comics, # Current user comics (for the borrowinngs) } # If the key is valid if len(friend) > 0: friend = ndb.Key( urlsafe=friend) # User who made the friend request friend = friend.get() # If user exists if friend != None: collection = ComicBook.query( ComicBook.users.username == friend.name).order( ComicBook.title) # Friend collection # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get( 'session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "all_comics": all_comics, # ALL comic (for the users search field) "collection": collection, # Friend collection "friend": friend # Friend } del lang, user, all, friends, requests, aux, all_comics, all_users, friend, collection # Delete variables to free memory self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/friends/collection.html", **values)) # Go to the collection page # Else show an error message else: del lang, user, all, friends, requests, aux, all_comics, all_users, friend values["error_message"] = lang[ "friend_not_collection2"] # There was a problem showing the user collection self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/friends/default.html", **values)) # Go to the friends home page # Else show an error message else: del lang, user, all, friends, requests, aux, all_comics, all_users, friend values["error_message"] = lang[ "friend_not_collection"] # Collection couldn't be shown self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/friends/default.html", **values)) # Go to the friends home page # Else redirect to the login page else: del user # Delete variables to free memory self.redirect("/login") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in. If it's logged in, add a borrowing if self.session.get('session_role') == 'client': user = User.query(User.name == self.session.get( "session_name")).fetch() # Current user # If users exists, get borrowings if user and len(user) > 0: user = user[0] # Get db borrowings all = Borrowing.query() # Db borrowed comics borrow_comics = copy.copy(all) borrow_comics = borrow_comics.filter( Borrowing.is_borrowed == True) borrow_comics = borrow_comics.filter( Borrowing.who_borrow == user.key) aux = list() for borrow_comic in borrow_comics: aux2 = borrow_comic.comic.get() aux3 = borrow_comic.who_want.get() aux2.owner_name = aux3.name aux.append(aux2) del borrow_comic, aux2, aux3 borrow_comics = aux # Db asked comics orders = copy.copy(all) orders = orders.filter(Borrowing.is_borrowed == False) orders = orders.filter(Borrowing.who_borrow == user.key) aux = list() for order in orders: aux2 = order.comic.get() aux3 = order.who_want.get() aux2.owner_name = aux3.name aux.append(aux2) del order, aux2, aux3 orders = aux # Db comics that user ask for own = copy.copy(all) own = own.filter(Borrowing.is_borrowed == True) own = own.filter(Borrowing.who_want == user.key) aux = list() for elem in own: aux2 = elem.comic.get() aux3 = elem.who_borrow.get() aux2.owner_name = aux3.name aux.append(aux2) del elem, aux2, aux3 own = aux # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english all_comics = ComicBook.query().fetch() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "borrow_comics": borrow_comics, # Borrowed comics "orders": orders, # Asked comics "own": own, # Comics that current user ask for "all_comics": all_comics, # ALL comic (for the users search field) } friend = self.request.get("friend_name", "") # Friend friend = User.query(User.name == friend).fetch() comic = self.request.get("comic_key", "") # Comic # If the friend is valid if len(friend) > 0 and len(comic) > 0: friend = friend[0] comic = ndb.Key(urlsafe=comic) comic = comic.get() # If user exists if friend is not None and comic is not None: # Add a new borrowing borrowing = Borrowing.query( Borrowing.who_want == friend.key, Borrowing.who_borrow == user.key, Borrowing.comic == comic.key).fetch() # If the borrowing exists if len(borrowing) > 0: # If the adding was successful if borrowing is not None: borrowing = borrowing[0] borrowing.is_borrowed = True borrowing.put() time.sleep(1) comic = borrowing.comic.get() # Update lists if borrowing in orders: orders.remove(comic) comic.owner_name = friend.name borrow_comics.append(comic) # Values to be sent to the HTML page values["orders"] = orders values["borrow_comics"] = borrow_comics values["ok_message"] = lang[ "borrowing_added_successfully"] + " " + str( friend.name ) # Borrowing added successfully # Else else: values["error_message"] = lang["error_add"] # Else show an error message else: values["error_message"] = lang[ "error_add"] # The adding couldn't be done del borrowing # Delete variables to free memory # Else show an error message else: values["error_message"] = lang[ "error_add"] # The adding couldn't be done # Else show an error message else: values["error_message"] = lang[ "borrowing_not_added"] # Borrowing couldn't be added del lang, user, all, borrow_comics, orders, aux, all_comics, own, friend, comic self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/borrowings/default.html", **values)) # Go to the borrowings home page # Else redirect to the login page else: del user # Delete variables to free memory self.redirect("/login") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def get(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in. If it's logged in, show the home page if self.session.get('session_role') == 'client': # Initialize variables pages_to_show = self.session.get( 'pages_to_show') # Number of pages that shows the pagination pages = list( ) # Pages for the pagination (it's a list because of django for templates) self.session[ 'current_number_page'] = 1 # Session current number page all_keys = list() # All comic keys keys_page_list = list() # Comic keys currently in the page shelving_name = list() # Shelving name self.session['num_elems_page'] = 12 # Amount of elements # Get all comics that belongs to the current user if not self.session.get("shelving"): comics = ComicBook.query( ComicBook.users.username == self.session.get('session_name')).order( ComicBook.users.addition_date ) # All comics ordered by the addition date all_comics_user = copy.copy( comics) # ALL comics (for the search field) all_comics_user = all_comics_user.fetch() else: key = ndb.Key(urlsafe=self.session.get("shelving")) comics = ComicBook.query( ComicBook.users.username == self.session.get( 'session_name'), ComicBook.users.shelving == key).order( ComicBook.users.addition_date ) # Comics in the shelving ordered by the addition date shelving = key.get() shelving_name = shelving.name del shelving all_comics_user = ComicBook.query( ComicBook.users.username == self.session.get( 'session_name')).fetch( ) # ALL comics (for the search field) # Get ALL the comic keys (they are necessary to do the order and filter) for comic in comics: all_keys.append(comic.key.urlsafe()) del comic # Number of comics shown in the page comics = comics.fetch(12) # Get the comics (if --> default, else --> see shelving) if not self.session.get('shelving'): comics = self.get_comics_read_and_without_shelving( comics ) # Get read comics and the ones that aren't in a shelving else: self.get_comics_read(comics) # Get read comics num_total = len(all_comics_user) # Total number of elements # Get the shelvings shelvings = Shelving.query( Shelving.username == self.session.get('session_name')).order( Shelving.name).fetch() self.session['page_name'] = "/library" # Current page name # If the total number of elements is above 0 do the pagination if num_total > 0: # Get the number of pages for the pagination num_elems_page = self.session.get( "num_elems_page") # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages # If there are less elements than session["num_elems_page"] if num_pages == 0: num_pages = 1 # At least one page # Else else: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page self.session['last_page'] = num_pages # Last page # Set the pages for the pagination for i in range(1, num_pages + 1): pages.append(i) # Set number of pages for the pagination (pagination only shows 3 pages) current_number_page = 1 # If the current page is 1 if current_number_page == 1: pages = pages[0:pages_to_show] # Shows pages 1,2,3 # If the current page is the final page elif current_number_page == num_pages: # Shows the last three pages if (num_pages - pages_to_show) >= 0: pages = pages[(num_pages - pages_to_show):] else: pages = pages[0:pages_to_show] # Else else: pages = pages[(current_number_page - 1):( current_number_page + 1)] # Show the current, last and next pages self.session['pages'] = pages # Pages for the pagination # Setting the keys list to allow the use of the "Delete page" button for comic in comics: keys_page_list.append(comic.key.urlsafe()) del comic del num_elems_page, num_pages, current_number_page, pages # Delete variables to free memory # If num_total < 0 else: comics = all_comics_user # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english all_comics = ComicBook.query().fetch() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": 1, # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics_user": all_comics_user, # All user comics (for the search field) "all_comics": all_comics, # ALL comics (for the user search field) "shelvings": shelvings, # All user shelvings "shelving_name": shelving_name # Shelving name } del num_total, pages_to_show, keys_page_list, all_keys, lang, comics, all_comics_user, all_comics, shelving_name # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/library/default.html", **values)) # Go to the library home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in if self.session.get('session_role') == 'client': # If it's logged in, get the session variables, show the home page # Get the comic attributes name = self.request.get("m_name", "") # Shelving name name = name.encode("utf8") picture = self.request.get("m_picture", "") # Shelving picture key = self.request.get("key", "") key = ndb.Key(urlsafe=key) shelving = key.get() # Get the shelving with that key keys_page_list = self.request.get("keys_page_list", "") # Comic keys (only comics in the current page) aux_all_keys = self.request.get("all_keys", "") # All the comic keys (for the order field) # Initialize variables aux = list() # Support variable aux3 = list() # Support variable all_comics = list() # List with all comics (for the search field) shelving_name = list() # Shelving name # Get the user shelvings shelvings = Shelving.query(Shelving.username == self.session.get("session_name")).order(Shelving.name).fetch() # Transform the HTML string in a list all_keys = copy.copy(aux_all_keys) aux_all_keys = self.transform_keys(aux_all_keys) # Get all comics that belongs to the current user if not self.session.get("shelving"): comics = ComicBook.query(ComicBook.users.username == self.session.get('session_name')).order(ComicBook.users.addition_date) # All comics ordered by the addition date all_comics_user = copy.copy(comics) # ALL comics (for the search field) all_comics_user = all_comics_user.fetch() else: key = ndb.Key(urlsafe=self.session.get("shelving")) comics = ComicBook.query(ComicBook.users.username == self.session.get('session_name'),ComicBook.users.shelving == key).order(ComicBook.users.addition_date) # Comics in the shelving ordered by the addition date shelving2 = key.get() shelving_name = shelving2.name del shelving2 all_comics_user = ComicBook.query(ComicBook.users.username == self.session.get('session_name')).fetch() # ALL comics (for the search field) for comic2 in comics: # Get ALL the keys aux.append(comic2.key.urlsafe()) del comic2 for key3 in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key3 == str(key2): key2 = ndb.Key(urlsafe=key2) aux3.append(key2) break del key2 del key3 # Get all db comics offset = (self.session.get('current_number_page') - 1) * self.session.get('num_elems_page') if len(aux3) > 0: comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch(self.session.get('num_elems_page'), offset=offset) # Get the comics (if --> default, else --> see shelving) if not self.session.get('shelving'): comics = self.get_comics_read_and_without_shelving(comics) # Get read comics and the ones that aren't in a shelving else: self.get_comics_read(comics) # Get read comics else: comics = list() # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image(self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get('current_number_page'), # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics": all_comics, # ALL comic (for the user search field) "authors": self.get_authors(), # Authors for adding them to the comics "all_comics_user": all_comics_user, # All user comics (for the user search field) "shelvings": shelvings, # Shelvings "shelving_name": shelving_name # Shelving name } # If the key is from an shelving if shelving and shelving is not None: # Check the shelving attributes if len(name) > 0: shelving.name = name if picture != "": shelving.picture = images.resize(picture, 300, 250) aux2 = shelving.put() time.sleep(1) # If the modification was successful if aux2 is not None: # Variables to be sent to the HTML page values["ok_message"] = lang["shelving_modified_successfully"] # Ok message (Shelving modified successfully) # Get all db Comics (Limited to the number given by the session variable [10 by default]) if len(aux3) > 0: comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch(self.session.get('num_elems_page'), offset=offset) # Get the comics (if --> default, else --> see shelving) if not self.session.get('shelving'): comics = self.get_comics_read_and_without_shelving( comics) # Get read comics and the ones that aren't in a shelving else: self.get_comics_read(comics) # Get read comics values["comics"] = comics else: comics = ComicBook.query().fetch() # Get the user shelvings shelvings = Shelving.query(Shelving.username == self.session.get("session_name")).order(Shelving.name).fetch() values["shelvings"] = shelvings # Else show an error message else: # Variables to be sent to the HTML page values["error_message"] = lang["error_modify"] # Error message (The modification couldn't be done) del aux2 # Delete variables to free memory # Else show an error message else: # Values to be sent to the HTML page values["error_message"] = lang["shelving_not_modified"] # Error message (Shelving couldn't be modified) all_comics = ComicBook.query().fetch() # ALL comics (for the search field) values["all_comics"] = all_comics del lang, name, key, shelving, keys_page_list, aux_all_keys, aux, aux3, \ all_comics, offset, all_keys, all_comics_user, shelvings # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write(jinja.render_template("/library/default.html", **values)) # Go to the library home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in if self.session.get('session_role') == 'client': # If it's logged in, get the session variables, show the home page # Get the search field search = self.request.get("search", "") # Sort option search = search.encode("utf8") # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Initializing variables all_keys = list() comics = list() pages = list() keys_page_list = list() all_comics = ComicBook.query().fetch() all_comics_user = ComicBook.query( ComicBook.users.username == self.session.get( 'session_name')).fetch() # Get the shelvings shelvings = Shelving.query( Shelving.username == self.session.get('session_name')).order( Shelving.name).fetch() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get('current_number_page'), # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics_user": all_comics_user, # All user comic (for the search field) "all_comics": all_comics, # ALL comics (for the user search field) "shelvings": shelvings # All user shelvings } # Check if there is a field to search if search != "" and len(search) > 0: # Get the key sent in the search field key = ndb.Key(urlsafe=search) # Get the comics with that key comic = key.get() # Check if the comics exists if comic and comic is not None: comics.append(comic) # Comics list comics = self.get_comics_read_and_without_shelving( comics) # Get only the comics without a shelving values["comics"] = comics # Search results # Setting all the comic keys all_keys.append(comic.key.urlsafe()) values["all_keys"] = all_keys # Setting the keys list to allow the use of the "Delete page" button keys_page_list.append(comic.key.urlsafe()) values["keys_page_list"] = keys_page_list # If not exists, shows a message else: values["ok_message"] = lang[ "search_not_results"] # Ok message (No matches found) del key, comic # Delete variables to free memory # If not exists, shows a message else: values["ok_message"] = lang[ "search_not_results"] # Ok message (No matches found) del search, comics, pages, keys_page_list, all_keys, lang, all_comics_user, all_comics # Delete variables to free memory self.response.write( jinja.render_template("/library/default.html", **values)) # Go to the library home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in. If it's logged in, show the home page if self.session.get('session_role') == 'client': user = User.query(User.name == self.session.get( "session_name")).fetch() # Current user # Get the friend id search = self.request.get("search", "") # If users exists, get friends and friends requests if user and len(user) > 0: user = user[0] all_comics = ComicBook.query() comics = copy.copy(all_comics) comics = comics.filter(ComicBook.users.username == self. session.get("session_name")).fetch() all_comics = all_comics.fetch() all_users = User.query(User.role == "client").fetch() # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "all_comics": all_comics, # ALL comic (for the users search field) "all_users": all_users, # ALL users (for the search field) "comics": comics, # Current user comics (for the borrowinngs) } # If the key is valid if len(search) > 0: search = ndb.Key(urlsafe=search) search = search.get() # If the user exists, see if it's a friend, request other user if search and search is not None: all = Friend.query( Friend.who_ask == search.key, Friend.who_answer == user.key).fetch() aux = copy.copy(all) all = Friend.query( Friend.who_ask == user.key, Friend.who_answer == search.key).fetch() all += aux is_friend = None aux = None # See if the user is already a friend or has a friend request for elem in all: if elem.who_ask == search.key and elem.who_answer == user.key: is_friend = elem.is_friend aux = True break else: aux = False del elem # Variables to be shown in the HTML page if is_friend is None and aux is None: others = list() others.append(search) values["others"] = others del others else: if is_friend == False and aux == True: requests = list() requests.append(search) values["requests"] = requests del requests elif is_friend == True and aux == True: friends = list() friends.append(search) values["friends"] = friends del friends del all, is_friend, aux # Delete variables to free memory # Else show a message else: values["ok_message"] = lang[ "search_not_results"] # There aren't any search result # Else show a message else: values["ok_message"] = lang[ "search_not_results"] # There aren't any search result del lang, user, all_comics, all_users, search self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/friends/default.html", **values)) # Go to the friends home page # Else redirect to the login page else: del user # Delete variables to free memory self.redirect("/login") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the admin is logged in. If it's logged in, show the home page if self.session.get('session_role') == 'admin': pages_to_show = self.session.get( 'pages_to_show') # Number of pages that shows the pagination pages = list( ) # Pages for the pagination (it's a list because of django for templates) # Retrieve the page number page_number = self.request.get( "page", self.session.get('current_number_page')) page_number = int(page_number) # If the page number sent by HTML is above 0 if page_number > 0: # Initialize variables all_keys = list() # New comic keys list comics = list() # Comics list aux3 = list() # All comic keys support list all_comics = list() # ALL comics (for the search field) # Set the offset for the query (offset allows to ignore an amount of query results given by its value) num_elems_page = self.session.get('num_elems_page') offset = num_elems_page * (page_number - 1) # Get ALL keys from ALL comics (I do this because I want to maintain the ordinations and filters done in the list) aux_all_keys = self.request.get("all_keys", "") # All comic keys list # Check if the comics keys are empty if len(aux_all_keys) > 2: # Transform the HTML string in a list aux_all_keys = self.transform_keys(aux_all_keys) # It's necessary to compare the HTML with the query list in order to obtain the desired list, like I said comics = ComicBook.query() all_comics = copy.copy( comics) # ALL comics (for the search field) all_comics = all_comics.fetch() aux = list() for comic in comics: # Get ALL the keys aux.append(comic.key.urlsafe()) del comic for key in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key == str(key2): key2 = ndb.Key(urlsafe=key) all_keys.append(key2) aux3.append(key2.urlsafe()) break del key2 del key del aux # Delete variables to free memory # Get all db comics (Limited to the number given by the session variable [10 by default]) # Not ordered because it has to respect if the comic used some ordination or filter comics = ComicBook.query(ComicBook.key.IN(all_keys)).fetch( num_elems_page, offset=offset) num_total = len(aux_all_keys) # Total number of elements # Setting all the comic keys all_keys = aux3 del aux3 # Delete variables to free memory # If the total number of elements is above 0 do the pagination if num_total > 0: # Get the number of pages for the pagination num_pages = num_total / num_elems_page # Number of pages # If the page number sent by html is less than the last possible page if page_number <= (num_pages + 1): # Update the session current number page self.session['current_number_page'] = page_number current_number_page = page_number # If there are less elements than session["num_elems_page"] if num_pages == 0: num_pages = 1 # At least one page # Else else: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page self.session['last_page'] = num_pages # Last page # Set the pages for the pagination for i in range(1, num_pages + 1): pages.append(i) # If the current page is 1 if current_number_page == 1: pages = pages[0:pages_to_show] # Shows pages 1,2,3 # If the current page is the final page elif current_number_page == num_pages: # Shows the last three pages if (num_pages - pages_to_show) >= 0: pages = pages[(num_pages - pages_to_show):] else: pages = pages[0:pages_to_show] # Else else: pages = pages[(current_number_page - 2):( current_number_page + 1)] # Show the current, last and next pages self.session[ 'pages'] = pages # Pages for the pagination # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Setting the keys list to allow the use of the "Delete page" button keys_page_list = list() for comic in comics: keys_page_list.append(comic.key.urlsafe()) del comic # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get( 'session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get( 'current_number_page'), # Current number page "pages": self.session.get( 'pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics": all_comics, # ALL comic (for the search field) "authors": self.get_authors( ) # Authors for adding them to the comics } del pages_to_show, page_number, num_elems_page, num_total, offset, \ num_pages, current_number_page, pages, keys_page_list, comics, aux_all_keys, all_keys,\ all_comics # Delete variables to free memory self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/comics/default.html", **values)) # Go to the comics home page # If it isn't less redirect to comics home page else: del pages_to_show, page_number, pages, num_elems_page, num_total, offset, comics, num_pages, \ aux_all_keys, all_comics # Delete variables to free memory self.redirect("/comics") # If the total number is not above 0, redirect to the comics home page else: del pages_to_show, page_number, pages, offset, comics, num_elems_page, aux_all_keys # Delete variables to free memory self.redirect("/comics") # If it isn't above redirect to comics home page else: del pages_to_show, page_number, pages # Delete variables to free memory self.redirect("/comics") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the admin or client are logged in if self.session.get('session_role') == 'admin' or self.session.get( 'session_role') == 'client': # If it's logged in, get the session variables # Get the user attributes key = self.request.get("key", "") # User key key = ndb.Key(urlsafe=key) user = key.get() # Get the user with that key # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "user_email": user.email, # User email "user_key": user.key.urlsafe(), # User key "user_genre": self.session.get('session_genre'), # User genre "statistics": self.get_statistics() # Client statistics } # If the user and key exist if user is not None and key is not None: # Delete all associations between ComicBook and User_ComicBook aux = list() comics = ComicBook.query(ComicBook.users.username == user.name) for comic in comics: for user_comic in comic.users: if user_comic.username != user.name: aux.append(user_comic) comic.users = aux comic.put() del comic aux = list() del aux # Delete the client library shelvings = Shelving.query(Shelving.username == user.name) for shelving in shelvings: shelving.key.delete() del shelving # Delete all user User_ComicBook rows user_comics = User_ComicBook.query( User_ComicBook.username == user.name) for user_comic in user_comics: user_comic.key.delete() del user_comic # Delete all user friends friends = Friend.query( ndb.OR(Friend.who_ask == user.key, Friend.who_answer == user.key)) for friend in friends: friend.key.delete() # Delete the user user.key.delete() del lang, key, user, user_comics, comics, shelvings, friends # Delete variables to free memory self.redirect("/logout") # Redirect to the login page # Else show an error message else: # Values to be sent to the HTML page values["error_message"] = lang[ "account_not_deleted"] # Error message (Account couldn't be modified) del lang, key, user # Delete variables to free memory self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/profile/default.html", **values)) # Go to the profile home page # If they aren't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in if self.session.get('session_role') == 'client': # If it's logged in, get the session variables, show the home page # Get the search field search = self.request.get("search", "") # Comic search = search.encode("utf8") # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english self.session['page_name'] = "/user_search" # Current page name # Initializing variables comics = list() all_comics = ComicBook.query() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "all_comics": all_comics # All comics (for the user search) } # Check if there is a field to search if search != "" and len(search) > 0: # Get the key sent in the search field key = ndb.Key(urlsafe=search) # Get the comics with that key comic = key.get() # Check if the comics exists if comic and comic is not None: comics.append(comic) # Comics list self.get_own_comics( comics) # Get the comics added by the user values["comics"] = comics # Search results # If not exists, shows a message else: values["ok_message"] = lang[ "search_not_results"] # Ok message (No matches found) del key, comic # Delete variables to free memory # If not exists, shows a message else: values["ok_message"] = lang[ "search_not_results"] # Ok message (No matches found) del search, comics, lang # Delete variables to free memory self.response.write( jinja.render_template("/elements/user_search_results.html", **values)) # Go to the comics home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in if self.session.get('session_role') == 'client': # If it's logged in, get the session variables, show the home page # Get the comic attributes state = self.request.get("state", "") # Comic read or unread state = state.decode("utf8") key = self.request.get("comic_key", "") key = ndb.Key(urlsafe=key) comic = key.get() # Get the comic with that key keys_page_list = self.request.get("keys_page_list", "") # Comic keys (only comics in the current page) aux_all_keys = self.request.get("all_keys", "") # All the comic keys (for the order field) # Initialize variables aux = list() # Support variable aux3 = list() # Support variable all_comics_user = list() # List with all comics (for the search field) all_comics = list() # All comics (for the user search field) # Transform the HTML string in a list all_keys = copy.copy(aux_all_keys) aux_all_keys = self.transform_keys(aux_all_keys) comics = ComicBook.query() for comic2 in comics: # Get ALL the keys aux.append(comic2.key.urlsafe()) del comic2 for key3 in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key3 == str(key2): key2 = ndb.Key(urlsafe=key2) aux3.append(key2) break del key2 del key3 # Get all db comics offset = (self.session.get('current_number_page') - 1) * self.session.get('num_elems_page') comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch(self.session.get('num_elems_page'), offset=offset) # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image(self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get('current_number_page'), # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics_user": all_comics_user, # All user comic (for the search field) "all_comics": all_comics # ALL comics (for the user search field) } # If the key is from an comic if comic and comic is not None and (state == "read" or state == "unread"): # Modify the comic state for user_comic in comic.users: if user_comic.username == self.session.get("session_name"): user_comic.state = state aux2 = comic.put() time.sleep(1) # If the modification was successful if aux2 is not None: # Variables to be sent to the HTML page values["ok_message"] = lang["comic_modified_successfully"] # Ok message (Comic modified successfully) # Get all db Comics (Limited to the number given by the session variable [10 by default]) comics = ComicBook.query(ComicBook.key.IN(aux3)).fetch(self.session.get('num_elems_page'), offset=offset) values["comics"] = comics # Else show an error message else: # Variables to be sent to the HTML page values["error_message"] = lang["error_modify"] # Error message (The modification couldn't be done) del aux2 # Delete variables to free memory # Else show an error message else: # Values to be sent to the HTML page values["error_message"] = lang["comic_not_modified"] # Error message (Comic couldn't be modified) all_comics_user = ComicBook.query(ComicBook.users.username == self.session.get('session_name')).fetch() values["all_comics_user"] = all_comics_user all_comics = ComicBook.query().fetch() self.get_read_comics(comics) del lang, key, comic, keys_page_list, aux_all_keys, aux, aux3, \ all_comics_user, offset, all_keys, all_comics # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write(jinja.render_template("/collection/default.html", **values)) # Go to the collection home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def get(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in. If it's logged in, show the home page if self.session.get('session_role') == 'client': user = User.query(User.name == self.session.get( "session_name")).fetch() # Current user # If users exists, get borrowings if user and len(user) > 0: self.session['page_name'] = "/borrowings" # Current page name user = user[0] # Get db borrowings all = Borrowing.query() # Db borrowed comics borrow_comics = copy.copy(all) borrow_comics = borrow_comics.filter( Borrowing.is_borrowed == True) borrow_comics = borrow_comics.filter( Borrowing.who_borrow == user.key) aux = list() for borrow_comic in borrow_comics: aux2 = borrow_comic.comic.get() aux3 = borrow_comic.who_want.get() aux2.owner_name = aux3.name aux.append(aux2) del borrow_comic, aux2, aux3 borrow_comics = aux # Db asked comics orders = copy.copy(all) orders = orders.filter(Borrowing.is_borrowed == False) orders = orders.filter(Borrowing.who_borrow == user.key) aux = list() for order in orders: aux2 = order.comic.get() aux3 = order.who_want.get() aux2.owner_name = aux3.name aux.append(aux2) del order, aux2, aux3 orders = aux # Db comics that user ask for own = copy.copy(all) own = own.filter(Borrowing.is_borrowed == True) own = own.filter(Borrowing.who_want == user.key) aux = list() for elem in own: aux2 = elem.comic.get() aux3 = elem.who_borrow.get() aux2.owner_name = aux3.name aux.append(aux2) del elem, aux2, aux3 own = aux # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english all_comics = ComicBook.query().fetch() # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "borrow_comics": borrow_comics, # Borrowed comics "orders": orders, # Asked comics "own": own, # Comics that current user ask for "all_comics": all_comics, # ALL comic (for the users search field) } del lang, user, all, borrow_comics, orders, aux, all_comics, own self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/borrowings/default.html", **values)) # Go to the borrowings home page # Else redirect to the login page else: del user # Delete variables to free memory self.redirect("/login") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def post(self): jinja = jinja2.get_jinja2(app=self.app) # Check if the client is logged in if self.session.get('session_role') == 'client': # Initialize variables all_keys = list() # New comic keys list comics = None # Comics list all_comics_user = list() # ALL comics (for the search field) pages = list() # Pages for the pagination # If it's logged in, get the session variables, show the home page # Get the filter attributes filter_by = self.request.get_all("filter_by[]") # Filter option aux_all_keys = self.request.get("all_keys", "") # All comic keys list # Set the default language of the app if self.session['session_idiom'] == "spa": lang = spa # Spanish strings elif self.session['session_idiom'] == "eng": lang = eng # English strings else: lang = eng # Default english if len(aux_all_keys) > 2: # Transform the HTML string in a list aux_all_keys = self.transform_keys(aux_all_keys) # It's necessary to compare the HTML with the query list in order to obtain the desired list, like I said comics = ComicBook.query(ComicBook.users.username == self.session.get('session_name')) all_comics_user = copy.copy( comics) # ALL comics (for the search field) all_comics_user = all_comics_user.fetch() aux = list() for comic in comics: # Get ALL the keys aux.append(comic.key.urlsafe()) del comic for key in aux_all_keys: # Compare the "list" given by HTML with aux for making the new all keys list (all_keys) for key2 in aux: if key == str(key2): key2 = ndb.Key(urlsafe=key) all_keys.append(key2) break del key2 del key del aux # Delete variables to free memory # Get all db comics (Limited to the number given by the session variable [10 by default]) # Not ordered because it has to respect if the comic used some ordination or filter comics = ComicBook.query(ComicBook.key.IN(all_keys)) # If there is a list of comics and a sort option if len(filter_by) > 0 and comics is not None: ok_message = lang[ "filtered_by"] + ": " # Ok message (filters applied) for i in range(0, len(filter_by)): # Filter the comic list if filter_by[i] == "comic" or filter_by[ i] == "manga" or filter_by[i] == "anthology": comics = ComicBook.query( ComicBook.key.IN(all_keys)).filter( ComicBook.type == filter_by[i]) # Type male or female elif filter_by[i] == "american" or filter_by[ i] == "european" or filter_by[i] == "other": comics = ComicBook.query( ComicBook.key.IN(all_keys)).filter( ComicBook.origin == filter_by[i]) # Origin admin or client elif filter_by[i] == "read" or filter_by[i] == "unread": comics = ComicBook.query( ComicBook.key.IN(all_keys)).filter( ComicBook.users.username == self.session.get("session_name") and ComicBook.users.state == filter_by[i]) # Comic read or unread # Setting the all comic keys list all_keys = list() aux_all_keys = list() if comics.count() > 0: for comic in comics: aux_all_keys.append(comic.key.urlsafe()) all_keys.append(comic.key) del comic comics = comics.fetch( self.session.get('num_elems_page') ) # Setting the first page to show else: comics = list() ok_message += lang[filter_by[i]].lower( ) + ", " # Update the ok message (the filter applied) ok_message = ok_message[:len( ok_message ) - 2] # Update the ok message (remove the last ", ") all_keys = aux_all_keys # Get the number of pages for the pagination num_total = len(all_keys) num_elems_page = self.session.get( 'num_elems_page') # Number of elements (comics) per page num_pages = num_total / num_elems_page # Number of pages if num_pages > 0: # If the rest of the division is above 0 if (num_total % num_elems_page) > 0: num_pages = (num_total / num_elems_page ) + 1 # It's necessary other page else: num_pages = (num_total / num_elems_page ) # It doesn't need other page # Set the pages for the pagination if num_pages >= self.session.get('pages_to_show'): for i in range(1, self.session.get('pages_to_show') + 1): pages.append(i) else: for i in range(1, num_pages + 1): pages.append(i) # Setting the comic keys list (only on the current page) keys_page_list = list() for comic in comics: keys_page_list.append(comic.key.urlsafe()) del comic all_comics = ComicBook.query().fetch() self.get_read_comics(comics) # Variables to be sent to the HTML page values = { "lang": lang, # Language strings "session_name": self.session.get('session_name'), # User name "session_role": self.session.get('session_role'), # User role "session_picture": self.get_session_image( self.session.get('session_name')), # User picture "session_genre": self.session.get('session_genre'), # User genre "comics": comics, # Comics "current_number_page": self.session.get( 'current_number_page'), # Current number page "pages": self.session.get('pages'), # Pages for the pagination "last_page": self.session.get('last_page'), # Last page number "keys_page_list": keys_page_list, # Comics keys that are currently in the page "all_keys": all_keys, # All comic keys "all_comics_user": all_comics_user, # All user comic (for the search field) "all_comics": all_comics, # ALL comics (for the user search field) "filter_message": ok_message # Applied filters message } del filter_by, aux_all_keys, all_keys, comics, lang, keys_page_list, pages,\ num_total, num_pages, num_elems_page, all_comics_user, ok_message # Delete variables to free memory self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/collection/default.html", **values)) # Go to the comics home page # Else redirect to the comics home page else: del filter_by, aux_all_keys, all_keys, comics # Delete variables to free memory self.redirect("/collection") # If it isn't logged in, redirect to the login page else: self.redirect("/login")