def get_dict(self, url=False, ref_urls=False, history=False):
        """
        Return the fields and their contents as a dict.

        If url=True, add own absolute url, if ref_urls=True, add urls for any
        referencefields present.
        """
        fieldnames = (field.name for field in self._meta.fields)
        items = ((field,getattr(self, field)) for field in fieldnames)
        result = {}
        for k, v in items:
            if isinstance(v, models.Model):
                # Replace object by representation
                result.update({k: str(v)})
                if ref_urls:
                    result.update({k + '_url': v.get_absolute_url()})
            else:
                result.update({k: v})

        if history:
            result.update(get_simple_history(self))
            # Since only modified is displayed, so do
            if result['datetime_modified'] is None:
                result['datetime_modified'] = result['datetime_created']
            if result['modified_by'] is None:
                result['modified_by'] = result['created_by']


            
        return result
Beispiel #2
0
    def get_object_for_api(self,
                           worksp,
                           flat=True,
                           size=BaseApiView.COMPLETE,
                           include_geom=False):
        """
        create object of workspace
        """

        # You get a dict with keys datetime_modified, modified_by,
        # created_by, datetime_created
        history = get_simple_history(worksp)

        # Fields are read-only if the object is not yours
        read_only = ((self.request.user != worksp.owner) and
                     not self.request.user.is_superuser)
        #read_only = not(worksp.owner_type == self.model_class.OWNER_TYPE_USER)

        if size == self.ID_NAME:
            output = {
                'id': worksp.id,
                'name': worksp.name,
            }
        elif size == self.MEDIUM or size == self.SMALL:
            output = {
                'id': worksp.id,
                'name': worksp.name,
                'personal_category': worksp.personal_category,
                'category': self._get_related_object(
                    worksp.category,
                    flat
                ),
                'data_set': self._get_related_object(
                    worksp.data_set,
                    flat
                ),
                'owner': self._get_related_object(
                    worksp.owner,
                    flat
                ),
                'owner_type': self._get_choice(
                    worksp.owner_type,
                    flat
                ),
                'read_only': read_only,
                'datetime_modified': history['datetime_modified'],
                'datetime_created': history['datetime_created'],
            }

        elif size == self.COMPLETE:
            output = {
                'id': worksp.id,
                'name': worksp.name,
                'personal_category': worksp.personal_category,
                'category': self._get_related_object(
                    worksp.category,
                    flat
                ),
                'data_set': self._get_related_object(
                    worksp.data_set,
                    flat
                ),
                'owner': self._get_related_object(
                    worksp.owner,
                    flat
                ),
                'owner_type': self._get_choice(
                    self.model_class._meta.get_field('owner_type'),
                    worksp.owner_type,
                    flat
                ),
                'read_only': read_only,
                'layers': worksp.get_workspace_layers(),
                'datetime_modified': history['datetime_modified'],
                'datetime_created': history['datetime_created'],
            }
        # Only for collages, the collage view uses the same class.
        if 'secret_slug' in worksp.__dict__:
            output['secret_slug'] = worksp.secret_slug
        if 'is_temp' in worksp.__dict__:
            output['is_temp'] = worksp.is_temp
        return output