Skip to content

Commit

Permalink
all done except a "small" problem with bookmark creation and csrf pro…
Browse files Browse the repository at this point in the history
…tection
  • Loading branch information
Daroth committed Jan 10, 2012
1 parent 3c11f90 commit 89366ca
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 50 deletions.
27 changes: 7 additions & 20 deletions bookmark/blueprint/api/__init__.py
@@ -1,4 +1,4 @@
from flask import Blueprint, request
from flask import Blueprint, request, jsonify
from functools import wraps


Expand All @@ -11,27 +11,15 @@
from .item.Tag import ItemTag
from .form import BookmarkForm

import json

b = Blueprint('api', __name__)

def json_result_decorator(f):
@wraps(f)
def json_result(*args, **kwargs):
dumped = json.dumps(f(*args, **kwargs))
response = app.make_response(dumped)
response.mimetype = 'application/json'
return response
return json_result

@b.route('/', methods=['GET', ])
def index():
return VERSION


@b.route('/bookmarks/', methods=['GET', ])
@b.route('/bookmarks/<string:tags>', methods=['GET', ])
@json_result_decorator
def bookmarks(tags=None):
filters = []
if tags is not None:
Expand All @@ -57,26 +45,25 @@ def bookmarks(tags=None):
"total": total,
}

return ret
return jsonify(ret)


@b.route('/bookmarks/', methods=['POST', ])
@json_result_decorator
def add_bookmark():
form = BookmarkForm(create=True)
form = BookmarkForm(create=True, json=request.json)
ret = None
if form.validate_on_submit():
# register form
add_bookmark(form.bookmark)
ret = ["OK"]
else:
ret = form.errors
return ret
return jsonify(ret)



@b.route('/tagcloud', methods=['GET', ])
@b.route('/tagcloud/<string:tags>', methods=['GET', ])
@json_result_decorator
def tagcloud(tags=None):
filters = []
filters_tags = []
Expand All @@ -94,6 +81,6 @@ def tagcloud(tags=None):

tags_list = map(lambda x: x.json(), filter_list)
process_tag_count(tags_list, max_percent=30, min_percent=11)
tags_list = [x.json() for x in filters_tags] + tags_list
tags_list = {"tags": [x.json() for x in filters_tags] + tags_list}

return tags_list
return jsonify(tags_list)
16 changes: 15 additions & 1 deletion bookmark/blueprint/api/form/__init__.py
Expand Up @@ -5,6 +5,7 @@
from flaskext.wtf import TextAreaField
from flaskext.wtf import SubmitField
from flaskext.wtf.html5 import URLField
from bookmark.blueprint.api.item.Bookmark import ItemBookmark

class BookmarkForm(Form):
id = HiddenField(u'Id')
Expand All @@ -14,14 +15,27 @@ class BookmarkForm(Form):
tags = TextField(u'Tags')
submit = SubmitField(u'Add')

def __init__(self, create, *args, **kwargs):
def __init__(self, create, json=None, *args, **kwargs):
Form.__init__(self, *args, **kwargs)
self.create = create
if json is not None:
self.link.data = json['link']
self.title.data = json['title']
self.description.data = json['description']
self.tags.data = json['tags']
#self.csrf.data = json['csrf']
self.bookmark = None

def validate(self):
rv = Form.validate(self)
if not rv:
return False

link = self.link.data
title = self.title.data
description = self.description.data
tags = map(lambda x : x.strip(), self.tags.data.split[','])

# TODO : add id control if create is False (modification mode need a valid bookmark id)
self.bookmark = ItemBookmark(ptags=tags, plink=link, ptitle=title, pdescription=description)
return True
2 changes: 1 addition & 1 deletion bookmark/blueprint/login/__init__.py
Expand Up @@ -26,7 +26,7 @@ def login():
form = LoginForm()
if form.validate_on_submit():
# login and valide the user...
login_user(form.user)
login_user(form.user, remember=form.remember.data)
return redirect(request.args.get('next') or url_for('web.index'))
return render_template('login.html', form=form)

Expand Down
50 changes: 41 additions & 9 deletions bookmark/web/js/app.js
Expand Up @@ -11412,6 +11412,10 @@ window.jQuery = window.$ = jQuery;
return filters;
};

TagscloudCollection.prototype.parse = function(response) {
return response.tags;
};

