Ejemplo n.º 1
0
def create_implicit_fields(category, stores_end_time=False):
    for field in implicit_fields:
        if stores_end_time or field.get('key') != 'EndTime':
            Field.create(
                field.get('name'),
                field.get('key'),
                '',
                False,
                category,
                field.get('type')
            )
Ejemplo n.º 2
0
def create(project, user, input_dict):
    fields = []

    category = Category.objects.create(
        name=input_dict.get('name'),
        description=input_dict.get('description'),
        project=project,
        creator=user
    )

    for key in sorted(input_dict):
        if key.startswith('field'):
            order = key[key.find('_'):]
            field_type = input_dict.get('type' + order)
            imp = input_dict.get('import' + order)

            if imp == 'true':
                field = Field.create(
                    input_dict.get(key),
                    slugify(input_dict.get(key)),
                    '',
                    False,
                    category,
                    field_type
                )

                fields.append(field.key)
            else:
                fields.append('-1')

    return category, fields
Ejemplo n.º 3
0
    def post(self, request, project_id):
        name = self.request.POST.get('checklistName')
        description = self.request.POST.get('checklistDescription')
        creator = self.request.user
        project = None
        checklist_settings = None

        #not the most robust method; need to rework...
        if int(project_id) == 999999:
            project = Project.create("MyChecklist", "", True, False, 'auth', creator) #can be true, auth, or false
            checklist_settings = ChecklistSettings.objects.create(
                project=project,
                reminderson=True,
                frequencyonexpiration='twice',
                #frequencybeforeexpiration='one_week'
            )
        else:
            project = Project.objects.get_single(self.request.user, project_id)
            checklist_settings = ChecklistSettings.objects.get(project=project)

        default_status = 'active' #can be 'active' or 'pending'

        category = Category.objects.create(
            project=project,
            creator=creator,
            name=name,
            description=description,
            default_status=default_status
        )

        latitude = self.request.POST.get('checklistLat')
        longitude = self.request.POST.get('checklistLng')
        geom_point = Point((float(longitude), float(latitude)))
        geometry = GEOSGeometry(geom_point)
        created_at = datetime.datetime.now()
        location_status = 'active' #can be 'active' or 'pending'

        location = Location.objects.create(
            name=name,
            description=description,
            geometry=geometry,
            created_at=created_at,
            creator=creator,
            private_for_project=project,
            status=location_status
        )

        data = {} #e.g. {'text': 'Text', 'number': 12}
        observation_status = 'active' #can be 'active' or 'pending'

        observation = Observation.create(
            properties=data,
            creator=creator,
            location=location,
            project=project,
            category=category,
            status=observation_status
        )

        checklisttype = self.request.POST.get('checklistType')
        numberofpeople = self.request.POST.get('checklistNumPeople')
        numberofchildren = self.request.POST.get('checklistNumChildren')
        numberoftoddlers = self.request.POST.get('checklistNumToddlers')
        numberofinfants = self.request.POST.get('checklistNumInfants')
        numberofpets = self.request.POST.get('checklistNumPets')

        checklist = Checklist.objects.create(
            name=name,
            description=description,
            project=project,
            category=category,
            creator=creator,
            checklisttype=checklisttype,
            numberofpeople=numberofpeople,
            numberofchildren=numberofchildren,
            numberoftoddlers=numberoftoddlers,
            numberofinfants=numberofinfants,
            numberofpets=numberofpets,
            latitude=latitude,
            longitude=longitude
        )

        default_items = DEFAULT_ITEMS

        for item_dict in default_items:

            dict_checklisttype = ""
            dict_name = ""
            dict_checklistitemdescription = ""
            dict_checklistitemurl = ""
            dict_checklistitemtype = ""
            dict_quantityfactor = ""
            dict_pertype = ""
            dict_quantityunit = ""
            dict_expiryfactor = ""

            for key in item_dict:
                if key == "checklisttype":
                    dict_checklisttype = item_dict[key]
                elif key == "name":
                    dict_name = item_dict[key]
                elif key == "checklistitemdescription":
                    dict_checklistitemdescription = item_dict[key]
                elif key == "checklistitemurl":
                    dict_checklistitemurl = item_dict[key]
                elif key == "checklistitemtype":
                    dict_checklistitemtype = item_dict[key]
                elif key == "quantityfactor":
                    dict_quantityfactor = item_dict[key]
                elif key == "pertype":
                    dict_pertype = item_dict[key]
                elif key == "quantityunit":
                    dict_quantityunit = item_dict[key]
                elif key == "expiryfactor":
                    dict_expiryfactor = item_dict[key]

            #create the default items from the given values
            if dict_checklisttype == checklisttype:
                totalnumber = 1
                quantity = 0

                if dict_checklistitemtype == "Essential" or dict_checklistitemtype == "Useful" or dict_checklistitemtype == "Personal":
                    totalnumber_float = float(numberofpeople) + (float(numberofchildren) * 0.5) + (float(numberoftoddlers) * 0.3) + (float(numberofinfants) * 0.1) + (float(numberofpets) * 0.1)
                    totalnumber = int(math.ceil(totalnumber_float))
                elif dict_checklistitemtype == "Children":
                    totalnumber = int(numberofchildren)
                elif dict_checklistitemtype == "Toddlers":
                    totalnumber = int(numberoftoddlers)
                elif dict_checklistitemtype == "Infants":
                    totalnumber = int(numberofinfants)
                elif dict_checklistitemtype == "Pets":
                    totalnumber = int(numberofpets)
                else:
                    totalnumber = 1  # for 'Custom' or 'Fixit'

                if dict_pertype == "location":
                    quantity = int(dict_quantityfactor)  # this is for an item per household (e.g. first aid kit)
                else:
                    quantity = totalnumber * int(dict_quantityfactor)  # this is for an item per member of household (e.g. water)

                if quantity > 0:
                    field = Field.create(dict_name, dict_name, "", False, category, 'TextField')

                    checklist_item = ChecklistItem.objects.create(
                        name=dict_name,
                        project=project,
                        category=category,
                        field=field,
                        creator=creator,
                        # checklisttype=checklisttype,
                        checklistitemdescription=dict_checklistitemdescription,
                        checklistitemurl=dict_checklistitemurl,
                        checklistitemtype=dict_checklistitemtype,
                        quantityfactor=dict_quantityfactor,
                        pertype=dict_pertype,
                        quantity=quantity,
                        quantityunit=dict_quantityunit,
                        expiryfactor=dict_expiryfactor,
                        expiry=None,
                        haveit=False
                    )

        successful_message = checklist.name + " has been added."
        messages.success(self.request, successful_message)
        return redirect('geokey_checklist:index', checklist_id=category.id)
