Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

all done except a "small" problem with bookmark creation and csrf pro…

…tection
  • Loading branch information...
commit 89366ca69133f1622133b37955ba42feae875f27 1 parent 3c11f90
Daroth Daroth authored
27 bookmark/blueprint/api/__init__.py
... ... @@ -1,4 +1,4 @@
1   -from flask import Blueprint, request
  1 +from flask import Blueprint, request, jsonify
2 2 from functools import wraps
3 3
4 4
@@ -11,19 +11,8 @@
11 11 from .item.Tag import ItemTag
12 12 from .form import BookmarkForm
13 13
14   -import json
15   -
16 14 b = Blueprint('api', __name__)
17 15
18   -def json_result_decorator(f):
19   - @wraps(f)
20   - def json_result(*args, **kwargs):
21   - dumped = json.dumps(f(*args, **kwargs))
22   - response = app.make_response(dumped)
23   - response.mimetype = 'application/json'
24   - return response
25   - return json_result
26   -
27 16 @b.route('/', methods=['GET', ])
28 17 def index():
29 18 return VERSION
@@ -31,7 +20,6 @@ def index():
31 20
32 21 @b.route('/bookmarks/', methods=['GET', ])
33 22 @b.route('/bookmarks/<string:tags>', methods=['GET', ])
34   -@json_result_decorator
35 23 def bookmarks(tags=None):
36 24 filters = []
37 25 if tags is not None:
@@ -57,26 +45,25 @@ def bookmarks(tags=None):
57 45 "total": total,
58 46 }
59 47
60   - return ret
  48 + return jsonify(ret)
61 49
62 50
63 51 @b.route('/bookmarks/', methods=['POST', ])
64   -@json_result_decorator
65 52 def add_bookmark():
66   - form = BookmarkForm(create=True)
  53 + form = BookmarkForm(create=True, json=request.json)
67 54 ret = None
68 55 if form.validate_on_submit():
69 56 # register form
  57 + add_bookmark(form.bookmark)
70 58 ret = ["OK"]
71 59 else:
72 60 ret = form.errors
73   - return ret
  61 + return jsonify(ret)
74 62
75 63
76 64
77 65 @b.route('/tagcloud', methods=['GET', ])
78 66 @b.route('/tagcloud/<string:tags>', methods=['GET', ])
79   -@json_result_decorator
80 67 def tagcloud(tags=None):
81 68 filters = []
82 69 filters_tags = []
@@ -94,6 +81,6 @@ def tagcloud(tags=None):
94 81
95 82 tags_list = map(lambda x: x.json(), filter_list)
96 83 process_tag_count(tags_list, max_percent=30, min_percent=11)
97   - tags_list = [x.json() for x in filters_tags] + tags_list
  84 + tags_list = {"tags": [x.json() for x in filters_tags] + tags_list}
98 85
99   - return tags_list
  86 + return jsonify(tags_list)
16 bookmark/blueprint/api/form/__init__.py
@@ -5,6 +5,7 @@
5 5 from flaskext.wtf import TextAreaField
6 6 from flaskext.wtf import SubmitField
7 7 from flaskext.wtf.html5 import URLField
  8 +from bookmark.blueprint.api.item.Bookmark import ItemBookmark
8 9
9 10 class BookmarkForm(Form):
10 11 id = HiddenField(u'Id')
@@ -14,14 +15,27 @@ class BookmarkForm(Form):
14 15 tags = TextField(u'Tags')
15 16 submit = SubmitField(u'Add')
16 17
17   - def __init__(self, create, *args, **kwargs):
  18 + def __init__(self, create, json=None, *args, **kwargs):
18 19 Form.__init__(self, *args, **kwargs)
19 20 self.create = create
  21 + if json is not None:
  22 + self.link.data = json['link']
  23 + self.title.data = json['title']
  24 + self.description.data = json['description']
  25 + self.tags.data = json['tags']
  26 + #self.csrf.data = json['csrf']
  27 + self.bookmark = None
