# -*- coding: utf-8 -*- from __future__ import unicode_literals from tastypie import fields from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS from tastypie.authorization import Authorization from tastypie.exceptions import ImmediateHttpResponse from core.models.user import User from api.validations.user_validation import UserValidation from api.resources.group_resource import GroupResource ''' ''' class UserResource(ModelResource): groups = fields.ToManyField(GroupResource, 'groups') class Meta: queryset = User.objects.all() authorization = Authorization() always_return_data = True validation = UserValidation() ''' ''' def save(self, bundle, skip_errors=False): if bundle.via_uri: return bundle self.is_valid(bundle) if bundle.errors and not skip_errors: raise ImmediateHttpResponse(response=self.error_response(bundle.request, bundle.errors)) user = User.objects.create_user(bundle.data['username'], bundle.data['email'], bundle.data['password']) bundle.obj = user bundle.data['groups'] = bundle.data.get('groups', []) if bundle.obj.pk: self.authorized_update_detail(self.get_object_list(bundle.request), bundle) else: self.authorized_create_detail(self.get_object_list(bundle.request), bundle) self.save_related(bundle) obj_id = self.create_identifier(bundle.obj) if obj_id not in bundle.objects_saved or bundle.obj._state.adding: bundle.obj.save() bundle.objects_saved.add(obj_id) m2m_bundle = self.hydrate_m2m(bundle) self.save_m2m(m2m_bundle) return bundle