Skip to content

Commit

Permalink
Fixes an issue where the new id would not serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelduchesne committed Sep 9, 2020
1 parent 1a15294 commit 140d8cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
13 changes: 11 additions & 2 deletions tests/test_mongodb_schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json

import geojson
import pytest
import shapely.geometry
from archetypal import UmiTemplateLibrary
Expand Down Expand Up @@ -72,6 +71,14 @@ def test_import_library(db, imported):
assert bldg


def test_serialize_db(imported):
bldgs = BuildingTemplate.objects().all()
templates = []
for bldg in bldgs:
templates.append(bldg.to_template())
lib = UmiTemplateLibrary(BuildingTemplates=templates).to_json()


def test_serialize_templatelist(bldg, window, struct, core):
"""From a list of :class:~`umitemplatedb.mongodb_schema.BuildingTemplate`
create an :class:~`archetypal.umi_template.UmiTemplateLibrary`"""
Expand All @@ -82,7 +89,7 @@ def test_serialize_templatelist(bldg, window, struct, core):
templates.append(bldg.to_template())

lib = UmiTemplateLibrary(BuildingTemplates=templates)
lib.to_json()
print(lib.to_json())


@pytest.fixture(scope="session")
Expand All @@ -95,6 +102,8 @@ def db():

@pytest.fixture(scope="session")
def imported(db):
UmiBase.drop_collection()

path = "tests/test_templates/BostonTemplateLibrary.json"
import_umitemplate(path)

Expand Down
18 changes: 4 additions & 14 deletions umitemplatedb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from mongoengine import *

from umitemplatedb import mongodb_schema
from umitemplatedb.mongodb_schema import (
BuildingTemplate,
)
from umitemplatedb.mongodb_schema import BuildingTemplate


def import_umitemplate(filename, **kwargs):
Expand All @@ -24,7 +22,7 @@ def import_umitemplate(filename, **kwargs):
lib = UmiTemplateLibrary.read_file(filename)

# Loop over building templates
for bldgtemplate in lib.__dict__["BuildingTemplates"]:
for bldgtemplate in lib.BuildingTemplates:

def recursive(umibase, **metaattributes):
"""recursively create db objects from UmiBase objects. Start with
Expand Down Expand Up @@ -63,20 +61,12 @@ def recursive(umibase, **metaattributes):
if isinstance(class_instance, EmbeddedDocument):
return class_instance
else:
class_instance = class_(
**instance_attr,
key=hash(
(instance_attr.get("Name", id(umibase).__str__()),
type(umibase).__name__)
)
)
class_instance = class_(**instance_attr,)
if isinstance(class_instance, BuildingTemplate):
for key, value in metaattributes.items():
class_instance[key] = value
class_instance.save()
return class_instance

# loop starts here
bldg = recursive(
bldgtemplate, **kwargs
)
recursive(bldgtemplate, **kwargs)
31 changes: 14 additions & 17 deletions umitemplatedb/mongodb_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from mongoengine import *


class PrimaryKey(IntField):
def __init__(self, field=None, *args, **kwargs):
super().__init__(field=field, *args, **kwargs)
class CompoundKey(EmbeddedDocument):
name = StringField(required=True)
clsname = StringField(required=True)

meta = {"allow_inheritance": True, "strict": False}


class UmiBase(Document):
Expand All @@ -22,16 +24,19 @@ class UmiBase(Document):
"""

key = StringField(primary_key=True)

Name = StringField(required=True)
key = PrimaryKey(
primary_key=True, default=hash((str(Name), Document.__name__))
)
Comments = StringField(null=True)
DataSource = StringField(null=True)
Category = StringField(default="Uncategorized")

meta = {"allow_inheritance": True}

def save(self, *args, **kwargs):
self.key = ", ".join([type(self).__name__, self.Name])
return super(UmiBase, self).save(*args, **kwargs)


class Material(UmiBase):
# MaterialBase
Expand Down Expand Up @@ -277,15 +282,6 @@ class WindowSetting(UmiBase):
}


def validate_poly(val):
if not val:
raise ValidationError("value can not be empty")


def update_modified(sender, document):
document.DateModified = datetime.utcnow()


class BuildingTemplate(UmiBase):
"""Top most object in Umi Template Structure"""

Expand Down Expand Up @@ -375,8 +371,9 @@ def geo_countries(self):
from datapackage import Package

try:
package = Package("https://datahub.io/core/geo-countries/datapackage"
".json")
package = Package(
"https://datahub.io/core/geo-countries/datapackage" ".json"
)
except:
self._geo_countries = []
else:
Expand Down

0 comments on commit 140d8cc

Please sign in to comment.