Ejemplo n.º 4
0
    def post(self, request, project_id, checklist_id):
        project = Project.objects.get_single(self.request.user, project_id)
        category = Category.objects.get_single(self.request.user, project_id, checklist_id)
        checklist = Checklist.objects.get(project=project,category=category)
        numberofpeople = checklist.numberofpeople
        numberofchildren = checklist.numberofchildren
        numberoftoddlers = checklist.numberoftoddlers
        numberofinfants = checklist.numberofinfants
        numberofpets = checklist.numberofpets

        name = self.request.POST.get('checklistItemName')
        project = Project.objects.get_single(self.request.user, project_id)
        category = Category.objects.get_single(
            self.request.user,
            project_id,
            checklist_id
        )
        creator = self.request.user

        checklistitemtype = self.request.POST.get('checklistItemType')

        checklistitemdescription = self.request.POST.get('checklistItemDescription')
        checklistitemurl = self.request.POST.get('checklistItemURL')
        quantityfactor_str = self.request.POST.get('checklistItemQuantityFactor')
        quantityfactor = int(quantityfactor_str)

        pertype = self.request.POST.get('checklistItemPerType')

        quantityunit = self.request.POST.get('checklistItemQuantityUnit')
        expiryfactor = self.request.POST.get('checklistItemExpiry')
        expiry = None
        haveit = False

        totalnumber = 1
        quantity = 0

        if checklistitemtype == "Essential" or checklistitemtype == "Useful" or checklistitemtype == "Personal":
            totalnumber_float = float(numberofpeople) + (float(numberofchildren) * 0.5) + (float(numberoftoddlers) * 0.3) + (float(numberofinfants) * 0.1) + (float(numberofpets) * 0.1)
            totalnumber = int(math.ceil(totalnumber_float))
        elif checklistitemtype == "Children":
            totalnumber = numberofchildren
        elif checklistitemtype == "Toddlers":
            totalnumber = numberoftoddlers
        elif checklistitemtype == "Infants":
            totalnumber = numberofinfants
        elif checklistitemtype == "Pets":
            totalnumber = numberofpets
        else:
            totalnumber = 1 #for 'Custom' or 'Fixit'

        if pertype == "location":
            quantity = quantityfactor # this is for an item per household (e.g. first aid kit)
        else:
            quantity = totalnumber * quantityfactor # this is for an item per member of household (e.g. water)

        field = Field.create(name, name, "", False, category, 'TextField')

        checklist_item = ChecklistItem.objects.create(
            name=name,
            project=project,
            category=category,
            field=field,
            creator=creator,
            #checklisttype=checklisttype,
            checklistitemdescription=checklistitemdescription,
            checklistitemurl=checklistitemurl,
            checklistitemtype=checklistitemtype,
            quantityfactor=quantityfactor,
            pertype=pertype,
            quantity=quantity,
            quantityunit=quantityunit,
            expiryfactor=expiryfactor,
            expiry=expiry,
            haveit=haveit
        )
        successful_message = checklist_item.name + " has been added."
        messages.success(self.request, successful_message)
        return redirect('geokey_checklist:index', checklist_id=category.id)
