(cliped from http://stackoverflow.com/a/21786764/117292)
The least obstrusive way is to use a CurrentUserMiddleware to store the current user in a thread local object:
from threading import local
_user = local()
| class Sample(): | |
| def __init__(self, values=None, *args, **kwargs): | |
| self.reset(values) | |
| def reset(self, values=None): | |
| values = values or (0, 0, None, None, None, 0) | |
| (self._total, self._cnt, self._min, self._max, self._first, self._ex2) = values | |
| def append(self, value): | |
| # self._all.append(value) |
(cliped from http://stackoverflow.com/a/21786764/117292)
The least obstrusive way is to use a CurrentUserMiddleware to store the current user in a thread local object:
from threading import local
_user = local()
| class MyModel(models.Model): | |
| foo = models.CharField(max_length=20) | |
| bar = models.CharField(max_length=20) | |
| def __setattr__(self, attrname, val): | |
| setter_func = 'setter_' + attrname | |
| if attrname in self.__dict__ and callable(getattr(self, setter_func, None)): | |
| super(MyModel, self).__setattr__(attrname, getattr(self, setter_func)(val)) | |
| else: | |
| super(MyModel, self).__setattr__(attrname, val) |
| from utils.api import action, actionurls | |
| from tastypie.resources import ModelResource | |
| from tastypie.authorization import Authorization | |
| class NoteResource(ModelResource): | |
| class Meta: | |
| object_class = Note | |
| queryset = Note.objects.all() | |
| authorization = Authorization() |
| function create_task(title) { | |
| return $.ajax({ | |
| url: '/api/v1/task/', | |
| dataType: "application/json", | |
| data: JSON.stringify({ | |
| title: title | |
| }), | |
| type:'POST', | |
| contentType:'application/json', |
| // note the $httpProvider | |
| // must be called AFTER login (ie ajax login) | |
| function($httpProvider, $http) { | |
| $httpProvider.defaults.headers.post['X-CSRFToken'] = (document.cookie.match(/csrftoken=([0-9a-zA-Z]*)/) || ['']).pop(); | |
| }; | |
| //alternately, dont' forget to install angular-cookies.js |
| Data.Model = Backbone.Model.extend({ | |
| toJSON: function(){ | |
| var clone = _.clone(this.attributes); | |
| _.each(clone, function (attr, idx) { | |
| if(attr.toJSON){ | |
| clone[idx] = attr.toJSON(); | |
| } | |
| }); | |
| return clone; | |
| } |