src/catedit/api.py
changeset 18 37981f4fdb77
parent 17 2db9202ad7cf
--- a/src/catedit/api.py	Wed Dec 31 15:28:40 2014 +0100
+++ b/src/catedit/api.py	Fri Jan 02 10:56:43 2015 +0100
@@ -74,8 +74,6 @@
                     modified_cat_list = session.get("modified_categories", []),
                     message=args["commit_message"]
                 )
-            session["deleted_categories"] = []
-            session["modified_categories"] = []
         else:
             new_property_list = []
             logger.debug(args["property_predicate"])
@@ -98,7 +96,21 @@
                         new_property_list.append((property_predicate,
                                                   property_object_to_append))
             logger.debug(new_property_list)
-            cat = cat_manager_instance.load_category(cat_id)
+
+            # is the edition occuring on an already modified category?
+            if cat_id in [category["name"] for category
+                          in session.get("modified_categories", [])]:
+                for element in session.get("modified_categories", []):
+                    if element["name"] == cat_id:
+                        cat_graph = Graph()
+                        cat_graph.parse(
+                            source=StringIO(element["content"]),
+                            format="turtle"
+                        )
+                        cat = Category(graph=cat_graph)
+            else:
+                cat = cat_manager_instance.load_category(cat_id)
+
             cat.edit_category(new_description=args["description"],
                               new_label=args["label"],
                               new_other_properties=new_property_list)
@@ -169,7 +181,8 @@
     @classmethod
     def delete(cls, deleted_cat_id):
         """
-            API to delete the category of id cat_id from the deletion list
+            API to delete the category of id cat_id or restore it from the
+            deletion list
         """
         args = cat_parser.parse_args()
         if (deleted_cat_id in [
@@ -198,12 +211,15 @@
             logger.debug(session["modified_categories"])
             element_list = list(session.get("modified_categories", []))
             if deleted_cat_id in [element["name"] for
-                                  element in session.get("deleted_categories", [])]:
+                                  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_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):
@@ -283,7 +299,52 @@
         cache.clear()
         return 204
 
+class CategoryChangesAPI(Resource):
+    """
+        API for getting and deleting category changes, returns a dict when
+        succesful if category is a modified one, returns only the cat_id if it
+        is a deleted one
+
+        All changes and deletions are saved in session["modified_categories"]
+        and session["deleted_categories"]
+    """
+    @classmethod
+    def get(cls, modified_cat_id=None):
+        """
+            API to get the pending changes for category cat_id
+        """
+        logger.debug(modified_cat_id)
+        logger.debug(session.get("modified_categories", []))
+        if modified_cat_id is None:
+            return {
+                "modified_categories": session.get("modified_categories", []),
+                "deleted_categories": session.get("deleted_categories", [])
+            }, 201
+        else:
+            for category in session.get("modified_categories", []):
+                logger.debug(category)
+                if category["name"] == modified_cat_id:
+                    return { "type": "modified", "category": category }, 201
+            for category in session.get("deleted_categories", []):
+                logger.debug(category)
+                if category["name"] == modified_cat_id:
+                    return { "type": "deleted", "category": category }, 201
+            return 404
+
+    def delete(cls, cat_id=None):
+        """
+            API to delete the category cat_id from the changelist or if cat_id
+            is None, delete the whole changelist
+        """
+        session["modified_categories"] = []
+        session["deleted_categories"] = []
+        return 204
+
 api.add_resource(CategoryAPI,
                  '/category/<string:cat_id>',
                  '/category',
                  endpoint='category')
+api.add_resource(CategoryChangesAPI,
+                 '/category-changes/<string:cat_id>',
+                 '/category-changes',
+                 endpoint='category_changes')