Ejemplo n.º 5
0
def create_project(sapelli_project_info, user, sap_file_path=None):
    geokey_project = Project.create(
        name=sapelli_project_info.get('display_name'),
        description='',
        isprivate=True,
        islocked=True,
        everyone_contributes=EVERYONE_CONTRIBUTES.false,
        creator=user
    )

    # If anything below fails the geokey_project will be deleted:
    try:
        sapelli_project = SapelliProject.objects.create(
            geokey_project=geokey_project,
            name=sapelli_project_info.get('name'),
            variant=sapelli_project_info.get('variant'),
            version=sapelli_project_info.get('version'),
            sapelli_id=sapelli_project_info.get('sapelli_id'),
            sapelli_fingerprint=sapelli_project_info.get('sapelli_fingerprint'),
            sapelli_model_id=sapelli_project_info.get('sapelli_model_id'),
            dir_path=sapelli_project_info.get('installation_path'),
            sap_path=sap_file_path
        )

        for form in sapelli_project_info.get('forms'):
            category = Category.objects.create(
                project=geokey_project,
                creator=user,
                name=form.get('sapelli_id'),
                description='',
                default_status='active'
            )
            sapelli_form = SapelliForm.objects.create(
                category=category,
                sapelli_project=sapelli_project,
                sapelli_id=form.get('sapelli_id'),
                sapelli_model_schema_number=form.get('sapelli_model_schema_number')
            )

            create_implicit_fields(category, stores_end_time=form.get('stores_end_time'))

            for location in form.get('locations'):
                LocationField.objects.create(
                    sapelli_form=sapelli_form,
                    sapelli_id=location.get('sapelli_id'),
                )

            for field in form.get('fields'):
                field_type = field.get('geokey_type')

                name = field.get('caption')
                if not name:
                    name = field.get('sapelli_id')

                geokey_field = Field.create(
                    name,
                    slugify(name),
                    field.get('description') if field.get('description') else '',
                    False,
                    category,
                    field_type
                )

                sapelli_field = SapelliField.objects.create(
                    sapelli_form=sapelli_form,
                    sapelli_id=field.get('sapelli_id'),
                    field=geokey_field,
                    truefalse=field.get('truefalse')
                )

                if field_type == 'LookupField':
                    # Loop over items:
                    for idx, item in enumerate(field.get('items')):
                        # Image:
                        img_relative_path = item.get('img')
                        img_file = None
                        if img_relative_path and sapelli_project.dir_path:
                            try:
                                img_file = File(open(os.path.join(sapelli_project.dir_path, 'img/', img_relative_path), 'rb'))
                            except IOError:
                                pass
                        # Value:
                        value = LookupValue.objects.create(
                            name=item.get('value'),
                            field=geokey_field,
                            symbol=img_file
                        )
                        # Create SapelliItem:
                        SapelliItem.objects.create(
                            lookup_value=value,
                            sapelli_field=sapelli_field,
                            number=idx
                        )
    except BaseException, e:
        try:  # delete geokey_project:
            geokey_project.delete()
        except BaseException:
            pass
        raise e