20 28
21 29 def validate(self):
22 30 rv = Form.validate(self)
23 31 if not rv:
24 32 return False
25 33
  34 + link = self.link.data
  35 + title = self.title.data
  36 + description = self.description.data
  37 + tags = map(lambda x : x.strip(), self.tags.data.split[','])
  38 +
26 39 # TODO : add id control if create is False (modification mode need a valid bookmark id)
  40 + self.bookmark = ItemBookmark(ptags=tags, plink=link, ptitle=title, pdescription=description)
27 41 return True
2  bookmark/blueprint/login/__init__.py
@@ -26,7 +26,7 @@ def login():
26 26 form = LoginForm()
27 27 if form.validate_on_submit():
28 28 # login and valide the user...
29   - login_user(form.user)
  29 + login_user(form.user, remember=form.remember.data)
30 30 return redirect(request.args.get('next') or url_for('web.index'))
31 31 return render_template('login.html', form=form)
32 32
50 bookmark/web/js/app.js
@@ -11412,6 +11412,10 @@ window.jQuery = window.$ = jQuery;
11412 11412 return filters;
11413 11413 };
11414 11414
  11415 + TagscloudCollection.prototype.parse = function(response) {
  11416 + return response.tags;
  11417 + };
  11418 +
11415 11419 return TagscloudCollection;
11416 11420
11417 11421 })(Backbone.Collection);
@@ -11816,9 +11820,12 @@ window.jQuery = window.$ = jQuery;
11816 11820
11817 11821 }).call(this);
11818 11822 }, "views/bookmark_form_view": function(exports, require, module) {(function() {
11819   - var __hasProp = Object.prototype.hasOwnProperty,
  11823 + var BookmarkModel,
  11824 + __hasProp = Object.prototype.hasOwnProperty,
11820 11825 __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
11821 11826
  11827 + BookmarkModel = require('models/bookmark_model').BookmarkModel;
  11828 +
11822 11829 exports.BookmarkFormView = (function(_super) {
11823 11830
11824 11831 __extends(BookmarkFormView, _super);
@@ -11834,7 +11841,11 @@ window.jQuery = window.$ = jQuery;
11834 11841 BookmarkFormView.prototype.template = _.template($("#bookmark-form-modal-template").html());
11835 11842
11836 11843 BookmarkFormView.prototype.events = {
11837   - "click #submit": "submitForm"
  11844 + "submit": "submitForm"
  11845 + };
  11846 +
  11847 + BookmarkFormView.prototype.initialize = function() {
  11848 + return app.collections.bookmarks.bind("show-bookmark-form", this.showBookmarkForm);
11838 11849 };
11839 11850
11840 11851 BookmarkFormView.prototype.render = function() {
@@ -11843,8 +11854,34 @@ window.jQuery = window.$ = jQuery;
11843 11854 };
11844 11855
11845 11856 BookmarkFormView.prototype.submitForm = function(e) {
  11857 + var bookmark, datas, id;
11846 11858 e.preventDefault();
11847   - return this;
  11859 + id = $("#id").val();
  11860 + if (_.isNumber(id)) {
  11861 + bookmark = app.collections.bookmarks.get($("#id").val());
  11862 + } else {
  11863 + bookmark = new BookmarkModel;
  11864 + }
  11865 + datas = {
  11866 + link: $("#link").val(),
  11867 + title: $("#title").val(),
  11868 + description: $("#description").val(),
  11869 + tags: $("#tags").val(),
  11870 + csrf: $("#csrf").val()
  11871 + };
  11872 + if (!_.isNumber(id)) app.collections.bookmarks.add(bookmark);
  11873 + bookmark.save(datas, {
  11874 + error: function(model, errors) {
  11875 + return console.log("error", model, errors);
  11876 + },
  11877 + success: function(model, errors) {
  11878 + return console.log("success", model, errors);
  11879 + }
  11880 + });
  11881 + };
  11882 +
  11883 + BookmarkFormView.prototype.showBookmarkForm = function(bookmarkId) {
  11884 + return console.log("catchec", bookmarkId);
11848 11885 };
11849 11886
11850 11887 return BookmarkFormView;
@@ -11881,12 +11918,7 @@ window.jQuery = window.$ = jQuery;
11881 11918 };
11882 11919
11883 11920 BookmarkView.prototype.editBookmark = function() {
11884   - console.log(this);
11885   - $("#link").val(this.model.get("link"));
11886   - $("#title").val(this.model.get("title"));
11887   - $("#description").val(this.model.get("description"));
11888   - $("#tags").val(this.model.get("tags"));
11889   - return $("#bookmark-form-modal").modal("toggle");
  11921 + return this.trigger("show-bookmark-form", $('#id').val());
11890 11922 };
11891 11923
11892 11924 return BookmarkView;
2  brunch/src/app/collections/tagscloud_collection.coffee
@@ -25,3 +25,5 @@ class exports.TagscloudCollection extends Backbone.Collection
25 25 e.get('name')
26 26 filters = list.join('+')
27 27 return filters
  28 + parse: (response) ->
  29 + response.tags
1  brunch/src/app/models/bookmark_model.coffee
... ... @@ -1,2 +1 @@
1 1 class exports.BookmarkModel extends Backbone.Model
2   -
48 brunch/src/app/views/bookmark_form_view.coffee
... ... @@ -1,13 +1,37 @@
  1 +BookmarkModel = require('models/bookmark_model').BookmarkModel
  2 +
1 3 class exports.BookmarkFormView extends Backbone.View
2   - tagName: 'div'
3   - el: $('#bookmark-form-modal')
4   - template: _.template($("#bookmark-form-modal-template").html())
5   - events:
6   - "click #submit": "submitForm"
7   - render: ->
8   - $(@el).html(@template())
9   - @
10   - submitForm: (e) ->
11   - e.preventDefault()
12   -
13   - @
  4 + tagName: 'div'
  5 + el: $('#bookmark-form-modal')
  6 + template: _.template($("#bookmark-form-modal-template").html())
  7 + events:
  8 + "submit": "submitForm"
  9 + initialize: ->
  10 + app.collections.bookmarks.bind "show-bookmark-form", @showBookmarkForm
  11 + render: ->
  12 + $(@el).html(@template())
  13 + @
  14 + submitForm: (e) ->
  15 + e.preventDefault()
  16 + id = $("#id").val()
  17 + if _.isNumber id
  18 + bookmark = app.collections.bookmarks.get $("#id").val()
  19 + else
  20 + bookmark = new BookmarkModel
  21 + datas =
  22 + link: $("#link").val()
  23 + title: $("#title").val()
  24 + description: $("#description").val()
  25 + tags: $("#tags").val()
  26 + csrf: $("#csrf").val()
  27 +
  28 + if !_.isNumber id
  29 + app.collections.bookmarks.add bookmark
  30 + bookmark.save datas,
  31 + error: (model, errors) ->
  32 + console.log "error", model, errors
  33 + success: (model, errors) ->
  34 + console.log "success", model, errors
  35 + return
  36 + showBookmarkForm: (bookmarkId) ->
  37 + console.log "catchec", bookmarkId
7 brunch/src/app/views/bookmark_view.coffee
@@ -8,9 +8,4 @@ class exports.BookmarkView extends Backbone.View
8 8 $(@el).html bookmarkTemplate(bookmark: @model.toJSON())
9 9 @
10 10 editBookmark: ->
11   - console.log @
12   - $("#link").val @model.get "link"
13   - $("#title").val @model.get "title"
14   - $("#description").val @model.get "description"
15   - $("#tags").val @model.get "tags"
16   - $("#bookmark-form-modal").modal "toggle"
  11 + @trigger "show-bookmark-form", $('#id').val()

0 comments on commit 89366ca

Please sign in to comment.
Something went wrong with that request. Please try again.