src/catedit/resources.py
author durandn
Mon, 13 Apr 2015 12:03:48 +0200
changeset 95 0a131f9b92a5
parent 94 71a4a7300c35
child 97 fe8782a67fcf
permissions -rw-r--r--
Properties are now cached globally instead of being stored in the session
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
"""
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
     2
resources.py:
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
contains the api that links the views (views.py) to the model (models.py) and
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
its persistence method (persistence.py). As it only trades rdf graphs
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
serializations strings, it isn't bound to one specific view
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
"""
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
45
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
     9
from catedit import app, cache, github
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from catedit.models import Category, CategoryManager
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
import catedit.persistence
72
75170fe4d56b Started work on task management for submitting changes
durandn
parents: 62
diff changeset
    12
from catedit.tasks import submit_changes
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
    13
from catedit.utils import get_property_list
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
from io import StringIO
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
    16
from flask import session
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
    17
from rdflib import Graph, URIRef, Literal
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
    18
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
    19
from flask.ext.restful import Resource, reqparse
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
    20
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
    21
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
logger = app.logger
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
cat_parser = reqparse.RequestParser()
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
cat_parser.add_argument('label', type=str)
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
cat_parser.add_argument('description', type=str)
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
cat_parser.add_argument('property_predicate', type=str, action="append")
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
cat_parser.add_argument('property_object', type=str, action="append")
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
23
877c3b66313a Removed references to REPOSITORY_NAME + some refactoring of cat_editor in views.py (wtforms)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 22
diff changeset
    30
cat_parser.add_argument('commit_message', type=str)
877c3b66313a Removed references to REPOSITORY_NAME + some refactoring of cat_editor in views.py (wtforms)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 22
diff changeset
    31
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
class CategoryAPI(Resource):
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    """
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        The API to create and edit categories, returns rdf graph serializations
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        when successful
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    """
45
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    38
    def get(self, repository, cat_id=""):
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        """
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
            API to get the category of id cat_id, or if cat_id is None,
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
            get the list of category
45
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    42
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    43
            The result of this API function goes into the Flask Cache.
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        """
45
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    45
        cache_key = "categoryapi_get_" + repository + "_" + cat_id
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    46
        if cache.get(cache_key) is None:
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    47
            rv = None
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    48
            cat_manager_instance = CategoryManager(
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    49
                getattr(
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    50
                    catedit.persistence,
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    51
                    app.config["PERSISTENCE_CONFIG"]["METHOD"]
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    52
                )(repository=repository),
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    53
            )
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    54
            if cat_id != "":
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    55
                cat = cat_manager_instance.load_category(cat_id)
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    56
                if cat is not None:
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    57
                    rv = cat.cat_graph.serialize(
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    58
                        format='turtle'
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    59
                    ).decode("utf-8"), 200
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    60
                else:
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    61
                    rv = 404
40
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
    62
            else:
45
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    63
                response = []
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    64
                for cat in cat_manager_instance.list_categories():
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    65
                    response.append(
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    66
                        cat.cat_graph.serialize(format='turtle').decode("utf-8")
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    67
                    )
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    68
                rv = response, 200
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    69
            cache.set(cache_key, rv, timeout=3600)
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    70
            return rv
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        else:
45
1506da593f40 Caching fix (now works on views.categories submodule) and config (dict CACHE_CONFIG, see Flask-cache doc for expected values) + Pagination for comments (changeset & issues) + Hooks to log API rate consumption and get pagination info
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 43
diff changeset
    72
            return cache.get(cache_key)
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    # update category cat_id
38
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    75
    def put(self, repository, cat_id=None, cat_data=None):
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        """
23
877c3b66313a Removed references to REPOSITORY_NAME + some refactoring of cat_editor in views.py (wtforms)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 22
diff changeset
    77
            API to edit an existing category
38
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    78
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    79
            Args are:
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    80
            * repository: the repository where the category cat_id is stored
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    81
            * cat_id : the id of the category to edit
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    82
            * cat_data : the new data of the category, dict of the form:
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    83
            {
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    84
                "label": edited_cat_label,
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    85
                "description": edited_cat_description,
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    86
                "properties": [(predicate1, object1), (predicate2, object2)]
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    87
            }
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    88
            List of predicate is available in config.py, key PROPERTY_LIST
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    89
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    90
            Note: If cat_id is None and persistence support change sets, will
23
877c3b66313a Removed references to REPOSITORY_NAME + some refactoring of cat_editor in views.py (wtforms)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 22
diff changeset
    91
            submit all changes to category list
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        """
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        cat_manager_instance = CategoryManager(
21
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
    94
            getattr(
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
    95
                catedit.persistence,
38
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
    96
                app.config["PERSISTENCE_CONFIG"]["METHOD"]
21
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
    97
            )(repository=repository),
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
        )
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        args = cat_parser.parse_args()
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   100
        if cat_id is None:
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   101
            if cat_manager_instance.persistence.session_compliant is True:
