Initial commit (this time I'll push) + added checks if the authenticated user has write access to categories repo + added try except blocks around github api request
authorNicolas DURAND <nicolas.durand@iri.centrepompidou.fr>
Fri, 21 Nov 2014 14:19:20 +0100
changeset 1 83d266c0c832
parent 0 54f4e0f9d636
child 2 f7bc7942969a
Initial commit (this time I'll push) + added checks if the authenticated user has write access to categories repo + added try except blocks around github api request
src/catedit/persistence.py
src/catedit/settings.py
src/catedit/static/css/style.css
src/catedit/templates/cateditor.html
src/catedit/templates/catrecap.html
src/catedit/views.py
--- a/src/catedit/persistence.py	Thu Nov 20 17:44:01 2014 +0100
+++ b/src/catedit/persistence.py	Fri Nov 21 14:19:20 2014 +0100
@@ -80,43 +80,53 @@
         except GitHubError:
             pass
         # print json.dumps(request_data)
-        github.request('PUT',
-                       "repos/"
-                       +app.config["REPOSITORY_OWNER"]+"/"
-                       +app.config["REPOSITORY_NAME"]
-                       +"/contents/"
-                       +app.config["CATEGORIES_PATH"]
-                       +kwargs["name"],
-                       data=json.dumps(request_data))
+        try:
+            github.request('PUT',
+                           "repos/"
+                           +app.config["REPOSITORY_OWNER"]+"/"
+                           +app.config["REPOSITORY_NAME"]
+                           +"/contents/"
+                           +app.config["CATEGORIES_PATH"]
+                           +kwargs["name"],
+                           data=json.dumps(request_data))
+        except GitHubError:
+            pass
 
     def load(self, **kwargs):
-        filedict = github.get("repos/"
-                              +app.config["REPOSITORY_OWNER"]+"/"
-                              +app.config["REPOSITORY_NAME"]
-                              +"/contents/"
-                              +app.config["CATEGORIES_PATH"]
-                              +kwargs["name"])
-        file_content=b64decode(filedict["content"])
+        try:
+            filedict = github.get("repos/"
+                                  +app.config["REPOSITORY_OWNER"]+"/"
+                                  +app.config["REPOSITORY_NAME"]
+                                  +"/contents/"
+                                  +app.config["CATEGORIES_PATH"]
+                                  +kwargs["name"])
+            file_content=b64decode(filedict["content"])
+        except GitHubError:
+            pass
         return file_content
 
     def delete(self, **kwargs):
         request_data = { "message": kwargs["message"] }
+        try:
+            filedict = github.get("repos/"
+                                  +app.config["REPOSITORY_OWNER"]+"/"
+                                  +app.config["REPOSITORY_NAME"]
+                                  +"/contents/"
+                                  +app.config["CATEGORIES_PATH"]
+                                  +kwargs["name"])
+            request_data["sha"] = filedict["sha"]
+        except GitHubError:
+            pass
 
-        filedict = github.get("repos/"
-                              +app.config["REPOSITORY_OWNER"]+"/"
-                              +app.config["REPOSITORY_NAME"]
-                              +"/contents/"
-                              +app.config["CATEGORIES_PATH"]
-                              +kwargs["name"])
-        request_data["sha"] = filedict["sha"]
-
-        # print json.dumps(request_data)
-        github.request('DELETE',
-                        "repos/catedit-system/"
-                        +app.config["REPOSITORY_NAME"]
-                        +"/contents/categories/"
-                        +kwargs["name"],
-                        data=json.dumps(request_data))
+        try:
+            github.request('DELETE',
+                            "repos/catedit-system/"
+                            +app.config["REPOSITORY_NAME"]
+                            +"/contents/categories/"
+                            +kwargs["name"],
+                            data=json.dumps(request_data))
+        except GitHubError:
+            pass
 
     def list(self, **kwargs):
         filenames_list=[]
@@ -131,12 +141,15 @@
             pass
         file_content_list=[]
         for filename in filenames_list:
-            filedict = github.get("repos/"
-                                  +app.config["REPOSITORY_OWNER"]+"/"
-                                  +app.config["REPOSITORY_NAME"]
-                                  +"/contents/"
-                                  +app.config["CATEGORIES_PATH"]
-                                  +filename)
-            file_content_list.append(b64decode(filedict["content"]))
+            try:
+                filedict = github.get("repos/"
+                                      +app.config["REPOSITORY_OWNER"]+"/"
+                                      +app.config["REPOSITORY_NAME"]
+                                      +"/contents/"
+                                      +app.config["CATEGORIES_PATH"]
+                                      +filename)
+                file_content_list.append(b64decode(filedict["content"]))
+            except GitHubError:
+                pass
         # print file_content_list
         return file_content_list
--- a/src/catedit/settings.py	Thu Nov 20 17:44:01 2014 +0100
+++ b/src/catedit/settings.py	Fri Nov 21 14:19:20 2014 +0100
@@ -18,7 +18,7 @@
 
     # Logging config
     LOG_FILE_PATH = "log/log.txt"
-    LOGGING = True
+    LOGGING = False
 
     # Github repository config
 
--- a/src/catedit/static/css/style.css	Thu Nov 20 17:44:01 2014 +0100
+++ b/src/catedit/static/css/style.css	Fri Nov 21 14:19:20 2014 +0100
@@ -3,6 +3,10 @@
   margin-bottom: 4px;
 }
 
+.property-delete-button{
+  margin-left: 4px;
+}
+
 .visible{
   display:block;
 }
--- a/src/catedit/templates/cateditor.html	Thu Nov 20 17:44:01 2014 +0100
+++ b/src/catedit/templates/cateditor.html	Fri Nov 21 14:19:20 2014 +0100
@@ -1,4 +1,4 @@
-{% if not session.get("user_code", None) %}
+{% if not session["user_logged"] or not session["user_can_edit"] %}
   {% set readonly="readonly" %}
 {% else %}
   {% set readonly=False %}
@@ -31,14 +31,21 @@
             <li class="active"><a>Editeur de catégorie: {% if cat_id: %} Edition {% else %} Création {% endif %}</a></li>
           </ul>
           <div class="navbar-text navbar-right">
-            {% if not session.get("user_code", None)%}<a href="{{ url_for('github_login') }}" class="navbar-link">S'authentifier</a>
+            {% if not session.get("user_code", None)%} Non authentifié - <a href="{{ url_for('github_login') }}" class="navbar-link">S'authentifier</a>
             {% else %} Authentifié: {{ session["user_login"] }} - <a href="{{ url_for('logout') }}" class="navbar-link">Quitter</a>{% endif %}
           </div>
         </div>
       </div>
   </div>
   <div class="container">
-    {% if readonly %}
+    {% if session["user_logged"] and not session["user_can_edit"] %}
+    <div class="alert alert-warning" role="alert">
+      <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
+      <span class="sr-only">Attention:</span>
+      Vous n'avez pas accès en écriture au repository contenant les catégories - Vous ne pourrez pas les modifier
+    </div>
+    {% endif %}
+    {% if not session["user_logged"] %}
     <div class="alert alert-warning" role="alert">
       <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
       <span class="sr-only">Attention:</span>
--- a/src/catedit/templates/catrecap.html	Thu Nov 20 17:44:01 2014 +0100
+++ b/src/catedit/templates/catrecap.html	Fri Nov 21 14:19:20 2014 +0100
@@ -1,3 +1,8 @@
+{% if not session["user_logged"] or not session["user_can_edit"] %}
+  {% set readonly="readonly" %}
+{% else %}
+  {% set readonly=False %}
+{% endif %}
 <!DOCTYPE html>
 <html lang="fr">
   <head>
@@ -46,6 +51,13 @@
         </div>
     </div>
     <div class="container">
+      {% if session["user_logged"] and not session["user_can_edit"] %}
+      <div class="alert alert-warning" role="alert">
+        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
+        <span class="sr-only">Attention:</span>
+        Vous n'avez pas accès en écriture au repository contenant les catégories - Vous ne pourrez pas les modifier.
+      </div>
+      {% endif %}
       <h2>Liste des catégories existantes</h2>
       <table class="table table-striped table-bordered table-condensed">
         <thead>
@@ -57,7 +69,7 @@
         <tbody>
         {% if cat_list|length == 0 %}
           <tr>
-            <td class="col-md-12" colspan="3">Aucune catégorie n'a été créée. <a href="{{ url_for('cat_editor') }}">Créer une catégorie</a></td>
+            <td class="col-md-12" colspan="3">Aucune catégorie n'a été créée pour l'instant. {% if not readonly %}<a href="{{ url_for('cat_editor') }}">Créer une catégorie</a>{% endif %}</td>
           </tr>
         {% else %}
           {% for cat in cat_list %}
--- a/src/catedit/views.py	Thu Nov 20 17:44:01 2014 +0100
+++ b/src/catedit/views.py	Fri Nov 21 14:19:20 2014 +0100
@@ -1,6 +1,7 @@
 from app import app, github
 from models import Category, CategoryManager
 from flask import render_template, request, redirect, url_for, session
+from flask.ext.github import GitHubError
 from flask_wtf import Form
 from api import CategoryAPI
 from wtforms import StringField, TextAreaField
@@ -119,6 +120,19 @@
     session["user_code"] = oauth_code
     session["user_logged"] = True
     session["user_login"] = github.get("user")["login"]
+    try:
+        repoList=[]
+        repoList=github.get("user/repos")
+        # for repo in repoList:
+        #     print repo["name"]
+        session["user_can_edit"] = True
+        if not any (repo["name"] == app.config["REPOSITORY_NAME"] for repo in repoList):
+            session["user_can_edit"] = False
+        print session["user_can_edit"]
+    except GitHubError:
+        # print "error getting repos!"
+        pass
+
     # print session["user_login"]
     return redirect(url_for('cat_recap'))
 
@@ -132,4 +146,5 @@
     session["user_code"] = None
     session["user_logged"] = None
     session["user_login"] = None
+    session["user_can_edit"] = None
     return redirect(url_for('cat_recap'))