return TagscloudCollection;

})(Backbone.Collection);
Expand Down Expand Up @@ -11816,9 +11820,12 @@ window.jQuery = window.$ = jQuery;

}).call(this);
}, "views/bookmark_form_view": function(exports, require, module) {(function() {
var __hasProp = Object.prototype.hasOwnProperty,
var BookmarkModel,
__hasProp = Object.prototype.hasOwnProperty,
__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; };

BookmarkModel = require('models/bookmark_model').BookmarkModel;

exports.BookmarkFormView = (function(_super) {

__extends(BookmarkFormView, _super);
Expand All @@ -11834,7 +11841,11 @@ window.jQuery = window.$ = jQuery;
BookmarkFormView.prototype.template = _.template($("#bookmark-form-modal-template").html());

BookmarkFormView.prototype.events = {
"click #submit": "submitForm"
"submit": "submitForm"
};

BookmarkFormView.prototype.initialize = function() {
return app.collections.bookmarks.bind("show-bookmark-form", this.showBookmarkForm);
};

BookmarkFormView.prototype.render = function() {
Expand All @@ -11843,8 +11854,34 @@ window.jQuery = window.$ = jQuery;
};

BookmarkFormView.prototype.submitForm = function(e) {
var bookmark, datas, id;
e.preventDefault();
return this;
id = $("#id").val();
if (_.isNumber(id)) {
bookmark = app.collections.bookmarks.get($("#id").val());
} else {
bookmark = new BookmarkModel;
}
datas = {
link: $("#link").val(),
title: $("#title").val(),
description: $("#description").val(),
tags: $("#tags").val(),
csrf: $("#csrf").val()
};
if (!_.isNumber(id)) app.collections.bookmarks.add(bookmark);
bookmark.save(datas, {
error: function(model, errors) {
return console.log("error", model, errors);
},
success: function(model, errors) {
return console.log("success", model, errors);
}
});
};

BookmarkFormView.prototype.showBookmarkForm = function(bookmarkId) {
return console.log("catchec", bookmarkId);
};

return BookmarkFormView;
Expand Down Expand Up @@ -11881,12 +11918,7 @@ window.jQuery = window.$ = jQuery;
};

BookmarkView.prototype.editBookmark = function() {
console.log(this);
$("#link").val(this.model.get("link"));
$("#title").val(this.model.get("title"));
$("#description").val(this.model.get("description"));
$("#tags").val(this.model.get("tags"));
return $("#bookmark-form-modal").modal("toggle");
return this.trigger("show-bookmark-form", $('#id').val());
};

return BookmarkView;
Expand Down
2 changes: 2 additions & 0 deletions brunch/src/app/collections/tagscloud_collection.coffee
Expand Up @@ -25,3 +25,5 @@ class exports.TagscloudCollection extends Backbone.Collection
e.get('name')
filters = list.join('+')
return filters
parse: (response) ->
response.tags
1 change: 0 additions & 1 deletion brunch/src/app/models/bookmark_model.coffee
@@ -1,2 +1 @@
class exports.BookmarkModel extends Backbone.Model

48 changes: 36 additions & 12 deletions brunch/src/app/views/bookmark_form_view.coffee
@@ -1,13 +1,37 @@
BookmarkModel = require('models/bookmark_model').BookmarkModel

class exports.BookmarkFormView extends Backbone.View
tagName: 'div'
el: $('#bookmark-form-modal')
template: _.template($("#bookmark-form-modal-template").html())
events:
"click #submit": "submitForm"
render: ->
$(@el).html(@template())
@
submitForm: (e) ->
e.preventDefault()

@
tagName: 'div'
el: $('#bookmark-form-modal')
template: _.template($("#bookmark-form-modal-template").html())
events:
"submit": "submitForm"
initialize: ->
app.collections.bookmarks.bind "show-bookmark-form", @showBookmarkForm
render: ->
$(@el).html(@template())
@
submitForm: (e) ->
e.preventDefault()
id = $("#id").val()
if _.isNumber id
bookmark = app.collections.bookmarks.get $("#id").val()
else
bookmark = new BookmarkModel
datas =
link: $("#link").val()
title: $("#title").val()
description: $("#description").val()
tags: $("#tags").val()
csrf: $("#csrf").val()

if !_.isNumber id
app.collections.bookmarks.add bookmark
bookmark.save datas,
error: (model, errors) ->
console.log "error", model, errors
success: (model, errors) ->
console.log "success", model, errors
return
showBookmarkForm: (bookmarkId) ->
console.log "catchec", bookmarkId
7 changes: 1 addition & 6 deletions brunch/src/app/views/bookmark_view.coffee
Expand Up @@ -8,9 +8,4 @@ class exports.BookmarkView extends Backbone.View
$(@el).html bookmarkTemplate(bookmark: @model.toJSON())
@
editBookmark: ->
console.log @
$("#link").val @model.get "link"
$("#title").val @model.get "title"
$("#description").val @model.get "description"
$("#tags").val @model.get "tags"
$("#bookmark-form-modal").modal "toggle"
@trigger "show-bookmark-form", $('#id').val()

0 comments on commit 89366ca

Please sign in to comment.