86
1c84b37adaf4 Start on routing and queues, now need 1 worker per queue
durandn
parents: 73
diff changeset
   102
                task=submit_changes.apply_async(
1c84b37adaf4 Start on routing and queues, now need 1 worker per queue
durandn
parents: 73
diff changeset
   103
                    kwargs={
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   104
                        "deleted_categories" : session.setdefault(
42
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   105
                            "deleted_categories", {}
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   106
                        ).setdefault(repository, {}),
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   107
                        "modified_categories" : session.setdefault(
42
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   108
                            "modified_categories", {}
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   109
                        ).setdefault(repository, {}),
86
1c84b37adaf4 Start on routing and queues, now need 1 worker per queue
durandn
parents: 73
diff changeset
   110
                        "message" : args["commit_message"],
1c84b37adaf4 Start on routing and queues, now need 1 worker per queue
durandn
parents: 73
diff changeset
   111
                        "repository" : repository,
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   112
                        "token" : session.get("user_code")
86
1c84b37adaf4 Start on routing and queues, now need 1 worker per queue
durandn
parents: 73
diff changeset
   113
                    },
1c84b37adaf4 Start on routing and queues, now need 1 worker per queue
durandn
parents: 73
diff changeset
   114
                    queue="repo_"+repository,
1c84b37adaf4 Start on routing and queues, now need 1 worker per queue
durandn
parents: 73
diff changeset
   115
                    routing_key="task_for_"+repository
1c84b37adaf4 Start on routing and queues, now need 1 worker per queue
durandn
parents: 73
diff changeset
   116
                )
1c84b37adaf4 Start on routing and queues, now need 1 worker per queue
durandn
parents: 73
diff changeset
   117
                            
87
e7afb5bd5a85 clean log + session["tasks"] error
durandn
parents: 86
diff changeset
   118
                session.setdefault("tasks", {}).setdefault(repository, []).append(task.id)
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   119
                session.setdefault("deleted_categories", {})[repository] = {}
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   120
                session.setdefault("modified_categories", {})[repository] = {}
40
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   121
                cache.clear()
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   122
                return 204
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        else:
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
            # is the edition occuring on an already modified category?
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   125
            if (cat_id in session.setdefault(
42
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   126
                    "modified_categories", {}
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   127
            ).setdefault(repository, {}).keys()):
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   128
                cat_graph = Graph()
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   129
                cat_graph.parse(
42
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   130
                    source=StringIO(
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   131
                        session["modified_categories"][repository][cat_id]
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   132
                    ),
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   133
                    format="turtle"
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   134
                )
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   135
                cat = Category(graph=cat_graph)
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
            else:
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
                cat = cat_manager_instance.load_category(cat_id)
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   138
            
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   139
            new_property_list=[]
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   140
            for (predicate, obj) in cat_data["properties"]:
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   141
                if get_property_list()[repository].get(predicate, {}).get("object_type") == "uriref-category":
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   142
                    new_property_list.append(
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   143
                        (URIRef(get_property_list()[repository][predicate]["rdflib_class"]), app.config["CATEGORY_NAMESPACE"][obj])
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   144
                    )
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   145
                    new_property_list.append(
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   146
                        (URIRef(get_property_list()[repository][predicate]["rdflib_class"]), URIRef(obj))
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   147
                    )
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   148
                else:
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   149
                    new_property_list.append(
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   150
                        (URIRef(get_property_list()[repository][predicate]["rdflib_class"]), Literal(obj))
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   151
                    )
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   152
            
