def build_schema(self): schema = Resource.build_schema(self) for field_name, field_object in self.fields.items(): help_text = field_object.help_text if field_object.attribute: mf = self._meta.object_model._meta.get_field_by_name(field_object.attribute)[0] # try to fix help_text from the model field if not help_text or help_text == type(field_object).help_text: ht = mf.help_text if ht: help_text = ht # try to fix help_text from other side verbose name for relationships if not help_text or help_text == type(field_object).help_text: if isinstance(field_object,SetField): help_text = mf.rel.to._meta.verbose_name_plural elif isinstance(field_object,ToOneField): help_text = mf.rel.to._meta.verbose_name schema['fields'][field_name]['verbose_name'] = mf.verbose_name if isinstance(field_object,RelatedField): # add embedded field descriptors for full-filled relation if field_object.full: fields = field_object.to_class().build_schema()['fields'] for fn,fo in field_object.to_class().fields.items(): if getattr(fo,'hidden',False): del fields[fn] schema['fields'][field_name]['fields'] = fields schema['fields'][field_name]['help_text'] = help_text if hasattr(field_object,'full'): schema['fields'][field_name]['full'] = field_object.full if not field_object.full: schema['fields'][field_name]['schema_uri'] = field_object.to_class()._build_reverse_url( "api_get_schema", kwargs=field_object.to_class().resource_uri_kwargs() ) if 'resource_uri' in schema['fields']: schema['fields']['resource_uri']['help_text'] = _("URI to access the object using API") schema['fields']['resource_uri']['verbose_name'] = _("Resource URI") return schema
def build_schema(self): schema = Resource.build_schema(self) for field_name, field_object in self.fields.items(): help_text = field_object.help_text if field_object.attribute: mf = self._meta.object_model._meta.get_field_by_name(field_object.attribute)[0] # try to fix help_text from the model field if not help_text or help_text == type(field_object).help_text: ht = mf.help_text if ht: help_text = ht # try to fix help_text from other side verbose name for relationships if not help_text or help_text == type(field_object).help_text: if isinstance(field_object, SetField): help_text = mf.rel.to._meta.verbose_name_plural elif isinstance(field_object, ToOneField): help_text = mf.rel.to._meta.verbose_name schema["fields"][field_name]["verbose_name"] = mf.verbose_name if isinstance(field_object, RelatedField): # add embedded field descriptors for full-filled relation if field_object.full: fields = field_object.to_class().build_schema()["fields"] for fn, fo in field_object.to_class().fields.items(): if getattr(fo, "hidden", False): del fields[fn] schema["fields"][field_name]["fields"] = fields schema["fields"][field_name]["help_text"] = help_text if hasattr(field_object, "full"): schema["fields"][field_name]["full"] = field_object.full if not field_object.full: schema["fields"][field_name]["schema_uri"] = field_object.to_class()._build_reverse_url( "api_get_schema", kwargs=field_object.to_class().resource_uri_kwargs() ) if "resource_uri" in schema["fields"]: schema["fields"]["resource_uri"]["help_text"] = _("URI to access the object using API") schema["fields"]["resource_uri"]["verbose_name"] = _("Resource URI") return schema
def test_case_fields(self): expected_fields = [ {"help_text": "", "key": "properties__level", "label": "Level", "type": "unicode"}, {"help_text": "", "key": "properties__mood", "label": "Mood", "type": "unicode"}, {"help_text": "", "key": "properties__move_type", "label": "Move type", "type": "unicode"}, {"help_text": "", "key": "properties__name", "label": "Name", "type": "unicode"}, {"help_text": "", "key": "properties__opened_on", "label": "Opened on", "type": "unicode"}, {"help_text": "", "key": "properties__owner_id", "label": "Owner id", "type": "unicode"}, {"help_text": "", "key": "properties__prop1", "label": "Prop1", "type": "unicode"}, {"help_text": "", "key": "properties__type", "label": "Type", "type": "unicode"}, {"help_text": "", "key": "date_closed", "label": "Date closed", "type": "unicode"}, {"help_text": "", "key": "xform_ids", "label": "XForm IDs", "type": "unicode"}, {"help_text": "", "key": "properties__date_opened", "label": "Date opened", "type": "unicode"}, {"help_text": "", "key": "properties__external_id", "label": "External ID", "type": "unicode"}, {"help_text": "", "key": "properties__case_name", "label": "Case name", "type": "unicode"}, {"help_text": "", "key": "properties__case_type", "label": "Case type", "type": "unicode"}, {"help_text": "", "key": "user_id", "label": "User ID", "type": "unicode"}, {"help_text": "", "key": "date_modified", "label": "Date modified", "type": "unicode"}, {"help_text": "", "key": "case_id", "label": "Case ID", "type": "unicode"}, {"help_text": "", "key": "properties__owner_id", "label": "Owner ID", "type": "unicode"}, {"help_text": "", "key": "resource_uri", "label": "Resource URI", "type": "unicode"} ] request = Client().get(self.test_url).wsgi_request bundle = Resource().build_bundle(data={}, request=request) factory = CaseFactory(domain="joto") factory.create_case( case_type='teddiursa', owner_id='owner1', case_name='dre', update={'prop1': 'blah', 'move_type': 'scratch', 'mood': 'happy', 'level': '100'} ) actual_fields = ZapierCustomFieldCaseResource().obj_get_list(bundle) for i in range(len(actual_fields)): self.assertEqual(expected_fields[i], actual_fields[i].get_content())
def __init__(self): Resource.__init__(self) self.staf_obj = wrapper_STAF.staf_obj
def get_schema(self, *args, **kw): return Resource.get_schema(self, *args, **kw)
def get_multiple(self, *args, **kw): return Resource.get_multiple(self, *args, **kw)
def dispatch_list(self, *args, **kw): return Resource.dispatch_list(self, *args, **kw)
def dispatch_detail(self, *args, **kw): return Resource.dispatch_detail(self, *args, **kw)
def __init__(self, dao, api_name): ''' Initialize. ''' Resource.__init__(self, api_name=api_name) self.dao = dao
def is_authenticated(self, request): # Abandons csrf check to simplify using API request._dont_enforce_csrf_checks = True return Resource.is_authenticated(self, request)
def dispatch(self, request_type, request, **kwargs): return Resource.dispatch(self, request_type, request, **kwargs)
def is_authenticated(self, request): # Abandons csrf check to simplify using API request._dont_enforce_csrf_checks = True return Resource.is_authenticated(self,request)
def dispatch_list(self,*args,**kw): return Resource.dispatch_list(self,*args,**kw)
def get_multiple(self,*args,**kw): return Resource.get_multiple(self,*args,**kw)
def get_schema(self,*args,**kw): return Resource.get_schema(self,*args,**kw)
def dispatch_detail(self,*args,**kw): return Resource.dispatch_detail(self,*args,**kw)
def wrap_view(self, view): return cache_page(Resource.wrap_view(self, view),timeout=60)
def deserialize_data(self, request): ''' Deserialize the data from the POST request to valid python object. ''' return Resource.deserialize(self, request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json')) # @IgnorePep8
def get_resource_uri(self, bundle=None, url_name=None): return Resource.get_resource_uri(self)
def test_case_fields(self): expected_fields = [{ "help_text": "", "key": "properties__level", "label": "Level", "type": "unicode" }, { "help_text": "", "key": "properties__mood", "label": "Mood", "type": "unicode" }, { "help_text": "", "key": "properties__move_type", "label": "Move type", "type": "unicode" }, { "help_text": "", "key": "properties__name", "label": "Name", "type": "unicode" }, { "help_text": "", "key": "properties__prop1", "label": "Prop1", "type": "unicode" }, { "help_text": "", "key": "properties__type", "label": "Type", "type": "unicode" }, { "help_text": "", "key": "date_closed", "label": "Date closed", "type": "unicode" }, { "help_text": "", "key": "xform_ids", "label": "XForm IDs", "type": "unicode" }, { "help_text": "", "key": "properties__date_opened", "label": "Date opened", "type": "unicode" }, { "help_text": "", "key": "properties__external_id", "label": "External ID", "type": "unicode" }, { "help_text": "", "key": "properties__case_name", "label": "Case name", "type": "unicode" }, { "help_text": "", "key": "properties__case_type", "label": "Case type", "type": "unicode" }, { "help_text": "", "key": "user_id", "label": "User ID", "type": "unicode" }, { "help_text": "", "key": "date_modified", "label": "Date modified", "type": "unicode" }, { "help_text": "", "key": "case_id", "label": "Case ID", "type": "unicode" }, { "help_text": "", "key": "properties__owner_id", "label": "Owner ID", "type": "unicode" }, { "help_text": "", "key": "resource_uri", "label": "Resource URI", "type": "unicode" }] request = Client().get(self.test_url).wsgi_request bundle = Resource().build_bundle(data={}, request=request) actual_fields = [ field.get_content() for field in ZapierCustomFieldCaseResource().obj_get_list(bundle) ] self.assertItemsEqual(expected_fields, actual_fields)
def __init__(self, *args, **kwargs): Resource.__init__(self, *args, **kwargs) self.base_uri = os.path.join(QUESTIONNAIRE_APP_URI, 'api/')