示例#1
0
文件: utils.py 项目: yangmaoer/geokey
def create_new_observation(si_pull, geo_tweet, tweet_category, text_field, tweet_id_field):
    """Create new observation based on the tweet.

    Parameters
    -----------
    si_pull: SocialInteractionPull

    geo_tweet: array of tweets

    tweet_category: Category Object

    text_field: TextField Object
    
    tweet_id_field: TweetID Object
    """
    point = geo_tweet['geometry']
    properties = {
        text_field.key: geo_tweet['text'],
        tweet_id_field.key: geo_tweet['id']
    }

    location = Location(
        geometry=point,
        creator=si_pull.socialaccount.user)

    Observation.create(
        properties=properties,
        location=location,
        project=si_pull.project,
        creator=si_pull.socialaccount.user,
        category=tweet_category,
        status='active')

    si_pull.updated_at = timezone.now()
    si_pull.save()
示例#2
0
    def test_update_draft_observation(self):
        creator = UserFactory()
        location = LocationFactory()
        category = CategoryFactory()
        TextFieldFactory.create(**{
            'key': 'text',
            'category': category,
            'required': True,
            'order': 0
        })
        NumericFieldFactory.create(**{
            'key': 'number',
            'category': category,
            'order': 1
        })
        data = {'number': 12}
        observation = Observation.create(properties=data,
                                         creator=creator,
                                         location=location,
                                         category=category,
                                         project=category.project,
                                         status='draft')

        updater = UserFactory()
        update = {'number': 13}
        observation.update(properties=update, updator=updater, status='draft')

        self.assertEqual(observation.properties.get('number'), 13)
        self.assertEqual(observation.version, 1)
示例#3
0
    def test_validate_full_invalid(self):
        creator = UserFactory()
        location = LocationFactory()
        category = CategoryFactory()
        TextFieldFactory.create(**{
            'key': 'text',
            'category': category,
            'order': 0
        })
        NumericFieldFactory.create(**{
            'key': 'number',
            'category': category,
            'order': 1
        })
        data = {'text': 'Text', 'number': 12}
        observation = Observation.create(properties=data,
                                         creator=creator,
                                         location=location,
                                         category=category,
                                         project=category.project,
                                         status='active')

        updater = UserFactory()
        update = {'text': 'Udpated Text', 'number': 'abc', 'version': 1}
        Observation.validate_full(category=category, data=update)
        observation.update(properties=update, updator=updater)

        self.assertEqual(observation.properties, data)
        self.assertEqual(observation.version, 1)
示例#4
0
    def test_update_draft_observation(self):
        creator = UserF()
        location = LocationFactory()
        category = CategoryFactory()
        TextFieldFactory.create(**{
            'key': 'text',
            'category': category,
            'required': True,
            'order': 0
        })
        NumericFieldFactory.create(**{
            'key': 'number',
            'category': category,
            'order': 1
        })
        data = {'number': 12}
        observation = Observation.create(
            properties=data, creator=creator, location=location,
            category=category, project=category.project,
            status='draft'
        )

        updater = UserF()
        update = {'number': 13}
        observation.update(properties=update, updator=updater, status='draft')

        self.assertEqual(observation.properties.get('number'), 13)
        self.assertEqual(observation.version, 1)
示例#5
0
    def test_validate_full_invalid(self):
        creator = UserF()
        location = LocationFactory()
        category = CategoryFactory()
        TextFieldFactory.create(**{
            'key': 'text',
            'category': category,
            'order': 0
        })
        NumericFieldFactory.create(**{
            'key': 'number',
            'category': category,
            'order': 1
        })
        data = {'text': 'Text', 'number': 12}
        observation = Observation.create(
            properties=data, creator=creator, location=location,
            category=category, project=category.project, status='active'
        )

        updater = UserF()
        update = {'text': 'Udpated Text', 'number': 'abc', 'version': 1}
        Observation.validate_full(category=category, data=update)
        observation.update(properties=update, updator=updater)

        self.assertEqual(observation.properties, data)
        self.assertEqual(observation.version, 1)
示例#6
0
 def test_create_observation_active_default(self):
     creator = UserFactory()
     location = LocationFactory()
     category = CategoryFactory(**{'default_status': 'active'})
     TextFieldFactory(**{'key': 'text', 'category': category, 'order': 0})
     NumericFieldFactory(**{
         'key': 'number',
         'category': category,
         'order': 1
     })
     data = {'text': 'Text', 'number': 12}
     observation = Observation.create(properties=data,
                                      creator=creator,
                                      location=location,
                                      category=category,
                                      project=category.project,
                                      status='active')
     self.assertEqual(observation.properties, data)
示例#7
0
 def test_create_observation(self):
     creator = UserF()
     location = LocationFactory()
     category = CategoryFactory()
     TextFieldFactory(**{
         'key': 'text',
         'category': category,
         'order': 0
     })
     NumericFieldFactory(**{
         'key': 'number',
         'category': category,
         'order': 1
     })
     data = {'text': 'Text', 'number': 12}
     observation = Observation.create(
         properties=data, creator=creator, location=location,
         category=category, project=category.project, status='active'
     )
     self.assertEqual(observation.properties, data)
示例#8
0
 def test_create_observation_with_polish_chars(self):
     creator = UserF()
     location = LocationFactory()
     category = CategoryFactory()
     TextFieldFactory(**{
         'key': 'text',
         'category': category,
         'required': True,
         'order': 0
     })
     NumericFieldFactory(**{
         'key': 'number',
         'category': category,
         'order': 1
     })
     data = {'text': u'śmietnik', 'number': 12}
     observation = Observation.create(
         properties=data, creator=creator, location=location,
         category=category, project=category.project, status='active'
     )
     self.assertEqual(observation.properties, data)
示例#9
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)