def from_forms(cls, forms, domain): """Create a group from a set of forms. Walks through all of the forms' root tables and creates columns for each column in the form's table. If the column name and data type match a previously seen data column then the column is remapped to that column, otherwise a new column is created. Like many elements of CommCare HQ, the views and queries that are used by these models will pretty much exclusively work in mysql. """ if not forms: raise Exception("You can't create a group of empty forms!") # the name will just be the xmlns of the first form. We assume # that the majority of times (if not all times) this method will # be called is to harmonize a set of data across verisons of # forms with the same xmlns. name = forms[0].target_namespace now = datetime.utcnow() view_name = get_unique_value( FormDataGroup.objects, "view_name", format_table_name(name, prefix="view_", domain_name=domain.name) ) group = cls.objects.create(name=name, display_name=name, created=now, view_name=view_name, domain=domain) group.forms = forms group.save() for form in forms: group.add_form_columns(form) group.update_view() return group
def create_models(cls, formdef): """Create FormDefModel and ElementDefModel objects for a FormDef object.""" fdd = FormDefModel() table_name = format_table_name(formdef.target_namespace, formdef.version, formdef.domain_name) fdd.name = str(formdef.name) fdd.form_name = table_name fdd.target_namespace = formdef.target_namespace fdd.version = formdef.version fdd.uiversion = formdef.uiversion fdd.domain = formdef.domain try: fdd.save() except IntegrityError, e: raise IntegrityError( ("Schema %s already exists." % fdd.target_namespace) + " Did you remember to update your version number?" )