38
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   153
            cat.edit_category(new_description=cat_data["description"],
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   154
                              new_label=cat_data["label"],
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   155
                              new_other_properties=new_property_list)
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   157
            session.setdefault("modified_categories", {}).setdefault(repository, {})[cat.cat_id] = str(
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   158
                cat.cat_graph.serialize(format="turtle"), "utf-8"
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
            )
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
            # Now we must clean the deleted categories list in case the
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
            # modified category was deleted before being edited
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   163
            session.setdefault("deleted_categories", {})[repository] = {
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   164
                cat_name: cat_content for cat_name, cat_content
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   165
                in session.setdefault("deleted_categories", {}).setdefault(repository, {}).items()
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   166
                if cat_name != cat.cat_id
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   167
            }
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
            logger.debug("put id: "+cat.cat_id)
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
        return 204
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
38
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   171
    def post(self, repository, cat_data):
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
        """
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
            API to create a new category
38
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   174
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   175
            Args are:
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   176
            * repository: the repository where the category cat_id is stored
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   177
            * cat_data : the new data of the category, dict of the form:
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   178
            {
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   179
                "label": edited_cat_label,
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   180
                "description": edited_cat_description,
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   181
                "properties": [(predicate1, object1), (predicate2, object2)]
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   182
            }
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   183
            List of predicate is available in config.py, key PROPERTY_LIST
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
        """
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   185
        property_list = []
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   186
        for (predicate, obj) in cat_data["properties"]:
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   187
            if get_property_list()[repository].get(predicate, {}).get("object_type", "") == "uriref-category": # faire des get
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   188
                property_list.append(
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   189
                    (URIRef(get_property_list()[repository][predicate]["rdflib_class"]), app.config["CATEGORY_NAMESPACE"][obj])
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   190
                )
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   191
            elif get_property_list()[repository].get(predicate, {}).get("object_type", "") == "uriref-link":
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   192
                property_list.append(
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   193
                    (URIRef(get_property_list()[repository][predicate]["rdflib_class"]), URIRef(obj))
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   194
                )
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   195
            else:
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   196
                property_list.append(
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   197
                    (URIRef(get_property_list()[repository][predicate]["rdflib_class"]), Literal(obj))
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   198
                )
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   199
        
38
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   200
        cat = Category(
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   201
            label=cat_data["label"],
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   202
            description=cat_data["description"],
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   203
            other_properties=property_list
38
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   204
        )
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   205
        
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   206
        
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   207
        
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   208
        if cat.cat_id not in session.setdefault("modified_categories", {}).setdefault(repository, {}):
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   209
            session.setdefault("modified_categories", {}).setdefault(repository, {})[cat.cat_id] = str(
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
                cat.cat_graph.serialize(format="turtle"), "utf-8"
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   211
            )
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
        logger.debug("post id: "+cat.cat_id)
40
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   214
        return cat.cat_id, \
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   215
               cat.cat_graph.serialize(format='turtle').decode("utf-8"), \
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   216
               201
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
21
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
   218
    def delete(self, repository, deleted_cat_id):
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
        """
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
            API to delete the category of id cat_id or restore it from the
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
            deletion list
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
        """
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   223
        # if cat_id is already in deleted categories, we restore it
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   224
        if deleted_cat_id in session.setdefault("deleted_categories", {}).setdefault(repository, {}):
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   225
            session.setdefault("deleted_categories", {})[repository] = {
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   226
                cat_name: cat_content for cat_name, cat_content
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   227
                in session["deleted_categories"][repository].items()
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   228
                if cat_name != deleted_cat_id
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   229
            }
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
            # warning, not safe if 2 files share the same name (or category id)
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
            # but that shouldn't happen
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
        else:
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   233
            session.setdefault("deleted_categories", {}).setdefault(repository, {})[deleted_cat_id] = "deleted"
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   234
            # Maybe we can store something more useful in there, such as
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   235
            # category content when deleted to restore last edit?
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   236
            # It would be nice to have symetry between modified and
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   237
            # deleted dicts
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   238
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
            # now we must clean the modified categories list in case the
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
            # deleted category was modified before
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   241
            session.setdefault("modified_categories", {})[repository] = {
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   242
                cat_name: cat_content for cat_name, cat_content
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   243
                in session["modified_categories"][repository].items()
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   244
                if cat_name != deleted_cat_id
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   245
            }
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   246
            logger.debug(str(
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   247
                {
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   248
                    cat_name: cat_content for cat_name, cat_content
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   249
                    in session["modified_categories"][repository].items()
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   250
                    if cat_name != deleted_cat_id
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   251
                }
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   252
            ))
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
            # Now we also have to clean up categories that reference the
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
            # deleted category
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
            cat_manager_instance = CategoryManager(
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
                getattr(
21
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
   257
                    catedit.persistence,
38
d1bc73ce855a Finished WTForms support for properties + Refactored API so it doesn't handle form parsing anymore + added version.py and edited setup.py, version is now displayed in the footer of every page + reworked config to condense every persistence parameter and logging parameter
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 25
diff changeset
   258
                    app.config["PERSISTENCE_CONFIG"]["METHOD"]
21
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
   259
                )(repository=repository),
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
            )
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
            cat_list = cat_manager_instance.list_categories()
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
            # first we edit what was already modified before the deletion
