Пример #1
0
    def add_form_columns(self, form):
        """Given a group and a form, create the DB objects for each column
           in the form (if necessary) and add the form's columns to the 
           group"""
        table_name = form.table_name
        column_names = form.get_data_column_names()
        column_types = form.get_data_column_types()
        for name, type in zip(column_names, column_types):
            # Get the pointer object for this form, or create it if
            # this is the first time we've used this form/column
            pointer = FormDataPointer.objects.get_or_create(form=form, column_name=name, data_type=type)[0]

            # Get or create the column group.  If any other column had this
            # name and data type it will be used with this.
            try:
                column_group = self.columns.get(name=name, data_type=type)

            except FormDataColumn.DoesNotExist:
                # add a second check for the name, so we don't have duplicate
                # names inside a single form definition which will make queries
                # pretty challenging
                name = get_unique_value(self.columns, "name", name)
                column_group = FormDataColumn.objects.create(name=name, data_type=type)
                # don't forget to add the newly created column to this
                # group of forms as well.
                self.columns.add(column_group)
                self.save()

            column_group.fields.add(pointer)
            column_group.save()
Пример #2
0
    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
Пример #3
0
def get_django_user_object(phone_info):
    """From a PhoneUserInfo object, automatically build a django user"""
    user = User()
    user.username = get_unique_value(User.objects, "username", phone_info.username, "")
    user.set_password(phone_info.password)
    user.first_name = ''
    user.last_name  = ''
    user.email = ""
    user.is_staff = False # Can't log in to admin site
    user.is_active = False # Activated upon receipt of confirmation
    user.is_superuser = False # Certainly not, although this makes login sad
    user.last_login =  datetime.datetime(1970,1,1)
    user.date_joined = datetime.datetime.utcnow()
    return user
Пример #4
0
def new_form_data_group(req):
    """Create a new model-defined group of forms"""
    form_validation = None
    if req.method == "POST":
        form = FormDataGroupForm(req.POST) 
        if form.is_valid():
            if "formdefs" in req.POST:
                form_ids = req.POST.getlist("formdefs")
                if not form_ids:
                    form_validation = "You must choose at least one form!"
                else:
                    new_group = form.save(commit=False)
                    new_group.domain = req.user.selected_domain
                    # TODO: better handling of the name attribute.  
                    new_group.name = get_unique_value(FormDataGroup.objects, "name", new_group.display_name)
                    forms = [FormDefModel.objects.get(id=form_id) for form_id in form_ids]
                    new_group.save()
                    new_group.forms = forms
                    new_group.save()
                    # don't forget to do all the default column updates as well.
                    for form in forms:
                        new_group.add_form_columns(form)
                    
                    # finally update the sql view
                    new_group.update_view()
                    
                    # when done, take them to the edit page for further tweaking
                    return HttpResponseRedirect(reverse('xformmanager.views.edit_form_data_group', 
                                            kwargs={"group_id": new_group.id }))
                    
                        
            else: 
                form_validation = "You must choose at least one form!"
    else:
        form = FormDataGroupForm()
        
    all_forms = FormDefModel.objects.filter(domain=req.user.selected_domain)\
                            .order_by("target_namespace", "version")
    return render_to_response(req, "xformmanager/edit_form_data_group.html", 
                                  {"group_form": form,
                                   "all_forms": all_forms,
                                   "selected_forms": [],
                                   "form_validation": form_validation })