コード例 #1
0
    def update_data(self, data, user):
        """ Update locality data with new data.

        :param data: Data that will be inserted
        :type data: dict
        """
        import uuid
        from django.contrib.gis.geos import Point
        from localities.tasks import regenerate_cache, regenerate_cache_cluster

        self.validate_data(data)
        old_geom = None
        try:
            old_geom = [self.geom.x, self.geom.y]
            self.set_geom(data['lng'], data['lat'])
        except AttributeError:
            self.geom = Point(data['lng'], data['lat'])

        self.name = data['name']

        # there are some changes so create a new changeset
        changeset = Changeset.objects.create(
            social_user=user
        )
        self.changeset = changeset

        del data['lng']
        del data['lat']

        created = False
        if not self.pk:
            created = True
            self.domain = Domain.objects.get(name='Health')
            self.changeset = changeset
            self.uuid = uuid.uuid4().hex
            self.upstream_id = u'web¶{}'.format(self.uuid)
            self.save()

        self.set_specifications(data, changeset)
        if not created and self.tracker.changed():
            self.changeset = changeset
            self.save()

        # generate some attributes if location changed/created
        new_geom = [self.geom.x, self.geom.y]
        if new_geom != old_geom or created:
            try:
                self.update_what3words(user, changeset)
            except AttributeError:
                pass
            regenerate_cache_cluster.delay()
        regenerate_cache.delay(changeset.pk, self.pk)
        return True
コード例 #2
0
ファイル: utils.py プロジェクト: guoyu07/healthsites
def locality_create(request):
    if request.method == 'POST':
        if request.user.is_authenticated():
            json_request = get_json_from_request(request)
            # checking mandatory
            if json_request['is_valid'] == True:
                tmp_changeset = Changeset.objects.create(
                    social_user=request.user)
                # generate new uuid
                tmp_uuid = uuid.uuid4().hex

                loc = Locality()
                loc.changeset = tmp_changeset
                loc.domain = Domain.objects.get(name="Health")
                loc.uuid = tmp_uuid

                # generate unique upstream_id
                loc.upstream_id = u'web¶{}'.format(tmp_uuid)

                loc.geom = Point(float(json_request['long']),
                                 float(json_request['lat']))
                loc.name = json_request['name']

                loc.save()
                #  ------------------------------------------------------
                loc.set_values(json_request, request.user, tmp_changeset)

                loc.update_what3words(request.user, tmp_changeset)
                regenerate_cache.delay(tmp_changeset.pk, loc.pk)
                regenerate_cache_cluster.delay()

                return {
                    "success": json_request['is_valid'],
                    "uuid": tmp_uuid,
                    "reason": ""
                }
            else:
                return {
                    "success": json_request['is_valid'],
                    "reason": json_request['invalid_key'] + " can not be empty"
                }
        else:
            return {"success": False, "reason": "Not Login Yet"}

    return {"success": False, "reason": "There is error occured"}
コード例 #3
0
ファイル: utils.py プロジェクト: tyrasd/healthsites
def locality_edit(request):
    if request.method == 'POST':
        if request.user.is_authenticated():
            json_request = get_json_from_request(request)
            # checking mandatory
            if json_request['is_valid'] is True:
                locality = Locality.objects.get(uuid=json_request['uuid'])
                old_geom = [locality.geom.x, locality.geom.y]

                locality.set_geom(
                    float(json_request['long']), float(json_request['lat']))
                locality.name = json_request['name']

                # there are some changes so create a new changeset
                tmp_changeset = Changeset.objects.create(
                    social_user=request.user
                )
                locality.changeset = tmp_changeset

                locality.save()
                locality.set_values(json_request, request.user, tmp_changeset)

                # if location is changed
                new_geom = [locality.geom.x, locality.geom.y]
                if new_geom != old_geom:
                    locality.update_what3words(request.user, tmp_changeset)
                    regenerate_cache_cluster.delay()
                regenerate_cache.delay(tmp_changeset.id, locality.pk)

                return {
                    'success': json_request['is_valid'],
                    'uuid': json_request['uuid'],
                    'reason': ''
                }
            else:
                return {
                    'success': json_request['is_valid'],
                    'reason': json_request['invalid_key'] + ' can not be empty'
                }
        else:
            return {'success': False, 'reason': 'Not Login Yet'}
    return {'success': False, 'reason': 'There is error occured'}
コード例 #4
0
ファイル: utils.py プロジェクト: healthsites/healthsites
def locality_create(request):
    if request.method == 'POST':
        if request.user.is_authenticated():
            json_request = get_json_from_request(request)
            # checking mandatory
            if json_request['is_valid'] is True:
                tmp_changeset = Changeset.objects.create(
                    social_user=request.user
                )
                # generate new uuid
                tmp_uuid = uuid.uuid4().hex

                loc = Locality()
                loc.changeset = tmp_changeset
                loc.domain = Domain.objects.get(name='Health')
                loc.uuid = tmp_uuid

                # generate unique upstream_id
                loc.upstream_id = u'web¶{}'.format(tmp_uuid)

                loc.geom = Point(
                    float(json_request['long']), float(json_request['lat'])
                )
                loc.name = json_request['name']

                loc.save()
                #  ------------------------------------------------------
                loc.set_values(json_request, request.user, tmp_changeset)

                loc.update_what3words(request.user, tmp_changeset)
                regenerate_cache.delay(tmp_changeset.pk, loc.pk)
                regenerate_cache_cluster.delay()

                return {'success': json_request['is_valid'], 'uuid': tmp_uuid, 'reason': ''}
            else:
                return {'success': json_request['is_valid'],
                        'reason': json_request['invalid_key'] + ' can not be empty'}
        else:
            return {'success': False, 'reason': 'Not Login Yet'}

    return {'success': False, 'reason': 'There is error occured'}
コード例 #5
0
ファイル: utils.py プロジェクト: healthsites/healthsites
def locality_edit(request):
    if request.method == 'POST':
        if request.user.is_authenticated():
            json_request = get_json_from_request(request)
            # checking mandatory
            if json_request['is_valid'] is True:
                locality = Locality.objects.get(uuid=json_request['uuid'])
                old_geom = [locality.geom.x, locality.geom.y]

                locality.set_geom(float(json_request['long']), float(json_request['lat']))
                locality.name = json_request['name']

                # there are some changes so create a new changeset
                tmp_changeset = Changeset.objects.create(
                    social_user=request.user
                )
                locality.changeset = tmp_changeset

                locality.save()
                locality.set_values(json_request, request.user, tmp_changeset)

                # if location is changed
                new_geom = [locality.geom.x, locality.geom.y]
                if new_geom != old_geom:
                    locality.update_what3words(request.user, tmp_changeset)
                    regenerate_cache_cluster.delay()
                regenerate_cache.delay(tmp_changeset.id, locality.pk)

                return {
                    'success': json_request['is_valid'], 'uuid': json_request['uuid'],
                    'reason': ''
                }
            else:
                return {'success': json_request['is_valid'],
                        'reason': json_request['invalid_key'] + ' can not be empty'}
        else:
            return {'success': False, 'reason': 'Not Login Yet'}
    return {'success': False, 'reason': 'There is error occured'}