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 author keys list authors = None # Authors list all_authors = list() # ALL authors (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 author 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 authors = Author.query() all_authors = copy.copy( authors) # ALL authors (for the search field) all_authors = all_authors.fetch() aux = list() for author in authors: # Get ALL the keys aux.append(author.key.urlsafe()) del author 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 authors (Limited to the number given by the session variable [10 by default]) # Not ordered because it has to respect if the user used some ordination or filter authors = Author.query(Author.key.IN(all_keys)) # If there is a list of authors and a sort option if order_by != "" and authors 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 (authors) 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 author list if order_by == "name": authors = authors.order(Author.name) # Name ascendant # Setting the all author keys list all_keys = list() for author in authors: all_keys.append(author.key.urlsafe()) del author authors = authors.fetch( self.session.get('num_elems_page')) # Name ascendant elif order_by == "-name": authors = authors.order(-Author.name) # Name ascendant # Setting the all author keys list all_keys = list() for author in authors: all_keys.append(author.key.urlsafe()) del author authors = authors.fetch( self.session.get('num_elems_page')) # Name descendant else: authors = authors.order(Author.name) # Name ascendant # Setting the all author keys list all_keys = list() for author in authors: all_keys.append(author.key.urlsafe()) del author authors = authors.fetch( self.session.get('num_elems_page')) # Name ascendant # Setting the author keys list (only on the current page) keys_page_list = list() for author in authors: keys_page_list.append(author.key.urlsafe()) del author # 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 "authors": authors, # Authors "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, # Authors keys that are currently in the page "all_keys": all_keys, # All author keys "all_authors": all_authors # ALL author (for the search field) } del order_by, aux_all_keys, all_keys, authors, lang, keys_page_list, pages,\ num_total, num_pages, num_elems_page, all_authors # Delete variables to free memory self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/authors/default.html", **values)) # Go to the authors home page # Else redirect to the authors home page else: del order_by, aux_all_keys, all_keys, authors # Delete variables to free memory self.redirect("/authors") # If it isn't logged in, redirect to the login page else: self.redirect("/login")
def get_authors(self): authors = Author.query().fetch() return authors
def post(self): jinja = jinja2.get_jinja2(app=self.app) # If an admin is logged in delete the author if self.session.get('session_role') == 'admin': # Retrieving the author key key = self.request.get("key", "") key = ndb.Key(urlsafe=key) author = key.get() # Get the db author with that key keys_page_list = self.request.get( "keys_page_list", "") # Author keys (only authors in the current page) aux_all_keys = self.request.get( "all_keys", "") # All the author keys (for the order field) # Initialize variables all_keys = list() # All the author 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) authors = Author.query() all_authors = copy.copy( authors) # ALL authors (for the search field) all_authors = all_authors.fetch() for author2 in authors: # Get ALL the keys aux.append(author2.key.urlsafe()) del author2 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 authors offset = (self.session.get('current_number_page') - 1) * self.session.get('num_elems_page') authors = Author.query(Author.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 "authors": authors, # Authors "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, # Authors keys that are currently in the page "all_keys": all_keys, # All author keys "all_authors": all_authors # ALL author (for the search field) } # If the author exists if author and author is not None: # Deleting the author in other db models (all the ocurrences of the author in other models must be deleted) self.delete_author_everywhere(author) # Deleting the old author key to the author keys lists all_keys.remove(author.key.urlsafe()) aux3.remove(author.key) author.key.delete() # Delete db author 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 authors (Limited to the number given by the session variable [10 by default]) num_total = len(aux3) # Total number of elements if len(aux3) > 0: authors = Author.query(Author.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # List not empty else: authors = list() # If the total number of elements is above 0 do the pagination if num_total > 0 and len(authors) > 0: # Get the number of pages for the pagination num_elems_page = self.session.get( 'num_elems_page' ) # Number of elements (authors) 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(authors) == 0: # Get all db authors (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') authors = Author.query(Author.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 (authors) 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["authors"] = authors # Authors 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 author keys values["current_number_page"] = self.session.get( 'current_number_page') # Current number page # Setting the all authors list all_authors = Author.query().fetch() values["all_authors"] = all_authors # Setting the author keys list that are currently in the page keys_page_list = list() for author in authors: keys_page_list.append(author.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 key, author, keys_page_list, aux_all_keys, all_keys, aux, aux3, all_authors, offset, authors,\ lang # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/authors/default.html", **values)) # Go to the authors home page # Else 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 author keys keys_page_list = list() # Author keys currently in the page # Get all db authors (Limited to the number given by the session variable [10 by default]) authors = Author.query().order( Author.name) # All authors ordered by name all_authors = copy.copy( authors) # ALL authors (for the search field) all_authors = all_authors.fetch() num_total = len(all_authors) # Total number of elements self.session['page_name'] = "/authors" # Current page name self.session['num_elems_page'] = 10 # Amount of elements # Get ALL the author keys (they are necessary to do the order and filter) for author in authors: all_keys.append(author.key.urlsafe()) del author # 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 (authors) per page num_pages = num_total / num_elems_page # Number of pages authors = authors.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 author in authors: keys_page_list.append(author.key.urlsafe()) del author del num_elems_page, num_pages, current_number_page, pages # Delete variables to free memory # If num_total < 0 else: authors = all_authors # 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 "authors": authors, # Authors "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, # Authors keys that are currently in the page "all_keys": all_keys, # All author keys "all_authors": all_authors # ALL authors (for the search field) } del num_total, pages_to_show, keys_page_list, all_keys, lang, authors, all_authors # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/authors/default.html", **values)) # Go to the authors 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 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 author keys list authors = list() # Authors list aux3 = list() # All author keys support list all_authors = list() # ALL authors (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 authors (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 author keys list # Check if the authors 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 authors = Author.query() all_authors = copy.copy( authors) # ALL authors (for the search field) all_authors = all_authors.fetch() aux = list() for author in authors: # Get ALL the keys aux.append(author.key.urlsafe()) del author 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 authors (Limited to the number given by the session variable [10 by default]) # Not ordered because it has to respect if the user used some ordination or filter authors = Author.query(Author.key.IN(all_keys)).fetch( num_elems_page, offset=offset) num_total = len(aux_all_keys) # Total number of elements # Setting all the author 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 author in authors: keys_page_list.append(author.key.urlsafe()) del author # 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 "authors": authors, # Authors "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, # Authors keys that are currently in the page "all_keys": all_keys, # All author keys "all_authors": all_authors # ALL author (for the search field) } del pages_to_show, page_number, num_elems_page, num_total, offset, \ num_pages, current_number_page, pages, keys_page_list, authors, aux_all_keys, all_keys,\ all_authors # Delete variables to free memory self.session_store.save_sessions( self.response) # Save sessions self.response.write( jinja.render_template( "/authors/default.html", **values)) # Go to the authors home page # If it isn't less redirect to authors home page else: del pages_to_show, page_number, pages, num_elems_page, num_total, offset, authors, num_pages, \ aux_all_keys, all_authors # Delete variables to free memory self.redirect("/authors") # If the total number is not above 0, redirect to the authors home page else: del pages_to_show, page_number, pages, offset, authors, num_elems_page, aux_all_keys # Delete variables to free memory self.redirect("/authors") # If it isn't above redirect to authors home page else: del pages_to_show, page_number, pages # Delete variables to free memory self.redirect("/authors") # 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 author attributes name = self.request.get("name", "") # Author name name = name.encode("utf8") keys_page_list = self.request.get( "keys_page_list", "") # Author keys (only authors in the current page) aux_all_keys = self.request.get( "all_keys", "") # All the author keys (for the order field) # Initialize variables all_keys = list() # All the author keys (for the order field) all_authors = list() # All authors (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) authors = Author.query() all_authors = copy.copy( authors) # ALL authors (for the search field) all_authors = all_authors.fetch() for author in authors: # Get ALL the keys aux.append(author.key.urlsafe()) del author 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 authors authors = Author.query(Author.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # If empty else: authors = 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 "authors": authors, # Authors "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, # Author keys that are currently in the page "all_keys": all_keys, # All author keys "all_authors": all_authors # ALL author (for the search field) } # If the user enters all the attributes (the database can have more than one author with the same name) if len(name) > 0: # Add the new author to the home page author = Author(name=name) aux2 = author.put() time.sleep(1) # If the author was successfully added if aux2 is not None: # Adding the new author and its key to the lists aux3.append(author.key) all_keys.append(author.key.urlsafe()) all_authors.append(author) # 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 authors (Limited to the number given by the session variable [10 by default]) authors = Author.query(Author.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) # Setting the new keys list for the authors currently in the page (allows to use the "Delete page" button) for author in authors: keys_page_list.append(author.key.urlsafe()) del author num_total = len(all_authors) # 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 (authors) 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["authors"] = authors # Authors 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[ "author_added_successfully"] # Ok message (Author added successfully) values[ "keys_page_list"] = keys_page_list # Authors keys that are currently in the page values["all_keys"] = all_keys # All author keys values[ "all_authors"] = all_authors # ALL author (for the search field) del pages_to_show, pages, authors, num_total # Delete variables to free memory # If wasn't successfully added else: del author, 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_author_attributes"] # Error message (You must enter all author data) del lang, name, offset, keys_page_list, aux_all_keys, all_keys, all_authors, aux, aux3 # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/authors/default.html", **values)) # Go to the authors 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': # If it's logged in, get the session variables, show the home page # Get the author attributes name = self.request.get("name", "") name = name.encode("utf8") key = self.request.get("key", "") key = ndb.Key(urlsafe=key) author = key.get() # Get the author with that key keys_page_list = self.request.get( "keys_page_list", "") # Author keys (only authors in the current page) aux_all_keys = self.request.get( "all_keys", "") # All the author keys (for the order field) # Initialize variables aux = list() # Support variable aux3 = list() # Support variable all_authors = list( ) # List with all authors (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) authors = Author.query() for author2 in authors: # Get ALL the keys aux.append(author2.key.urlsafe()) del author2 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 authors offset = (self.session.get('current_number_page') - 1) * self.session.get('num_elems_page') authors = Author.query(Author.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 "authors": authors, # Authors "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, # Authors keys that are currently in the page "all_keys": all_keys, # All author keys "all_authors": all_authors # ALL author (for the search field) } # If the key is from an author if author and author is not None: # If there is some attribute to modify if len(name) > 0: old_name = author.name # Save the old author name for the modify method # Set the author attributes author.name = name aux2 = author.put() # If the modification was successful if aux2 is not None: # Modify the author in other db models (author must be modified in all db models that use it) self.modify_author_everywhere(old_name, author.name) # Variables to be sent to the HTML page values["ok_message"] = lang[ "author_modified_successfully"] # Ok message (Author modified successfully) # Get all db authors (Limited to the number given by the session variable [10 by default]) authors = Author.query(Author.key.IN(aux3)).fetch( self.session.get('num_elems_page'), offset=offset) values["authors"] = authors # 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, old_name # Delete variables to free memory # Else show an error message else: # Values to be sent to the HTML page values["error_message"] = lang[ "author_not_modified"] # Error message (Author couldn't be modified) all_authors = Author.query().fetch( ) # ALL authors (for the search field) values["all_authors"] = all_authors del lang, name, key, author, keys_page_list, aux_all_keys, aux, aux3, authors, \ all_authors, offset, all_keys # Delete variables to free memory self.session_store.save_sessions(self.response) # Save sessions self.response.write( jinja.render_template("/authors/default.html", **values)) # Go to the authors home page # If it isn't logged in, redirect to the login page else: self.redirect("/login")