示例#1
0
文件: tax_lots.py 项目: zolton1/seed
    def to_dict(self, fields=None, include_related_data=True):
        """
        Returns a dict version of the TaxLotState, either with all fields
        or masked to just those requested.
        """

        # TODO: make this a serializer and/or merge with PropertyState.to_dict
        if fields:
            model_fields, ed_fields = split_model_fields(self, fields)
            extra_data = self.extra_data
            ed_fields = list(filter(lambda f: f in extra_data, ed_fields))

            result = {field: getattr(self, field) for field in model_fields}
            result['extra_data'] = {
                field: extra_data[field]
                for field in ed_fields
            }

            # always return id's
            result['id'] = result['pk'] = self.pk

            return result

        d = obj_to_dict(self, include_m2m=include_related_data)

        return d
示例#2
0
    def to_dict(self, fields=None, include_related_data=True):
        """
        Returns a dict version of this building, either with all fields
        or masked to just those requested.
        """
        if fields:
            model_fields, ed_fields = split_model_fields(self, fields)
            extra_data = self.extra_data
            ed_fields = filter(lambda f: f in extra_data, ed_fields)

            result = {
                field: getattr(self, field) for field in model_fields
            }
            result['extra_data'] = {
                field: extra_data[field] for field in ed_fields
            }

            # always return id's and canonical_building id's
            result['id'] = result['pk'] = self.pk
            result['canonical_building'] = (
                self.canonical_building and self.canonical_building.pk
            )

            # should probably also return children, parents, and coparent
            result['children'] = map(lambda c: c.id, self.children.all())
            result['parents'] = map(lambda p: p.id, self.parents.all())
            result['co_parent'] = (self.co_parent and self.co_parent.pk)
            result['coparent'] = (self.co_parent and {
                field: self.co_parent.pk for field in ['pk', 'id']
            })

            return result

        d = obj_to_dict(self, include_m2m=include_related_data)

        if include_related_data:
            d['parents'] = list(self.parents.values_list('id', flat=True))
            d['co_parent'] = self.co_parent.pk if self.co_parent else None

        return d
示例#3
0
文件: deprecate.py 项目: mmclark/seed
    def to_dict(self, fields=None, include_related_data=True):
        """
        Returns a dict version of this building, either with all fields
        or masked to just those requested.
        """
        if fields:
            model_fields, ed_fields = split_model_fields(self, fields)
            extra_data = self.extra_data
            ed_fields = filter(lambda f: f in extra_data, ed_fields)

            result = {
                field: getattr(self, field) for field in model_fields
            }
            result['extra_data'] = {
                field: extra_data[field] for field in ed_fields
            }

            # always return id's and canonical_building id's
            result['id'] = result['pk'] = self.pk
            result['canonical_building'] = (
                self.canonical_building and self.canonical_building.pk
            )

            # should probably also return children, parents, and coparent
            result['children'] = map(lambda c: c.id, self.children.all())
            result['parents'] = map(lambda p: p.id, self.parents.all())
            result['co_parent'] = (self.co_parent and self.co_parent.pk)
            result['coparent'] = (self.co_parent and {
                field: self.co_parent.pk for field in ['pk', 'id']
            })

            return result

        d = obj_to_dict(self, include_m2m=include_related_data)

        if include_related_data:
            d['parents'] = list(self.parents.values_list('id', flat=True))
            d['co_parent'] = self.co_parent.pk if self.co_parent else None

        return d
示例#4
0
 def to_dict(self):
     return obj_to_dict(self)
示例#5
0
文件: projects.py 项目: mmclark/seed
 def to_dict(self):
     return obj_to_dict(self)
示例#6
0
def forwards(apps, schema_editor):
    # Remove duplicate columns
    Organization = apps.get_model("orgs", "Organization")
    Column = apps.get_model("seed", "Column")
    ColumnMapping = apps.get_model("seed", "ColumnMapping")

    column_keys = [
        'organization', 'table_name', 'column_name', 'is_extra_data'
    ]
    for o in Organization.objects.all():
        # for o in Organization.objects.filter(id=267):
        print "Processing organization {}.{}".format(o.id, o.name)

        objs_to_delete = set()

        columns = Column.objects.filter(organization_id=o.pk)
        for c in columns:
            # skip if the column is in the delete list
            if c.pk in [c_obj.pk for c_obj in objs_to_delete]:
                print 'skipping column because it is to be deleted {}'.format(
                    c.pk)
                continue

            check_c = {key: obj_to_dict(c)[key] for key in column_keys}

            multiples = Column.objects.filter(**check_c)
            if multiples.count() > 1:
                print "Found {} duplicate column".format(multiples.count())

                pointer_column = None
                for idx, m in enumerate(multiples):
                    if idx == 0:
                        print "  setting pointer columns to {}".format(m.pk)
                        pointer_column = m
                        continue

                    print "  checking idx {} - pk {}".format(idx, m.pk)
                    # check if there is mappings
                    cms = ColumnMapping.objects.filter(
                        Q(column_raw=m) | Q(column_mapped=m))
                    if cms.count() == 0:
                        print "  no column mappings and idx is > 1, so deleting column {}".format(
                            m.pk)
                        objs_to_delete.add(m)
                        continue

                    cms_raw = ColumnMapping.objects.filter(column_raw=m)
                    if cms_raw.count() == 1:
                        print "    removing old column and adding in new one {} -> {}".format(
                            m.pk, pointer_column.pk)
                        cms_raw.first().column_raw.add(pointer_column)
                        cms_raw.first().column_raw.remove(m)
                        objs_to_delete.add(m)
                        continue

                    if cms_raw.count() > 1:
                        print "  Not sure what to do here but it probably does not matter"

                    print ColumnMapping.objects.filter(column_raw=m).count()

                    for cm in ColumnMapping.objects.filter(column_mapped=m):
                        print "  cleaning up mapping"
                        if pointer_column in cm.column_mapped.all():
                            print "    already in there"
                        else:
                            print "    removing old column and adding in new one {} -> {}".format(
                                m.pk, pointer_column.pk)
                            cm.column_mapped.remove(m)
                            cm.column_mapped.add(pointer_column)
                            print "    staging old column for delete {}".format(
                                m.pk)
                            objs_to_delete.add(m)

        print "objects to delete:"
        for obj in objs_to_delete:
            print "  {}  --  {}".format(obj.id, obj.column_name)
            obj.delete()