def configure_model(self, attrs, field_name): ''' Hook for ResourceMeta class to call when initializing model class. Saves fields obtained from resource class backlinks ''' self.relationship = field_name self._set_method_names(relationship=field_name) if self.res_name is None: self.res_name = grammar.singularize(attrs.get('endpoint', 'unknown').strip('/'))
def _set_method_names(self, method_name=None, relationship=None): if self.method_name is None: if method_name is not None: self.method_name = method_name suffix = '' if method_name != '': suffix = '_{}'.format(method_name) elif relationship is not None: suffix = '_{}'.format(grammar.singularize(relationship)) else: return self.associate_method_name = 'associate{}'.format(suffix) self.disassociate_method_name = 'disassociate{}'.format(suffix)
def _set_method_names(self, method_name=None, relationship=None): if self.method_name is not None: return # provided in __init__, do not let metaclass override suffix = '' if method_name is not None: self.method_name = method_name if method_name != '': suffix = '_{}'.format(method_name) elif relationship is not None: suffix = '_{}'.format(grammar.singularize(relationship)) else: return self.associate_method_name = 'associate{}'.format(suffix) self.disassociate_method_name = 'disassociate{}'.format(suffix)
def _produce_doc(self, action='associate'): doc_relation = self.method_name if self.method_name else grammar.singularize(self.relationship) return """{title_action} {status_article} {status} with this {res_name}. =====API DOCS===== {title_action} {status_article} {status} with this {res_name}. :param {res_name}: Primary key or name of the {res_name} to {action} to. :type {res_name}: str :param {other_name}: Primary key or name of the {other_name} to be {action}d. :type {other_name}: str :returns: Dictionary of only one key "changed", which indicates whether the {action} succeeded. :rtype: dict =====API DOCS===== """.format( action=action, title_action=action.title(), status_article=grammar.article(doc_relation), status=doc_relation, res_name=self.res_name, other_name=self.other_name, )
def test_singulars(self): """English words changed from singular to plural""" self.assertEqual(grammar.singularize("inventories"), "inventory") self.assertEqual(grammar.singularize("job_templates"), "job_template") self.assertEqual(grammar.singularize("job_template"), "job_template")