Commits

Konstantine Rybnikov committed 2b11123

adding repository almost works. Gosh I'm slow :-)

Comments (0)

Files changed (6)

fat/fat_po_editor/bl/repository.py

     if not os.path.exists(path):
         raise UserAlertError(_(u'Path %(path)s does not exist.') % dict(
                 path=path))
-    # repostiory = Repository(path=path)
-    # repository.save()
+    
+    repository = Repository(path=path)
+    repository.save()
+    return repository
+
+def full_list():
+    Repository.objects()

fat/fat_po_editor/static/js/no_django_jquery_csrf.js

+$('html').ajaxSend(function(event, xhr, settings) {
+    function getCookie(name) {
+        var cookieValue = null;
+        if (document.cookie && document.cookie != '') {
+            var cookies = document.cookie.split(';');
+            for (var i = 0; i < cookies.length; i++) {
+                var cookie = jQuery.trim(cookies[i]);
+                // Does this cookie string begin with the name we want?
+                if (cookie.substring(0, name.length + 1) == (name + '=')) {
+                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+                    break;
+                }
+            }
+        }
+        return cookieValue;
+    }
+    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
+        // Only send the token to relative URLs i.e. locally.
+        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
+    }
+});

fat/fat_po_editor/static/js/repositories_list.js

 
 RepositoriesList.prototype.init = function(options) {
     // add button options
-    var $add_button = this.$add_button = options['$add_button'];
-    var $add_path_input = this.$add_path_input = options['$add_path_input'];
+    var $list = this.$list = options['$list'];
     var add_url = this.add_url = options['add_url'];
     
     // autocomplete options
     var autocomplete_url = this.autocomplete_url = options['autocomplete_url'];
 
+    var $add_button = this.$add_button = $list.find('.add-repo');
+    var $add_path_input = this.$add_path_input = $list.find('.repo-name');
+    
     // this.updateList options
-    var $repository_list_url = this.$repository_list_url = options['$repository_list_url'];
+    var $repository_list_ul = this.$repository_list_ul = $list.find('.repository-list');
     
     // add button
     this.$add_button.click(function() {
                 if (response.status == 'success') {
                     AjaxUtil.info(
                         response.data.message);
+                    this.updateList(response.data.repositories);
                 } else if (response.status == 'fail') {
                     AjaxUtil.error(
                         response.data.message);
         .autocomplete({source: autocomplete_url});
 };
 
-RepositoriesList.prototype.updateList = function() {
+RepositoriesList.prototype.updateList = function(repositories) {
+    console.log(repositories);
     // 1. get list
     // 2. update list
 };

fat/fat_po_editor/templates/fat_po_editor/base.html

     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
     <script type="text/javascript" src="{{ STATIC_URL }}js/oop.js"></script>
     <script type="text/javascript" src="{{ STATIC_URL }}js/ajax_util.js"></script>
+    <script type="text/javascript" src="{{ STATIC_URL }}js/no_django_jquery_csrf.js"></script>
     {% additional_js_render %}
     <script type="text/javascript">
         {% domready_render %}

fat/fat_po_editor/templates/fat_po_editor/repositories_list.html

 <script type="text/javascript" src="{{ STATIC_URL }}js/repositories_list.js"></script>
 {% end_add_js %}
 <h3>List of repositories</h2>
-<p>
-    {% if repositories %}
-    <ul id="repository-list-ul">
-        {% for repository in repositories %}
-        <li>{{ repository }}</li>
-        {% endfor %}
-    </ul>
-    {% else %}
-    {% trans "No repository path's have been added yet. Go ahead!" %}
-    {% endif %}
-</p>
-<p class="repositories-list">
-    <input type="text" name="path" value="" id="repository_add_path" class="input-path" />
-    <a href="javascript:void(0)"
-       class="pseudo"
-       id="add-repo">Add</a>
-    {% domready %}
-        var repolist = (
-            new RepositoriesList({
-                $add_button: $('#add-repo'),
-                $add_path_input: $('#repository_add_path'),
-                add_url: '{% url fat_po_editor.views.repository.add %}',
-                autocomplete_url: "{% url fat_po_editor.views.repository.path_autocomplete %}",
-                $repository_list_url: $('#repository-list-ul')}));
-    {% enddomready %}
-    
-    {# {% url fat_po_editor.views.repository.add %} #}
-</p>
+<div id="repositories-list">
+    <form action="javascript:void(0)">{% csrf_token %}
+        {% if repositories %}
+        <ul class="repository-list">
+            {% for repository in repositories %}
+            <li>{{ repository }}</li>
+            {% endfor %}
+        </ul>
+        {% else %}
+        {% trans "No repository path's have been added yet. Go ahead!" %}
+        {% endif %}
+        <div>
+            <input type="text" name="path" value="" class="repo-name" />
+            <a href="javascript:void(0)"
+               class="add-repo pseudo">Add</a>
+            {% domready %}
+                var repolist = (
+                    new RepositoriesList({
+                        $list: $('#repositories-list'),
+                        add_url: '{% url fat_po_editor.views.repository.add %}',
+                        autocomplete_url: "{% url fat_po_editor.views.repository.path_autocomplete %}"}));
+            {% enddomready %}
+
+            {# {% url fat_po_editor.views.repository.add %} #}
+        </div>
+    </form>
+</div>

fat/fat_po_editor/views/repository.py

             return json_http_response_fail({
                         'message': e.message})
         
-        return json_http_response_success({
+        repositories = repository.full_list()
+        
+        return (
+            json_http_response_success({
                 'message': _(u'Repository path %(repository_path)s was added') % dict(
-                    repository_path=repository_path)})
+                    repository_path=repository_path),
+                'repositories': repositories,
+            }))
     else:
         return HttpResponse(u'Waiting for POST data')