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 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 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) # 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 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")