42
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   263
            logger.debug(
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   264
                session.setdefault("modified_categories", {}).setdefault(repository, {})
42
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   265
            )
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   266
            if (
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   267
                    deleted_cat_id in session.setdefault("deleted_categories", {}).setdefault(repository, {})
42
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   268
            ):
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   269
                for element in session.setdefault("modified_categories", {}) \
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   270
                                      .setdefault(repository, {}):
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
                    logger.debug(str(element))
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
                    modified_cat_graph = Graph()
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
                    modified_cat_graph.parse(
42
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   274
                        source=StringIO(
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   275
                            session["modified_categories"][repository][element]
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   276
                        ),
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
                        format="turtle"
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
                    )
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
                    modified_cat = Category(graph=modified_cat_graph)
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   280
                    if (modified_cat.cat_id != app.config["CATEGORY_NAMESPACE"][deleted_cat_id]):
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
                        new_property_list = []
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
                        for (predicate, obj) in modified_cat.properties:
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
                            if not (
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   284
                                    get_property_list()[repository]
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   285
                                                       [predicate.toPython()]
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   286
                                                       ["object_type"]
42
dac9b1248e0f V0.1.4: Implemented github social functions into CatEdit: refactored template and views hierarchy, names, views submodule, created macro to generate category tables + small esthetic changes (buttons to display categories/tables switch state, possibility to hide tables) + views modules have been converted into Flask Blueprints
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 40
diff changeset
   287
                                    == "uriref-category"
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   288
                                    and (obj == app.config["CATEGORY_NAMESPACE"][deleted_cat_id])
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
                            ):
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
                                new_property_list.append((predicate, obj))
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
                        if new_property_list != modified_cat.properties:
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
                            logger.debug("Modifying modified category")
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
                            modified_cat.edit_category(
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
                                new_other_properties=new_property_list
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
                            )
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   297
                            session["modified_categories"] \
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   298
                                   [repository] \
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   299
                                   [modified_cat.cat_id] = str(
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   300
                                modified_cat.cat_graph
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   301
                                            .serialize(format="turtle"),
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   302
                                "utf-8"
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
                            )
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   304
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
                # now we check if an unmodified category reference the deleted
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
                # category
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
                for cat in cat_list:
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   308
                    if (cat.cat_id not in (
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   309
                            list(session.setdefault("modified_categories", {}).setdefault(repository, {}).keys())
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   310
                            + list(session.setdefault("deleted_categories", {}).setdefault(repository, {}).keys())
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   311
                    )):
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
                        new_property_list = []
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
                        for (predicate, obj) in cat.properties:
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
                            if not (
95
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   315
                                    get_property_list()[repository]
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   316
                                                       [predicate.toPython()]
0a131f9b92a5 Properties are now cached globally instead of being stored in the session
durandn
parents: 94
diff changeset
   317
                                                       ["object_type"]
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   318
                                    == "uriref-category"
62
746d486f7c42 Added support for custom properties per repository + added doc in the readme for this (config.py PROPERTY_LIST entry has changed a bit)
durandn
parents: 45
diff changeset
   319
                                    and (obj == app.config["CATEGORY_NAMESPACE"][deleted_cat_id])
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
                            ):
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
                                new_property_list.append((predicate, obj))
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
                        if new_property_list != cat.properties:
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
                            logger.debug("Modifying untouched category")
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
                            cat.edit_category(
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
                                new_other_properties=new_property_list
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
                            )
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   328
                            session.setdefault("modified_categories", {}).setdefault(repository, {})[cat.cat_id] = str(
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   329
                                cat.cat_graph.serialize(format="turtle"),
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   330
                                "utf-8"
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
                            )
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
        logger.debug("delete id: " + deleted_cat_id)
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
        return 204
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   336
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
class CategoryChangesAPI(Resource):
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
    """
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
        API for getting and deleting category changes, returns a dict when
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
        succesful if category is a modified one, returns only the cat_id if it
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
        is a deleted one
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   343
        All changes and deletions are saved in
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   344
        session["modified_categories"][repository] and
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   345
        session["deleted_categories"][repository] under the format:
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   346
        {changed_cat_id: content_of_changed_cat}
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
    """
