ajax login and save
authorcavaliet
Wed, 20 Aug 2014 15:36:50 +0200
changeset 310 05c9f6551fde
parent 309 c949cb431613
child 311 6c16170f947f
ajax login and save
src/hdalab/static/hdalab/css/additionnal_renkan.css
src/hdalab/static/hdalab/js/renkan-manual-save.js
src/hdalab/templates/ajax_identification/ajax_login_ok.html
src/hdalab/templates/renkan_edit.html
src/hdalab/views/profile.py
--- a/src/hdalab/static/hdalab/css/additionnal_renkan.css	Tue Aug 19 18:33:53 2014 +0200
+++ b/src/hdalab/static/hdalab/css/additionnal_renkan.css	Wed Aug 20 15:36:50 2014 +0200
@@ -1,3 +1,6 @@
 #container {
     overflow: hidden;
+}
+.hand{
+	cursor: pointer;
 }
\ No newline at end of file
--- a/src/hdalab/static/hdalab/js/renkan-manual-save.js	Tue Aug 19 18:33:53 2014 +0200
+++ b/src/hdalab/static/hdalab/js/renkan-manual-save.js	Wed Aug 20 15:36:50 2014 +0200
@@ -22,6 +22,10 @@
             success: function(_data) {
                 _proj.set(_data, {validate: true});
             	_renkan.renderer.autoScale();
+            	// Green color when user is authenticated
+            	if(_opts.user_authenticated==true){
+            	    $(".Rk-Save-Button").addClass("Rk-Save-Online");
+            	}
             }
         });
     };
@@ -34,11 +38,12 @@
             contentType: "application/json",
             data: JSON.stringify(_data),
             success: function(data, textStatus, jqXHR) {
-                console.log("RMS success !");
+                console.log("RMS success !", data);
                 $(window).off("beforeunload", _onLeave);
                 _saveWarn = false;
-                //document.location.hash = "#id=" + data.id;
-                //$(".Rk-Notifications").text("Saved as "+document.location.href).fadeIn().delay(2000).fadeOut();
+                if(data.substr(0,6)=="rk_id="){
+                    _opts.url = _opts.url.split("?")[0] + "?" + data;
+                }
             }
         });
     };
--- a/src/hdalab/templates/ajax_identification/ajax_login_ok.html	Tue Aug 19 18:33:53 2014 +0200
+++ b/src/hdalab/templates/ajax_identification/ajax_login_ok.html	Wed Aug 20 15:36:50 2014 +0200
@@ -1,5 +1,8 @@
-<div class="ok">OK</div>
-<div class="username">
+{% load i18n %}
+<div>
+  <div class="ok"><p>OK</p><p class="close hand">{% trans "Close window" %}</p></div>
+  <div class="username">
 	<li><a href="{% url 'profile_home' %}">{{ user.username }}</a></li>
 	<li><a href="{% url 'logout' %}?next={% url 'home' %}">{% trans "Log out" %}</a></li>
+  </div>
 </div>
\ No newline at end of file
--- a/src/hdalab/templates/renkan_edit.html	Tue Aug 19 18:33:53 2014 +0200
+++ b/src/hdalab/templates/renkan_edit.html	Wed Aug 20 15:36:50 2014 +0200
@@ -87,7 +87,7 @@
                     language: "fr"
                 });
                 
-                io_options = { url: "{% url 'renkan_get_put' %}" + window.location.search, user_authenticated: {% if request.user.is_authenticated %}true{% else %}false{% endif %} };
+                io_options = { url: "{% url 'renkan_get_put' %}" + window.location.search, user_authenticated: {% if user.is_authenticated %}true{% else %}false{% endif %} };
                 Rkns.jsonIOSaveOnClick(_renkan, io_options);
             };
             
