def save(self): """ Implements the save method so this form can be used in regular django views. It does the same validation that it usually does for the API, but instead of creating a JSON response, it just creates the object and then returns it. """ assert hasattr(self, 'request') assert self.type == 'create' or self.type == 'update' # Use the form's cleaned_data to create a bundle bundle = Bundle() bundle.data = self.cleaned_data if hasattr(self, 'request'): bundle.request = self.request if hasattr(self, 'instance'): bundle.obj = self.instance # Use the resource's methods to save the bundle self.resource.request = self.request if self.type == 'create': bundle = self.resource.obj_create(bundle) elif self.type == 'update': assert self.request != None assert bundle.obj != None bundle = self.resource.obj_update(bundle, self.request) # Return the object return bundle.obj
def dehydrate(self, bundle, for_list=False): print 1 if not bundle.obj or not bundle.obj.pk: print 2 if not self.null: raise ApiFieldError("The model '%r' does not have a primary key and can not be d in a ToMany context." % bundle.obj) return [] if not getattr(bundle.obj, self.attribute): print 3 if not self.null: raise ApiFieldError("The model '%r' has an empty attribute '%s' and doesn't all a null value." % (bundle.obj, self.attribute)) return [] self.m2m_resources = [] m2m_dehydrated = [] # TODO: Also model-specific and leaky. Relies on there being a # ``Manager`` there. # NOTE: only had to remove .all() print 4 for m2m in getattr(bundle.obj, self.attribute): print 5 m2m_resource = self.get_related_resource(m2m) m2m_bundle = Bundle(obj=m2m) self.m2m_resources.append(m2m_resource) # youhou, dirty hack again baby! m2m_bundle.obj = type("DummyContainer", (object,), {'pk': m2m_bundle.obj}) m2m_dehydrated.append(self.dehydrate_related(m2m_bundle, m2m_resource)) return m2m_dehydrated
def test_update_default_phone_number_returns_error_if_invalid_format(self): bundle = Bundle() bundle.obj = self.user bundle.data = {"default_phone_number": ["50253311399"]} errors = CommCareUserResource._update(bundle) self.assertIn('default_phone_number must be a string', errors)
def test_update_unknown_fields_returns_error(self): bundle = Bundle() bundle.obj = self.user bundle.data = {"id": 'updated-id'} errors = CommCareUserResource._update(bundle) self.assertIn("Attempted to update unknown or non-editable field 'id'", errors)
def hydrate_lang(self, bundle): translated_bundle = Bundle() translation_resource = I4pProjectTranslationEditResource() for language_code, language_data in bundle.data["lang"].iteritems(): if language_code not in dict(LANGUAGES): continue translated_bundle.data = language_data translated_bundle.obj = bundle.obj.translate(language_code) translation_resource.obj_create(translated_bundle) return bundle
def hydrate_lang(self, bundle): translated_bundle = Bundle() translation_resource = I4pProjectTranslationEditResource() for language_code, language_data in bundle.data['lang'].iteritems(): if language_code not in dict(LANGUAGES): continue translated_bundle.data = language_data translated_bundle.obj = bundle.obj.translate(language_code) translation_resource.obj_create(translated_bundle) return bundle
def test_update_password_with_weak_passwords_returns_error_if_strong_option_on( self): self.domain_obj.strong_mobile_passwords = True self.domain_obj.save() bundle = Bundle() bundle.obj = self.user bundle.data = {"password": '******'} errors = CommCareUserResource._update(bundle) expected_error_message = 'Password is not strong enough. Try making your password more complex.' self.assertIn(expected_error_message, errors)
def test_update_user_data_returns_error_if_profile_conflict(self): bundle = Bundle() bundle.obj = self.user bundle.data = { 'user_data': { PROFILE_SLUG: self.profile.id, 'conflicting_field': 'no' } } errors = CommCareUserResource._update(bundle) self.assertIn( 'metadata properties conflict with profile: conflicting_field', errors)
def delete_detail(self, request, **kwargs): from freenasUI.freeadmin.navtree import navtree bundle = Bundle(request=request) bundle.obj = self.obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs)) if bundle.obj._meta.model._admin.delete_form: deserialized = self.deserialize( request, request.body or '{}', format=request.META.get('CONTENT_TYPE', 'application/json'), ) Form = __import__( f'{bundle.obj._meta.app_label}.forms', globals(), locals(), [bundle.obj._meta.model._admin.delete_form], 0, ) Form = getattr(Form, bundle.obj._meta.model._admin.delete_form) form = Form(data=deserialized, instance=bundle.obj) if not form.is_valid(): raise ImmediateHttpResponse( response=self.error_response(request, form.errors) ) # Grab the form to call delete on same as in freeadmin m = bundle.obj._meta.model mf = None if not isinstance(navtree._modelforms[m], dict): mf = navtree._modelforms[m] else: if mf is None: try: mf = navtree._modelforms[m][m._admin.edit_modelform] except Exception: mf = list(navtree._modelforms[m].values())[-1] else: mf = navtree._modelforms[m][mf] if mf: form = mf(instance=bundle.obj) form.delete() return http.HttpNoContent() else: return super().delete_detail(request, **kwargs)
def delete_detail(self, request, **kwargs): bundle = Bundle(request=request) bundle.obj = self.obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs)) if bundle.obj._meta.model._admin.delete_form: deserialized = self.deserialize( request, request.body or '{}', format=request.META.get('CONTENT_TYPE', 'application/json'), ) Form = __import__( f'{bundle.obj._meta.app_label}.forms', globals(), locals(), [bundle.obj._meta.model._admin.delete_form], 0, ) Form = getattr(Form, bundle.obj._meta.model._admin.delete_form) form = Form(data=deserialized, instance=bundle.obj) if not form.is_valid(): raise ImmediateHttpResponse( response=self.error_response(request, form.errors)) if bundle.obj._meta.model._admin.edit_modelform: ModelForm = __import__( f'{bundle.obj._meta.app_label}.forms', globals(), locals(), [bundle.obj._meta.model._admin.edit_modelform], 0, ) ModelForm = getattr(ModelForm, bundle.obj._meta.model._admin.edit_modelform) mf = ModelForm(instance=bundle.obj) mf.delete() return http.HttpNoContent() else: return super().delete_detail(request, **kwargs)