Fixed cache registry handling + added forms.py (didn't commit last time) + moved forms in views/social.py into views/forms.py + small tweaks to timezone support
authordurandn
Wed, 15 Apr 2015 14:56:07 +0200
changeset 123 1e1c8f98f3a8
parent 122 cee0e7d7e6d2
child 124 e51330c85650
Fixed cache registry handling + added forms.py (didn't commit last time) + moved forms in views/social.py into views/forms.py + small tweaks to timezone support
src/catedit/resources.py
src/catedit/tasks.py
src/catedit/templates/categories/workshop.html
src/catedit/templates/social/changesets_index.html
src/catedit/views/forms.py
src/catedit/views/meta.py
src/catedit/views/social.py
src/catedit/views/utils.py
--- a/src/catedit/resources.py	Wed Apr 15 10:30:48 2015 +0200
+++ b/src/catedit/resources.py	Wed Apr 15 14:56:07 2015 +0200
@@ -40,7 +40,9 @@
 
         # Cache key for category key registry for this repo
         registry_key = "categories_"+repository+"_keys"
-        if (cache.get(registry_key) is None or cache_key not in cache.get(registry_key)) and cache.get(cache_key) is None:
+        keys_set = cache.get(registry_key)
+        
+        if keys_set is None or (keys_set is not None and cache_key not in keys_set):
             rv = None
             cat_manager_instance = CategoryManager(
                 getattr(
@@ -65,15 +67,14 @@
                 rv = response, 200
             # Cache operations
             # Setting key for this function
-            cache.set(cache_key, rv, timeout=3600)
-            # Setting key in the registry
-            if cache.get(registry_key) is None:
-                cache.set(registry_key, [cache_key])
+            cache.set(cache_key, rv)
+            # Setting key in the registry, we get the key list again in case it was updated while running the function
+            keys_set = cache.get(registry_key)
+            if keys_set is None:
+                cache.set(registry_key, set([cache_key]))
             else:
-                if cache_key not in cache.get(registry_key):
-                    key_list = cache.get(registry_key)
-                    key_list.append(cache_key)
-                    cache.set(registry_key, key_list)
+                keys_set.add(cache_key)
+                cache.set(registry_key, keys_set)
             return rv
         else:
             return cache.get(cache_key)
--- a/src/catedit/tasks.py	Wed Apr 15 10:30:48 2015 +0200
+++ b/src/catedit/tasks.py	Wed Apr 15 14:56:07 2015 +0200
@@ -1,9 +1,9 @@
-from catedit import app, celery, cache
+from catedit import app, celery
 from catedit.models import CategoryManager
 import catedit.persistence
 import logging
 
-from flask import g, url_for
+from flask import g
 import requests
 
 
@@ -27,8 +27,8 @@
                 message=message
             )
             registry_key = "categories_"+repository+"_keys"
-            #r = requests.post(app.config['BASE_URL']+"/meta/cache-clear/"+registry_key)
-            r = requests.post(app.config['BASE_URL']+"/meta/cache-clear")
+            r = requests.post(app.config['BASE_URL']+"/meta/cache-clear/"+registry_key)
+            #r = requests.post(app.config['BASE_URL']+"/meta/cache-clear")
             try:
                 r.raise_for_status()
             except:
--- a/src/catedit/templates/categories/workshop.html	Wed Apr 15 10:30:48 2015 +0200
+++ b/src/catedit/templates/categories/workshop.html	Wed Apr 15 14:56:07 2015 +0200
@@ -124,7 +124,7 @@
     <thead>
       <tr>
         <th>Auteur</th>
-        <th>Date</th>
+        <th>Soumis</th>
         <th>Message de soumission</th>
         <th>Commentaires</th>
       </tr>
--- a/src/catedit/templates/social/changesets_index.html	Wed Apr 15 10:30:48 2015 +0200
+++ b/src/catedit/templates/social/changesets_index.html	Wed Apr 15 14:56:07 2015 +0200
@@ -34,7 +34,7 @@
     <thead>
       <tr>
         <th>Auteur</th>
-        <th>Date</th>
+        <th>Soumis</th>
         <th>Message de soumission</th>
         <th>Commentaires</th>
       </tr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/catedit/views/forms.py	Wed Apr 15 14:56:07 2015 +0200
@@ -0,0 +1,67 @@
+"""
+forms.py
+Module that groups custom forms used in CatEdit
+"""
+from flask_wtf import Form
+from wtforms import StringField, TextAreaField, FormField, \
+                    FieldList, HiddenField
+from wtforms.validators import DataRequired, Optional, AnyOf
+
+
+class CommitForm(Form):
+    """
+        Custom form class for commiting changes
+    """
+    commit_message = StringField(
+        "Message de soumission (obligatoire)",
+        validators=[DataRequired()]
+    )
+
+
+class PropertyForm(Form):
+    """
+        Form of a given property, each one is a couple of hidden fields that
+        can be Javascript-generated in the template
+    """
+    property_predicate = HiddenField()
+    property_object = HiddenField(
+        validators=[DataRequired()]
+    )
+
+
+class CategoryForm(Form):
+    """
+        Custom form class for creating a category with the absolute minimal
+        attributes (label and description)
+    """
+    label = StringField(
+        "Nom de la categorie (obligatoire)",
+        validators=[DataRequired()]
+    )
+    description = TextAreaField(
+        "Description de la categorie (obligatoire)",
+        validators=[DataRequired()]
+    )
+    properties = FieldList(FormField(PropertyForm), validators=[Optional()])
+
+
+class CommentForm(Form):
+    """
+        Custom form class for commiting changes
+    """
+    comment_field = TextAreaField(
+        "Poster un commentaire: ",
+        validators=[DataRequired()]
+    )
+
+class NewDiscussionForm(Form):
+    """
+        Custom form class for commiting changes
+    """
+    discussion_title = StringField(
+        "Titre de la discussion: ",
+        validators=[DataRequired()]
+    )
+    comment_field = TextAreaField(
+        "Commentaire d'ouverture (facultatif): "
+    )
\ No newline at end of file
--- a/src/catedit/views/meta.py	Wed Apr 15 10:30:48 2015 +0200
+++ b/src/catedit/views/meta.py	Wed Apr 15 14:56:07 2015 +0200
@@ -21,11 +21,15 @@
     if not registry_key:
         cache.clear()
     else:
-        if cache.get(registry_key) is not None:
-            for key in cache.get(registry_key):
+        keys_set = cache.get(registry_key)
+        if keys_set is not None:
+            logger.debug(keys_set)
+            for key in keys_set:
                 if cache.get(key) is not None:
                     cache.delete(key)
             cache.delete(registry_key)
+        else:
+            logger.debug(registry_key+" not in cache")
     return ('', 204)
 
 
--- a/src/catedit/views/social.py	Wed Apr 15 10:30:48 2015 +0200
+++ b/src/catedit/views/social.py	Wed Apr 15 14:56:07 2015 +0200
@@ -5,15 +5,13 @@
 
 from catedit import app
 from catedit.utils import make_differences_list, compare_categories
+from catedit.views.forms import NewDiscussionForm, CommentForm
 from catedit.views.utils import check_user_status_and_repo_access, \
                                 get_comments, post_comment, get_commits, \
                                 get_issues, get_category_list_for_commit
 
 from flask import render_template, request, redirect, url_for, \
                   abort, Blueprint, session
-from flask_wtf import Form
-from wtforms import TextAreaField, StringField
-from wtforms.validators import DataRequired
 
 
 module = Blueprint('social', __name__)
@@ -92,16 +90,6 @@
     )
 
 
-class CommentForm(Form):
-    """
-        Custom form class for commiting changes
-    """
-    comment_field = TextAreaField(
-        "Poster un commentaire: ",
-        validators=[DataRequired()]
-    )
-
-
 @module.route("/<string:repository>/changesets/<string:changeset_id>",
               methods=["GET", "POST"],
               defaults={"per_page": 10, "page": 1, "details_cat_id": None})
@@ -275,19 +263,6 @@
         )
 
 
-class NewDiscussionForm(Form):
-    """
-        Custom form class for commiting changes
-    """
-    discussion_title = StringField(
-        "Titre de la discussion: ",
-        validators=[DataRequired()]
-    )
-    comment_field = TextAreaField(
-        "Commentaire d'ouverture (facultatif): "
-    )
-
-
 @module.route(
     "/<string:repository>/discussions/<string:discussion_id>",
     methods=["GET", "POST"],
--- a/src/catedit/views/utils.py	Wed Apr 15 10:30:48 2015 +0200
+++ b/src/catedit/views/utils.py	Wed Apr 15 14:56:07 2015 +0200
@@ -90,8 +90,9 @@
                 + str(page) + "_" + str(per_page)
     # Cache key for comments key registry for this repo
     registry_key = "comments_"+repository+"_keys"
-
-    if  (cache.get(registry_key) is None or cache_key not in cache.get(registry_key)) and cache.get(cache_key) is None:
+    keys_set = cache.get(registry_key)
+        
+    if keys_set is None or (keys_set is not None and cache_key not in keys_set):
         github_comments_data = []
 
         try:
@@ -197,15 +198,14 @@
 
         # Cache operations
         # Setting key for this function
-        cache.set(cache_key, rv, timeout=3600)
-        # Setting key in the registry
-        if cache.get(registry_key) is None:
-            cache.set(registry_key, [cache_key])
+        cache.set(cache_key, rv)
+        # Setting key in the registry, we get the key list again in case it was updated while running the function
+        keys_set = cache.get(registry_key)
+        if keys_set is None:
+            cache.set(registry_key, set([cache_key]))
         else:
-            if cache_key not in cache.get(registry_key):
-                key_list = cache.get(registry_key)
-                key_list.append(cache_key)
-                cache.set(registry_key, key_list)
+            keys_set.add(cache_key)
+            cache.set(registry_key, keys_set)
         return rv
     else:
         return cache.get(cache_key)
@@ -273,11 +273,15 @@
             logger.error(ghe.response.text)
 
     registry_key = "comments_"+repository+"_keys"
-    if cache.get(registry_key) is not None:
-        for key in cache.get(registry_key):
+    keys_set = cache.get(registry_key)
+    if keys_set is not None:
+        logger.debug(keys_set)
+        for key in keys_set:
             if cache.get(key) is not None:
                 cache.delete(key)
         cache.delete(registry_key)
+    else:
+        logger.debug(registry_key+" not in cache")
     return return_id
 
 
@@ -299,8 +303,9 @@
 
     # Cache key for comments key registry for this repo
     registry_key = "categories_"+repository+"_keys"
-
-    if (cache.get(registry_key) is None or cache_key not in cache.get(registry_key)) and cache.get(cache_key) is None:
+    keys_set = cache.get(registry_key)
+        
+    if keys_set is None or (keys_set is not None and cache_key not in keys_set):
         commits_data = []
         try:
             commits_data = github.get(
@@ -339,15 +344,14 @@
 
         # Cache operations
         # Setting key for this function
-        cache.set(cache_key, rv, timeout=3600)
-        # Setting key in the registry
-        if cache.get(registry_key) is None:
-            cache.set(registry_key, [cache_key])
+        cache.set(cache_key, rv)
+        # Setting key in the registry, we get the key list again in case it was updated while running the function
+        keys_set = cache.get(registry_key)
+        if keys_set is None:
+            cache.set(registry_key, set([cache_key]))
         else:
-            if cache_key not in cache.get(registry_key):
-                key_list = cache.get(registry_key)
-                key_list.append(cache_key)
-                cache.set(registry_key, key_list)
+            keys_set.add(cache_key)
+            cache.set(registry_key, keys_set)
         return rv
     else:
         return cache.get(cache_key)
@@ -373,8 +377,9 @@
 
     # Cache key for comments key registry for this repo
     registry_key = "comments_"+repository+"_keys"
-
-    if (cache.get(registry_key) is None or cache_key not in cache.get(registry_key)) and cache.get(cache_key) is None:
+    keys_set = cache.get(registry_key)
+        
+    if keys_set is None or (keys_set is not None and cache_key not in keys_set):
         issues_data = []
         try:
             issues_data = github.get(
@@ -412,15 +417,14 @@
 
         # Cache operations
         # Setting key for this function
-        cache.set(cache_key, rv, timeout=3600)
-        # Setting key in the registry
-        if cache.get(registry_key) is None:
-            cache.set(registry_key, [cache_key])
+        cache.set(cache_key, rv)
+        # Setting key in the registry, we get the key list again in case it was updated while running the function
+        keys_set = cache.get(registry_key)
+        if keys_set is None:
+            cache.set(registry_key, set([cache_key]))
         else:
-            if cache_key not in cache.get(registry_key):
-                key_list = cache.get(registry_key)
-                key_list.append(cache_key)
-                cache.set(registry_key, key_list)
+            keys_set.add(cache_key)
+            cache.set(registry_key, keys_set)
         return rv
     else:
         return cache.get(cache_key)