Item
The Item class is where you define CRUD methods for interfacing with a service. Since these are abstract methods they must be defined for concrete subclasses of Item. When you provide it with a normalizer attribute these methods will take on the extra functionality of normalizing the data automatically.
In order to keep track of all the items a model needs to sync with, all subclasses of Item must be declared in modules named items.py and their packages must be provided in the list of strings passed to the Config.
Usage
The required methods for the concrete subclasses of Item are: * get_recently_modified * read * read_many * create * update * delete
import json
from zync import Item
from normalizers import CalendarNormalizer
class CalendarItem(Item):
# Declare the normalizer which contains logic for creating NormalModels
# from the data returned by these methods.
normalizer = CalendarNormalizer()
def get_recently_modified(self) -> list:
with open('calendars.json') as jsonfile:
gcaldata = json.load(jsonfile)
results = [record for record in gcaldata if record["lastmodified"] > self.last_synced_timestamp]
return results
# Fetch a single item.
def read(self, *args, **kwargs):
with open('calendars.json') as jsonfile:
gcaldata = json.load(jsonfile)
return gcaldata[0]
# Fetch many items.
def read_many(self, *args, **kwargs) -> list:
with open('calendars.json') as jsonfile:
gcaldata = json.load(jsonfile)
return gcaldata
# Update an item.
def update(self, *args, **kwargs):
return super().update()
# Create a new item, or overwrite an item.
def create(self, *args, **kwargs):
return super().create()
# Delete an item.
def delete(self, *args, **kwargs):
return super().delete()
# Flag an item as synced, so that it doesn't get picked up by get_recently_modified
def mark_as_synced(self, data):
pass