# HG changeset patch # User Nicolas DURAND # Date 1419951176 -3600 # Node ID 4f08f1a107b3e6d98d09b0a4ef9d863bd9b8676b # Parent fc63b1a3d2ef140c9d57b7ac0fefbbcd793b3092 Cleaning the code (pep8, pylint) diff -r fc63b1a3d2ef -r 4f08f1a107b3 src/catedit/api.py --- a/src/catedit/api.py Mon Dec 29 18:01:09 2014 +0100 +++ b/src/catedit/api.py Tue Dec 30 15:52:56 2014 +0100 @@ -37,9 +37,8 @@ get the list of category """ cat_manager_instance = CategoryManager( - getattr(catedit.persistence, - app.config["PERSISTENCE_METHOD"])() - ) + getattr(catedit.persistence, app.config["PERSISTENCE_METHOD"])() + ) if cat_id is not None: cat = cat_manager_instance.load_cat(cat_id) return cat.cat_graph.serialize(format='turtle').decode("utf-8") @@ -62,8 +61,8 @@ getattr(catedit.persistence, app.config["PERSISTENCE_METHOD"])() ) args = cat_parser.parse_args() - if (cat_id is None and - cat_manager_instance.persistence.session_compliant is True): + if (cat_id is None and cat_manager_instance.persistence + .session_compliant is True): cat_manager_instance.save_cat(message=args["commit_message"]) else: new_property_list = [] @@ -123,9 +122,8 @@ description=args["description"], other_properties=property_list) cat_manager_instance = CategoryManager( - getattr(catedit.persistence, - app.config["PERSISTENCE_METHOD"])() - ) + getattr(catedit.persistence, app.config["PERSISTENCE_METHOD"])() + ) cat_manager_instance.save_cat(cat, message=args["commit_message"]) logger.debug("post id: "+cat.cat_id) cache.clear() @@ -138,9 +136,8 @@ """ args = cat_parser.parse_args() cat_manager_instance = CategoryManager( - getattr(catedit.persistence, - app.config["PERSISTENCE_METHOD"])() - ) + getattr(catedit.persistence, app.config["PERSISTENCE_METHOD"])() + ) if args["delete_message"] is None: message = "Deleting category "+cat_id else: diff -r fc63b1a3d2ef -r 4f08f1a107b3 src/catedit/models.py --- a/src/catedit/models.py Mon Dec 29 18:01:09 2014 +0100 +++ b/src/catedit/models.py Tue Dec 30 15:52:56 2014 +0100 @@ -6,7 +6,6 @@ """ from rdflib import Graph, RDF, RDFS, Literal, URIRef -from rdflib.namespace import SKOS from flask import session from uuid import uuid4 from io import StringIO @@ -146,9 +145,9 @@ Literal(new_description))) logger.debug("current properties: " - +str(self.properties) - +" new properties: " - +str(new_other_properties)) + + str(self.properties) + + " new properties: " + + str(new_other_properties)) if new_other_properties is not None and \ (new_other_properties != self.properties): @@ -224,18 +223,18 @@ cat_list = self.list_cat() # first we edit what was already modified before the deletion logger.debug(session["modified_categories"]) - element_list = list(session["modified_categories"]) - if deleted_cat_id in [element["name"] for \ - element in session["deleted_categories"]]: + element_list = list(session.get("modified_categories", [])) + if deleted_cat_id in [element["name"] for + element in session.get("deleted_categories", [])]: for element in element_list: logger.debug(str(element)) modified_cat_graph = Graph() modified_cat_graph.parse(source=StringIO(element["content"]), format="turtle") modified_cat = Category(graph=modified_cat_graph) - if (modified_cat.cat_id != app.config["CATEGORY_NAMESPACE"] - +deleted_cat_id): - new_property_list_for_modified_cat = [] + if (modified_cat.cat_id != + app.config["CATEGORY_NAMESPACE"] + deleted_cat_id): + new_property_list = [] for (predicate, obj) in modified_cat.properties: if not ( (app.config["PROPERTY_LIST"] @@ -245,15 +244,12 @@ (obj == (app.config["CATEGORY_NAMESPACE"] + deleted_cat_id)) ): - new_property_list_for_modified_cat.append( - (predicate, obj) - ) - if (new_property_list_for_modified_cat - != modified_cat.properties): + new_property_list.append((predicate, obj)) + + if new_property_list != modified_cat.properties: logger.debug("Modifying modified category") modified_cat.edit_category( - new_other_properties=\ - new_property_list_for_modified_cat + new_other_properties=new_property_list ) self.save_cat( modified_cat, @@ -262,11 +258,12 @@ # now we check if an unmodified category reference the deleted # category for cat in cat_list: - if cat.cat_id not in [element["name"] for \ - element in session["modified_categories"]] and \ - cat.cat_id not in [element["name"] for \ - element in session["deleted_categories"]]: - new_property_list_for_cat = [] + if cat.cat_id not in [element["name"] for element in + session.get("modified_categories", [])] \ + and \ + cat.cat_id not in [element["name"] for element in + session.get("deleted_categories", [])]: + new_property_list = [] for (predicate, obj) in cat.properties: if not ( (app.config["PROPERTY_LIST"] @@ -276,18 +273,18 @@ (obj == (app.config["CATEGORY_NAMESPACE"] + deleted_cat_id)) ): - new_property_list_for_cat.append((predicate, obj)) - if(new_property_list_for_cat!=cat.properties): + new_property_list.append((predicate, obj)) + + if new_property_list != cat.properties: logger.debug("Modifying untouched category") cat.edit_category( - new_other_properties=new_property_list_for_cat + new_other_properties=new_property_list ) self.save_cat( cat, message=message+", cleaning up other properties" ) - def list_cat(self): """ Lists all categories available diff -r fc63b1a3d2ef -r 4f08f1a107b3 src/catedit/persistence.py --- a/src/catedit/persistence.py Mon Dec 29 18:01:09 2014 +0100 +++ b/src/catedit/persistence.py Tue Dec 30 15:52:56 2014 +0100 @@ -10,7 +10,7 @@ """ from abc import ABCMeta, abstractmethod from catedit import app, github -from base64 import b64encode, b64decode +from base64 import b64decode from flask import session from flask.ext.github import GitHubError import os @@ -90,7 +90,6 @@ As each modification is submitted, this is where we save to a file """ - def save(self, **kwargs): """ Saves to a file @@ -215,39 +214,42 @@ if element["type"] == "blob": # test if element is in deleted categories, if it is, # no point doing anything, the file won't be in the new tree - if not(element["path"] in [(app.config["CATEGORIES_PATH"] - + category["name"]) - for category in - session["deleted_categories"]]): + if not( + element["path"] in [ + (app.config["CATEGORIES_PATH"] + category["name"]) + for category in session.get(["deleted_categories"], []) + ] + ): # the element is either modified or untouched so in both # case we'll need a blob sha blob_sha = "" # test if element is in modified categories - if element["path"] in [(app.config["CATEGORIES_PATH"] - + category["name"]) - for category in - session["modified_categories"]]: + if ( + element["path"] in [ + (app.config["CATEGORIES_PATH"] + category["name"]) + for category in + session.get(["modified_categories"], []) + ] + ): # find element in modified categories for category in session["modified_categories"]: if element["path"] == ( - app.config["CATEGORIES_PATH"] - + category["name"] - ): + app.config["CATEGORIES_PATH"] + category["name"] + ): # 4-1 for edited files new_blob_data = { - "content": category["content"], - "encoding": "utf-8" - } + "content": category["content"], + "encoding": "utf-8" + } new_blob = github.post( - "repos/" - + app.config["REPOSITORY_OWNER"] - +"/" - + app.config["REPOSITORY_NAME"] - + "/git/blobs", - data=new_blob_data - ) + "repos/" + + app.config["REPOSITORY_OWNER"] + "/" + + app.config["REPOSITORY_NAME"] + + "/git/blobs", + data=new_blob_data + ) blob_sha = new_blob["sha"] break @@ -257,31 +259,31 @@ new_tree_data["tree"].append({"path": element["path"], "mode": element["mode"], "type": "blob", - "sha": blob_sha }) + "sha": blob_sha}) logger.debug(str(new_tree_data["tree"])) # Now we'll add new files in the tree - for category in session["modified_categories"]: + for category in session.get("modified_categories", []): logger.debug(app.config["CATEGORIES_PATH"]+category["name"] + " should not be in " - + str([ file["path"] for - file in last_commit_tree["tree"]])) + + str([elt["path"] for + elt in last_commit_tree["tree"]])) if (app.config["CATEGORIES_PATH"]+category["name"] not in - [ file["path"] for file in last_commit_tree["tree"]]): + [file["path"] for file in last_commit_tree["tree"]]): # 4-1 for added files - new_blob_data={"content": category["content"], - "encoding": "utf-8"} + new_blob_data = {"content": category["content"], + "encoding": "utf-8"} new_blob = github.post("repos/" - + app.config["REPOSITORY_OWNER"]+"/" + + app.config["REPOSITORY_OWNER"] + "/" + app.config["REPOSITORY_NAME"] + "/git/blobs", data=new_blob_data) new_tree_data["tree"].append({ - "path": app.config["CATEGORIES_PATH"] - + category["name"], - "mode": "100644", - "type": "blob", - "sha": new_blob["sha"] }) + "path": app.config["CATEGORIES_PATH"] + category["name"], + "mode": "100644", + "type": "blob", + "sha": new_blob["sha"] + }) logger.debug(str(new_tree_data)) # Finally, we post the new tree @@ -323,17 +325,18 @@ * name: the name of the file to save * content: the content of the file to save """ - session["modified_categories"][:] = [elt \ - for elt in session["modified_categories"] \ - if elt["name"] != kwargs["name"]] + session["modified_categories"][:] = [ + elt for elt in session.get("modified_categories", []) + if elt["name"] != kwargs["name"] + ] session["modified_categories"].append( {"name": kwargs["name"], - "content": str(kwargs["content"],"utf-8")} + "content": str(kwargs["content"], "utf-8")} ) # Now we must clean the deleted categories list in case the modified # category was deleted before being edited - for element in session["deleted_categories"]: + for element in session.get("deleted_categories", []): if element["name"] == kwargs["name"]: session["deleted_categories"].remove(element) @@ -362,8 +365,9 @@ Calling delete for a file already deleted will restore it (by deleting it from the delete files list) """ - if (kwargs["name"] in [element["name"] for \ - element in session["deleted_categories"]]): + if (kwargs["name"] in [ + element["name"] for element in session.get("deleted_categories", []) + ]): session["deleted_categories"].remove({"name": kwargs["name"]}) # warning, not safe if 2 files share the same name (or category id) # but that shouldn't happen @@ -371,11 +375,10 @@ session["deleted_categories"].append({"name": kwargs["name"]}) # now we must clean the modified categories list in case the # deleted category was modified before - for element in session["modified_categories"]: - if element["name"]==kwargs["name"]: + for element in session.get("modified_categories", []): + if element["name"] == kwargs["name"]: session["modified_categories"].remove(element) - def list(self, **kwargs): """ Lists all files in the github repository (as set in config.py) @@ -412,51 +415,5 @@ except GitHubError as ghe: logger.debug("Github Error trying to get file: "+filename) logger.debug(ghe.response.text) - #logger.debug(file_content_list) + # logger.debug(file_content_list) return file_content_list - - -class PersistenceToGist(Persistence): - """ - This persistence class saves to Github gists - - It uses the Flask session object and an optional param in the save - method to submit a set of changes instead of one change at a time - - Expected kwargs for saving to/loading from a Github repository are: - * name : name of the file to write in/read from - * content : desired content of the file when writing - * message : when saving and/or deleting, commit message to be saved - to Github - """ - @property - def session_compliant(self): - """ - Session compliant: each modification is stored in flask session - object and every stacked changes can be submitted at once - """ - return True - - def save(self, **kwargs): - """ - TODO : Docstring - """ - return - - def delete(self, **kwargs): - """ - TODO : Docstring - """ - return - - def load(self, **kwargs): - """ - TODO : Docstring - """ - return - - def list(self, **kwargs): - """ - TODO : Docstring - """ - return diff -r fc63b1a3d2ef -r 4f08f1a107b3 src/catedit/views.py --- a/src/catedit/views.py Mon Dec 29 18:01:09 2014 +0100 +++ b/src/catedit/views.py Tue Dec 30 15:52:56 2014 +0100 @@ -32,6 +32,7 @@ validators=[DataRequired()] ) + class CommitForm(Form): """ Custom form class for commiting changes @@ -41,6 +42,7 @@ validators=[DataRequired()] ) + @app.route('/', methods=['GET']) @app.route('/catrecap', methods=['GET']) def cat_recap(): @@ -80,18 +82,19 @@ """ cat_api_instance = CategoryAPI() cat_list = [] - deleted_cat_list=[] - if (session.get("user_logged",None) is not None): + deleted_cat_list = [] + serialized_cat_list = [] + if session.get("user_logged", None) is not None: serialized_cat_list = cat_api_instance.get() - deleted_cat_list = [element["name"] for \ - element in session["deleted_categories"]] + deleted_cat_list = [element["name"] for + element in session.get("deleted_categories", [])] for serialized_cat in serialized_cat_list: cat_rdf_graph = Graph() cat_rdf_graph.parse(source=StringIO(serialized_cat), format='turtle') cat = Category(graph=cat_rdf_graph) - if cat.cat_id not in [element["name"] for element \ - in session["deleted_categories"]]: + if cat.cat_id not in [element["name"] for element + in session.get("deleted_categories", [])]: cat_list.append({"cat_label": cat.label, "cat_description": cat.description, "cat_id": cat.cat_id, @@ -132,11 +135,11 @@ # PUT + cat_id = Submit edited cat, only if cat is not already # in deleted categories - if cat_form.validate_on_submit() and \ - cat.cat_id not in [element["name"] for element \ - in session["deleted_categories"]]: + if cat_form.validate_on_submit() and cat.cat_id not in \ + [element["name"] for element in + session.get("deleted_categories", [])]: if (session.get("user_logged", None) is not None and - session.get("user_can_edit", False) is not False): + session.get("user_can_edit", False) is not False): cat_api_instance.put(cat_id) return redirect(url_for('cat_modifs')) else: @@ -169,7 +172,7 @@ # POST seul = Submit created cat if cat_form.validate_on_submit(): if (session.get("user_logged", None) is not None and - session.get("user_can_edit", False) is not False): + session.get("user_can_edit", False) is not False): cat_api_instance.post() return redirect(url_for('cat_modifs')) else: @@ -178,6 +181,7 @@ cat_list=cat_list, deleted_cat_list=deleted_cat_list) + @app.route('/catrecap/delete-modifs-', defaults={'delete_cat_id': None}, methods=['POST']) @@ -188,6 +192,11 @@ defaults={'delete_cat_id': None, 'delete_modifs_id': None}, methods=['GET', 'POST']) def cat_modifs(delete_cat_id, delete_modifs_id): + """ + View that handles all the visualisation of changes for a user's + session, links to the editor forms, allows the users to cancel their + own changes and submits all their changes. + """ cat_list = [] modified_cat_list = [] deleted_cat_list = [] @@ -195,9 +204,14 @@ cat_api_instance = CategoryAPI() if delete_cat_id is None and delete_modifs_id is None: commit_form = CommitForm(request.form) - if request.method=="GET": + + # if it's a GET with no delete_cat_id or deleted_modifs_id, then we'll + # display the changes + if request.method == "GET": if session.get("user_logged", None) is not None: serialized_cat_list = cat_api_instance.get() + + # Creating existing cat list for serialized_cat in serialized_cat_list: cat_rdf_graph = Graph() cat_rdf_graph.parse(source=StringIO(serialized_cat), @@ -208,7 +222,10 @@ "cat_description": cat.description, "cat_id": cat.cat_id, "cat_properties": cat.properties}) - for modified_category in session["modified_categories"]: + + # Creating modified and created cat lists + for modified_category in \ + session.get("modified_categories", []): modified_cat_rdf_graph = Graph() modified_cat_rdf_graph.parse( source=StringIO( @@ -217,51 +234,60 @@ format='turtle' ) modified_cat = Category(graph=modified_cat_rdf_graph) - if modified_cat.cat_id in [existing_cat["cat_id"] \ - for existing_cat in cat_list]: + if modified_cat.cat_id in \ + [existing_cat["cat_id"] + for existing_cat in cat_list]: modified_cat_list.append( {"cat_label": modified_cat.label, "cat_description": modified_cat.description, "cat_id": modified_cat.cat_id, - "cat_properties": modified_cat.properties} - ) + "cat_properties": modified_cat.properties}) else: created_cat_list.append( {"cat_label": modified_cat.label, "cat_description": modified_cat.description, "cat_id": modified_cat.cat_id, - "cat_properties": modified_cat.properties} - ) - deleted_cat_list = [element["name"] for element \ - in session["deleted_categories"]] + "cat_properties": modified_cat.properties}) + + # Creating deleted cat list + deleted_cat_list = [element["name"] for element in + session.get("deleted_categories", [])] return render_template('catmodifs.html', cat_list=cat_list, created_cat_list=created_cat_list, modified_cat_list=modified_cat_list, deleted_cat_list=deleted_cat_list, commit_form=commit_form) - elif request.method=="POST": + # If it's a POST with no delete_cat_id and delete_modifs_id, then we + # will submit all the stored changes + elif request.method == "POST": logger.debug("Submitting changes") if commit_form.validate_on_submit(): logger.debug("Form validates") cat_api_instance.put() return redirect(url_for('cat_modifs')) + + # One of the ids is not None (either deleting a category or a modification) else: - if request.method=="POST": + # We only do that if we have a POST + if request.method == "POST": + # We identify if we want to delete a change if delete_modifs_id is not None: - for element in session["modified_categories"]: + for element in session.get("modified_categories", []): if element["name"] == delete_modifs_id: logger.debug("Modification for " + delete_modifs_id + " will be deleted.") session["modified_categories"].remove(element) + # We identify if we want to delete a category if delete_cat_id is not None: logger.debug("Category "+delete_cat_id+" will be deleted.") if (session.get("user_logged", None) is not None and - session.get("user_can_edit", False) is not False): + session.get("user_can_edit", False) is not False): cat_api_instance.delete(delete_cat_id) return redirect(url_for('cat_modifs')) + @app.route('/catedit-github-login') def github_login(): """