|
@@ -4,9 +4,12 @@ from tastypie import fields
|
|
|
from tastypie.resources import ModelResource
|
|
|
from tastypie.authorization import Authorization
|
|
|
from tastypie.exceptions import ImmediateHttpResponse
|
|
|
+from tastypie.utils import trailing_slash
|
|
|
+from django.conf.urls import url
|
|
|
from core.models.user import User
|
|
|
from api.validations.user_validation import UserValidation
|
|
|
from api.resources.group_resource import GroupResource
|
|
|
+import simplejson as json
|
|
|
|
|
|
'''
|
|
|
'''
|
|
@@ -19,6 +22,45 @@ class UserResource(ModelResource):
|
|
|
always_return_data = True
|
|
|
validation = UserValidation()
|
|
|
|
|
|
+ '''
|
|
|
+ '''
|
|
|
+ def prepend_urls(self):
|
|
|
+ return [
|
|
|
+ url(r'^(?P<resource_name>%s)/(?P<%s>.*?)/change_password%s$' % (self._meta.resource_name, self._meta.detail_uri_name, trailing_slash), self.wrap_view('change_password'), name='api_change_password')
|
|
|
+ ]
|
|
|
+
|
|
|
+ '''
|
|
|
+ '''
|
|
|
+ def change_password(self, request, **kwargs):
|
|
|
+ self.method_check(request, ['post'])
|
|
|
+ self.is_authenticated(request)
|
|
|
+
|
|
|
+ # Check content type
|
|
|
+ if request.content_type != 'application/json':
|
|
|
+ return self.create_response(request, {
|
|
|
+ 'error_message': 'request is not json'
|
|
|
+ })
|
|
|
+
|
|
|
+ # Check body
|
|
|
+ if not request.body:
|
|
|
+ return self.create_response(request, {
|
|
|
+ 'error_message': 'request body is empty'
|
|
|
+ })
|
|
|
+
|
|
|
+ # Check if password is present in body
|
|
|
+ if not 'password' in request.body:
|
|
|
+ return self.create_response(request, {
|
|
|
+ 'error_message': 'password is not provided'
|
|
|
+ })
|
|
|
+
|
|
|
+ bundle = self.build_bundle(obj=json.loads(request.body), request=request)
|
|
|
+
|
|
|
+ import pdb; pdb.set_trace()
|
|
|
+
|
|
|
+ return self.create_response(request, {
|
|
|
+ self._meta.resource_name: 'ok'
|
|
|
+ })
|
|
|
+
|
|
|
'''
|
|
|
'''
|
|
|
def save(self, bundle, skip_errors=False):
|
|
@@ -27,9 +69,11 @@ class UserResource(ModelResource):
|
|
|
|
|
|
self.is_valid(bundle)
|
|
|
|
|
|
+ # If bundle has errors send this reponse
|
|
|
if bundle.errors and not skip_errors:
|
|
|
raise ImmediateHttpResponse(response=self.error_response(bundle.request, bundle.errors))
|
|
|
|
|
|
+ # If object data is not persist create django auth user
|
|
|
if bundle.obj._state.adding:
|
|
|
user = User.objects.create_user(bundle.data['username'], bundle.data['email'], bundle.data['password'])
|
|
|
user.first_name = bundle.data.get('first_name', '')
|