def update(self, request, *args, **kwargs):
        instance = self.get_object()
        previous_identifier = instance.identifier

        existing_identifier = Site.objects.filter(identifier=request.data.get('identifier'),
                                                  project_id=instance.project_id)
        check_identifier = previous_identifier == request.data.get('identifier')

        if not check_identifier and existing_identifier:
            return Response(status=status.HTTP_400_BAD_REQUEST, data={'detail': 'Your identifier "' + request.data.get(
                'identifier') + '" conflict with existing site please use different identifier to update site'})

        old_meta = instance.site_meta_attributes_ans

        serializer = self.get_serializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)

        self.perform_update(serializer)
        longitude = request.data.get('longitude', None)
        latitude = request.data.get('latitude', None)

        if latitude and longitude is not None:
            p = Point(round(float(longitude), 6), round(float(latitude), 6), srid=4326)
            instance.location = p
            instance.save()
        instance.save()

        new_meta = json.loads(instance.site_meta_attributes_ans)

        if instance.site_meta_attributes_ans:
            metas = get_site_meta_ans(instance.id)
            instance.all_ma_ans = metas
            instance.save()

        extra_json = None

        if old_meta != new_meta:
            updated = {}
            meta_questions = instance.project.site_meta_attributes
            for question in meta_questions:
                key = question['question_name']
                label = question['question_text']
                if old_meta.get(key) != new_meta.get(key):
                    updated[key] = {'label': label, 'data': [old_meta.get(key, 'null'), new_meta.get(key, 'null')]}
            extra_json = updated

        description = u'{0} changed the details of site named {1}'.format(
            self.request.user.get_full_name(), instance.name
        )
        instance.logs.create(
            source=self.request.user, type=15, title="edit Site",
            organization=instance.project.organization,
            project=instance.project, content_object=instance,
            description=description,
            extra_json=extra_json,
        )
        return Response(serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 2
0
 def get(self, request, pk):
     # metas = generateSiteMetaAttribs(int(pk))
     metas2 = get_site_meta_ans(int(pk))
     saved = Site.objects.get(pk=pk).all_ma_ans
     return Response({
         'saved': saved,
         'current': metas2
     },
                     status=status.HTTP_200_OK)
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        existing_identifier = Site.objects.filter(
            identifier=request.data.get('identifier'),
            project_id=request.query_params.get('project'))
        if existing_identifier:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    'detail':
                    'Your identifier "' + request.data.get('identifier') +
                    '" conflict with existing site '
                    'please use different identifier '
                    'to create site.'
                })

        serializer.is_valid(raise_exception=True)
        instance = self.perform_create(serializer)
        longitude = request.data.get('longitude', None)
        latitude = request.data.get('latitude', None)
        site = request.data.get('site', None)
        instance.save()

        if latitude and longitude is not None:
            p = Point(round(float(longitude), 6),
                      round(float(latitude), 6),
                      srid=4326)
            instance.location = p
            instance.save()

        if instance.site_meta_attributes_ans:
            metas = get_site_meta_ans(instance.id)
            instance.all_ma_ans = metas
            instance.save()
        if site is not None:
            instance.logs.create(source=self.request.user, type=110, title="new sub Site", site=instance.site,
                                 organization=instance.project.organization, project=instance.project,
                                 content_object=instance, extra_object=instance.site,
                                 description=u'{0} created a new Sub ' u'site named {1} in {2}'.\
                                 format(self.request.user.get_full_name(), instance.name, instance.project.name))

        else:
            instance.logs.create(source=self.request.user, type=11, title="new Site",
                                 organization=instance.project.organization, project=instance.project,
                                 content_object=instance, extra_object=instance.project,
                                 description=u'{0} created a new site ' u'named {1} in {2}'.\
                                 format(self.request.user.get_full_name(), instance.name, instance.project.name))

        return Response(serializer.data, status=status.HTTP_201_CREATED)