@@ -109,6 +109,20 @@
                         url: f.attr("action"),
                         data: f.serialize(),
                         success: function(data, textStatus, jqXHR) {
+                            var d = $(data);
+                            if($(".ok",d).length==1){
+                                $("#dialog-form").html($(".ok",d)[0]);
+                                $(".hello-user").remove();
+                                $("#nav").append($(".username",d).children());
+                                //$("#dialog-form .close").click(function(){
+                                //    dialog.dialog( "close" );
+                                //});
+                                io_options.user_authenticated = true;
+                                $(".Rk-Save-Button").addClass("Rk-Save-Online");
+                                dialog.dialog( "close" );
+                                $(".Rk-Save-Button").click();
+                                return true;
+                            }
                             $("#dialog-form").html(data);
                             // in case, login failed, we reassign the behaviour
                             form = dialog.find( "form" ).on( "submit", function( event ) {
--- a/src/hdalab/views/profile.py	Tue Aug 19 18:33:53 2014 +0200
+++ b/src/hdalab/views/profile.py	Wed Aug 20 15:36:50 2014 +0200
@@ -13,7 +13,7 @@
 from django.contrib.sites.models import get_current_site
 from django.core.urlresolvers import reverse
 from django.db.models import Q
-from django.http import HttpResponse
+from django.http import HttpResponse, HttpResponseBadRequest
 from django.http import HttpResponseRedirect
 from django.shortcuts import get_object_or_404, redirect
 from django.shortcuts import resolve_url
@@ -230,19 +230,72 @@
         
         response = json.dumps(content)
         
-        if request.user.is_authenticated():
-            rk = Renkan()
-            rk.rk_id = unicode(uuid.uuid1())
-            rk.title = title
-            rk.content = response
-            rk.owner = request.user
-            rk.save()
-            hr = HdalabRenkan()
-            hr.renkan = rk
-            hr.state = HdalabRenkan.EDITION
-            hr.save()
+#         if request.user.is_authenticated():
+#             rk = Renkan()
+#             rk.rk_id = unicode(uuid.uuid1())
+#             rk.title = title
+#             rk.content = response
+#             rk.owner = request.user
+#             rk.save()
+#             hr = HdalabRenkan()
+#             hr.renkan = rk
+#             hr.state = HdalabRenkan.EDITION
+#             hr.save()
         
         return HttpResponse(response, content_type="application/json")
+    
+    
+    def post(self, request):
+        
+        rk_id = request.GET.get("rk_id", "")
+        #data = json.loads(request.body)
+        logger.debug("YEAH !")
+        #logger.debug(data["edges"])
+        #logger.debug(data["nodes"])
+        #logger.debug(request.user.is_authenticated())
+        #logger.debug(request.user.is_anonymous())
+        if rk_id!="":
+            rk = get_object_or_404(Renkan, rk_id=rk_id)
+            if rk.owner!=request.user:
+                return HttpResponseBadRequest("You are not allowed to edit this renkan")
+            rk.content = request.body
+            data = json.loads(request.body)
+            if "title" in data:
+                rk.title = data["title"]
+            rk.save()
+            return HttpResponse("SAVED")
+        else:
+            logger.debug("1")
+            # if rk_id was not a get parameter AND if it is set in json data AND if user is authenticated
+            # Then we can save the renkan
+            data = json.loads(request.body)
+            logger.debug("2")
+            if "id" in data:
+                logger.debug("3")
+                rk_id = data["id"]
+                logger.debug("4")
+                logger.debug(rk_id)
+                logger.debug(Renkan.objects.filter(rk_id=rk_id))
+                logger.debug(Renkan.objects.filter(rk_id=rk_id).count())
+                logger.debug(request.user.is_authenticated())
+                logger.debug(request.user)
+                if rk_id != "" and Renkan.objects.filter(rk_id=rk_id).count()==0 and request.user.is_authenticated():
+                    logger.debug("5")
+                    rk = Renkan()
+                    rk.rk_id = rk_id
+                    rk.title = data["title"] if "title" in data else ""
+                    rk.content = data
+                    rk.owner = request.user
+                    rk.save()
+                    hr = HdalabRenkan()
+                    hr.renkan = rk
+                    hr.state = HdalabRenkan.EDITION
+                    hr.save()
+                    logger.debug("6")
+                    return HttpResponse("rk_id=" + rk_id)
+         
+            
+        return HttpResponse("NOT SAVED")