[Sync] + new Schedule sync

This commit is contained in:
Roman Timashev committed 2017-09-14 21:33:40 +03:00
1 parent 1a8adaf524
commit e54c448704
6 files changed
+170 -136

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
import logging
import settings
from sync import untouched
from sync.core import Sync
from sync.models.Schedule import Schedule
logger = logging.getLogger(settings.name + '.Schedule')
class SyncSchedule(Sync):
async def run(self):
logger.info('Checking for update')
documents = await Schedule.scan(self.provider)
documents_id = [doc.id for doc in documents]
# self.validate(documents)
logger.info('Found {} Item(s)'.format(len(documents)))
# SKIP UNTOUCHED DOCUMENTS
documents = await untouched(documents)
logger.info('Changed {} Items(s)'.format(len(documents)))
# UPDATING
if settings.update_enabled:
if len(documents) == 0:
logger.info('No Items to update')
for document in documents:
logger.info('Updating Item {}'.format(document.id))
await document.build()
await document.save()
logger.info('Updated Item {}'.format(document.id))
# DELETING
if settings.delete_enabled:
documents_delete = list(self.query({'id': {'$nin': documents_id}}))
if len(documents_delete) == 0:
logger.info('No Items to delete')
else:
for document in documents_delete:
logger.info('Deleting Item {}'.format(document['id']))
q = {'_id': document['_id']}
self.delete(q)
# await self.clean()
-38
View File
@@ -1,38 +0,0 @@
import os
from tempfile import mkstemp
from sync.core import Sync
from utils.fn import last_good, lmap
from utils.hash import hash_str, hash_file
from utils.image import create_image
from utils.image.resize import image_magick_pdf_to_img
from utils.text.transform import url_encode_text, url_encode_file
class SyncSchedule(Sync):
def __init__(self, collection, provider):
super().__init__()
self.collection = collection
self.provider = provider
def create_id(self, document):
period = get_period(document['period'])
doc_id = document['id'] if 'id' in document and document['id'] else None
if not doc_id:
doc_id = 'schedule-{period}-{semester}'.format(period=period, semester=document['semester'])
document['id'] = doc_id
return document
def create_hash(self, document):
return {
**document,
'hash': self.provider.hash(document['file'])
}
def get_period(value):
if isinstance(value, str):
return value
if isinstance(value, list):
return '-'.join(lmap(str, value))