Ejemplo n.º 6
0
def create_project(sapelli_project_info, user, sap_file_path=None):
    geokey_project = Project.create(
        name=sapelli_project_info.get('display_name'),
        description='',
        isprivate=True,
        islocked=True,
        everyone_contributes=EVERYONE_CONTRIBUTES.false,
        creator=user)

    # If anything below fails the geokey_project will be deleted:
    try:
        sapelli_project = SapelliProject.objects.create(
            geokey_project=geokey_project,
            name=sapelli_project_info.get('name'),
            variant=sapelli_project_info.get('variant'),
            version=sapelli_project_info.get('version'),
            sapelli_id=sapelli_project_info.get('sapelli_id'),
            sapelli_fingerprint=sapelli_project_info.get(
                'sapelli_fingerprint'),
            sapelli_model_id=sapelli_project_info.get('sapelli_model_id'),
            dir_path=sapelli_project_info.get('installation_path'),
            sap_path=sap_file_path)

        for form in sapelli_project_info.get('forms'):
            category = Category.objects.create(project=geokey_project,
                                               creator=user,
                                               name=form.get('sapelli_id'),
                                               description='',
                                               default_status='active')
            sapelli_form = SapelliForm.objects.create(
                category=category,
                sapelli_project=sapelli_project,
                sapelli_id=form.get('sapelli_id'),
                sapelli_model_schema_number=form.get(
                    'sapelli_model_schema_number'))

            create_implicit_fields(category,
                                   stores_end_time=form.get('stores_end_time'))

            for location in form.get('locations'):
                LocationField.objects.create(
                    sapelli_form=sapelli_form,
                    sapelli_id=location.get('sapelli_id'),
                )

            for field in form.get('fields'):
                field_type = field.get('geokey_type')

                name = field.get('caption')
                if not name:
                    name = field.get('sapelli_id')

                geokey_field = Field.create(
                    name, slugify(name),
                    field.get('description') if field.get('description') else
                    '', False, category, field_type)

                sapelli_field = SapelliField.objects.create(
                    sapelli_form=sapelli_form,
                    sapelli_id=field.get('sapelli_id'),
                    field=geokey_field,
                    truefalse=field.get('truefalse'))

                if field_type == 'LookupField':
                    # Loop over items:
                    for idx, item in enumerate(field.get('items')):
                        # Image:
                        img_relative_path = item.get('img')
                        img_file = None
                        if img_relative_path and sapelli_project.dir_path:
                            try:
                                img_file = File(
                                    open(
                                        os.path.join(sapelli_project.dir_path,
                                                     'img/',
                                                     img_relative_path), 'rb'))
                            except IOError:
                                pass
                        # Value:
                        value = LookupValue.objects.create(
                            name=item.get('value'),
                            field=geokey_field,
                            symbol=img_file)
                        # Create SapelliItem:
                        SapelliItem.objects.create(lookup_value=value,
                                                   sapelli_field=sapelli_field,
                                                   number=idx)
    except BaseException, e:
        try:  # delete geokey_project:
            geokey_project.delete()
        except BaseException:
            pass
        raise e
Ejemplo n.º 7
0
def create_implicit_fields(category, stores_end_time=False):
    for field in implicit_fields:
        if stores_end_time or field.get('key') != 'EndTime':
            Field.create(field.get('name'), field.get('key'), '', False,
                         category, field.get('type'))