21
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
   348
    def get(self, repository, modified_cat_id=None):
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
        """
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
            API to get the pending changes for category cat_id
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
        """
40
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   352
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
        if modified_cat_id is None:
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
            return {
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   355
                "modified_categories": session.setdefault("modified_categories", {})
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   356
                                              .setdefault(repository, {}),
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   357
                "deleted_categories": session.setdefault("deleted_categories", {})
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   358
                                             .setdefault(repository, {})
40
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   359
            }, 200
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
        else:
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   361
            if modified_cat_id in session.setdefault("modified_categories", {}) \
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   362
                                         .setdefault(repository, {}):
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   363
                return {
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   364
                    "type": "modified",
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   365
                    "category": {
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   366
                        modified_cat_id: session["modified_categories"]
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   367
                                                [repository]
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   368
                                                [modified_cat_id]
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   369
                    }
40
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   370
                }, 200
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   371
            if modified_cat_id in session.setdefault("deleted_categories", {}) \
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   372
                                         .setdefault(repository, {}):
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   373
                return {
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   374
                    "type": "deleted",
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   375
                    "category": {
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   376
                        modified_cat_id: session["deleted_categories"]
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   377
                                                [repository]
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   378
                                                [modified_cat_id]
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   379
                    }
40
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   380
                }, 200
23
877c3b66313a Removed references to REPOSITORY_NAME + some refactoring of cat_editor in views.py (wtforms)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 22
diff changeset
   381
            return {
877c3b66313a Removed references to REPOSITORY_NAME + some refactoring of cat_editor in views.py (wtforms)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 22
diff changeset
   382
                "type": "untouched",
877c3b66313a Removed references to REPOSITORY_NAME + some refactoring of cat_editor in views.py (wtforms)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 22
diff changeset
   383
                "category": modified_cat_id
40
8c32ea41b391 New version + Reworked login process to suppress unauthenticated request + reworked file persistence + added tests module and API tests + reworked how the list repositories user can access is generated (can now only access repositories that are both in his repository list AND in the config repository list, so there is no need to add every new user to all repositories)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 38
diff changeset
   384
            }, 200
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
21
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
   386
    def delete(self, repository, modified_cat_id=None):
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
        """
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
            API to delete the category cat_id from the changelist or if cat_id
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
            is None, delete the whole changelist
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
        """
21
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
   391
        if modified_cat_id is not None:
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   392
            session.setdefault("modified_categories", {})[repository] = {
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   393
                cat_name: cat_content for cat_name, cat_content
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   394
                in session.setdefault("modified_categories", {}).setdefault(repository, {}).items()
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   395
                if cat_name != modified_cat_id
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   396
            }
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   397
            session.setdefault("deleted_categories", {})[repository] = {
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   398
                cat_name: cat_content for cat_name, cat_content
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   399
                in session.setdefault("deleted_categories", {}).setdefault(repository, {}).items()
22
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   400
                if cat_name != modified_cat_id
0ba28595fd4d Refactoring: change lists are now dicts + template inheritance for app pages + added debug messages with remaining github request count after each github api request
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 21
diff changeset
   401
            }
21
b1b002c5ff60 Added support for multiple repositories + Started work on template inheritance (only created empty base template so far)
Nicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
parents: 20
diff changeset
   402
        else:
94
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   403
            session.setdefault("modified_categories", {})[repository] = {}
71a4a7300c35 made session dict usage a lot safer (using dict.setdefault("key, default) instead of directly using dict["key"])
durandn
parents: 87
diff changeset
   404
            session.setdefault("deleted_categories", {})[repository] = {}
20
d47f8f8b3faf correct initialization and import of application + rename api file
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
        return 204