Skip to content

Commit

Permalink
initial support for snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Apr 1, 2016
1 parent e084bc6 commit acde557
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
33 changes: 33 additions & 0 deletions src/appier_extras/parts/admin/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ def description_name(cls):
def is_indexed(cls):
return True

@classmethod
def is_snapshot(cls):
return False

@classmethod
def build_index_g(cls, *args, **kwargs):
models = cls.find(*args, **kwargs)
Expand Down Expand Up @@ -208,6 +212,7 @@ def pre_update(self):
appier.Model.pre_update(self)

self.modified = time.time()
self.build_snapshot()

def post_save(self):
appier.Model.post_save(self)
Expand Down Expand Up @@ -273,6 +278,34 @@ def destroy_index(self):
cls = self.__class__
search.Search.delete_indexes(self._id, cls)

def build_snapshot(self):
from appier_extras.parts.admin.models import snapshot

# retrieves the reference to the class of the entity to have a snapshot
# and verifies that the (model) class is enabled for snapshotting, in
# case it's not returns the control flow immediately no snapshot required
cls = self.__class__
if not cls.is_snapshot(): return

self = self.reload(
eager_l = False,
rules = False,
build = False,
meta = False
)

model = self._filter(
increment_a = False,
immutables_a = False,
normalize = True
)

snapshot.Snapshot.create_snapshot(
self._id,
cls,
model_data = model
)

def enable_s(self):
self.enabled = True
self.save()
Expand Down
11 changes: 6 additions & 5 deletions src/appier_extras/parts/admin/models/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,18 @@ def create_index(
index.save()

@classmethod
def find_indexes(cls, target_id, target_cls):
def find_indexes(cls, target_id, target_cls, *args, **kwargs):
target_cls = target_cls.__name__
return cls.find(
target_id = target_id,
target_cls = target_cls
target_cls = target_cls,
*args, **kwargs
)

@classmethod
def delete_indexes(cls, target_id, target_cls):
indexes = cls.find_indexes(target_id, target_cls)
for index in indexes: index.delete()
def delete_indexes(cls, target_id, target_cls, *args, **kwargs):
indexes = cls.find_indexes(target_id, target_cls, build = False)
for index in indexes: index.delete(*args, **kwargs)

@classmethod
def _build(cls, model, map):
Expand Down
25 changes: 17 additions & 8 deletions src/appier_extras/parts/admin/models/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@

class Snapshot(base.Base):

token = appier.field(
index = True,
default = True
)

target_id = appier.field(
index = True
)
Expand All @@ -63,9 +58,6 @@ class Snapshot(base.Base):
@classmethod
def validate(cls):
return super(Snapshot, cls).validate() + [
appier.not_null("token"),
appier.not_empty("token"),

appier.not_null("target_id"),

appier.not_null("target_cls"),
Expand All @@ -81,3 +73,20 @@ def list_names(cls):
@classmethod
def is_indexed(cls):
return False

@classmethod
def create_snapshot(
cls,
target_id,
target_cls,
model_data
):
snapshot = cls(
target_id = target_id,
target_cls = target_cls.__name__,
model_data = model_data
)
snapshot.save()

def restore(self):
pass
4 changes: 4 additions & 0 deletions src/appier_extras/test/admin/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ def validate(cls):
appier.not_empty("name"),
appier.not_duplicate("name", cls._name())
]

@classmethod
def is_snapshot(cls):
return True
14 changes: 14 additions & 0 deletions src/appier_extras/test/admin/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ def test_basic(self):
person.name = "Name"

person.save()

self.assertEqual(person.id, 1)
self.assertEqual(person.name, "Name")

person = mock.AdminPerson.get(1)

self.assertEqual(person.id, 1)
self.assertEqual(person.name, "Name")

person.name = "NameChanged"
person.save()

self.assertEqual(person.id, 1)
self.assertEqual(person.name, "NameChanged")

0 comments on commit acde557

Please sign in to comment.