def pull(self, sync_log = None): retorno = {} first = sync_log == None new_sync = datetime.datetime.now() if not first: last_sync = datetime.datetime(*time.strptime(sync_log['fields']['synced_at'], '%Y-%m-%d %H:%M:%S')[:6]) # Veamos los modelos # TODO: Mejorar esto de meterle mano al SyncData models = [] #import ipdb; ipdb.set_trace() for app in self._registry.values(): for model in app.keys(): # Si no es el primero filtro solo los modelos interesantes if not first: model_type = ContentType.objects.get_for_model(model) sd = SyncData.objects.filter(content_type__pk = model_type.id, update_at__gt=last_sync).filter(update_at__lt=new_sync) if bool(sd): models.append(model) else: models.append(model) models = get_model_order(models) retorno['models'] = map(lambda m: str(m._meta), models) retorno['objects'] = [] for model in models: remote = self._registry[model._meta.app_label][model] remote_manager = remote._default_manager # Si no es el primero filtro solo los objetos de modelos interesantes if first: retorno['objects'].extend(remote_manager.all()) else: retorno['objects'].extend(remote_manager.filter(update_at__gt = last_sync, update_at__lt = new_sync)) retorno['sync_log'] = {} retorno['sync_log']['synced_at'] = new_sync.strftime("%Y-%m-%d %H:%M:%S") retorno['sync_log']['sync_id'] = random_string(32) return retorno
#ipdb.set_trace() # Terminando los Creados for app_model, remote in remote_created.iteritems(): remote_manager = remote._default_manager retorno['created']['pks'][app_model] = remote_manager.insert(received['created']['objects'][app_model]) except Exception, e: raise DataException(str(e)) # mas un segundo para no entrar en errores de milis new_sync = datetime.datetime.now() + datetime.timedelta(seconds = 1) retorno['sync_log'] = {} retorno['sync_log']['synced_at'] = new_sync.strftime("%Y-%m-%d %H:%M:%S") retorno['sync_log']['sync_id'] = random_string(32) return retorno #=========================================================================== # Manifests #=========================================================================== @expose('^manifest.json$') def manifest(self, request): ''' For simlicity reasons, we merge both the protopy (aka system manifest) and the project manifest into mainfest.json Using the update_manifest command these manifests can be updated. ''' try: manifest = GearsManifest.objects.get(remotesite_name = self.name)
def handle_remotesite(self, site, **options): output = [] self.site = site try: manifest = GearsManifest.objects.get(remotesite_name = site.name) except models.exceptions.ObjectDoesNotExist: print "New manifest created" manifest = GearsManifest() manifest.remotesite_name = site.name except (models.exceptions.FieldError, models.exceptions.ValidationError): print "Syncdb?" return count = manifest.gearsmanifestentry_set.count() # Switches if options.get('clear'): if manifest.id: manifest.delete() return "Manifest entries for %s deleted (%d)" % (site.name, count) else: return "Nothing to delete" #entries = manifest.gearsmanifestentry_set.count() # Cambiar el numero de version if not manifest.version: if options.get('manifest_ver'): manifest.version = options.get('manifest_ver') else: manifest.version = random_string(32) print "Version:", manifest.version # Application Code file_list = [] # TODO: Uniformar las cosas en site para no tener que estar apendeando "/" print "Adding/updating root..." file_list.append(objdict({'name': '/', 'url': '/'.join([ site.url ]) + '/', }) ) print "Adding/updating templates..." for t in full_template_list(): if self.invalid_file(site, t): continue fname = self.get_template_file(t) mtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(fname))) fsize = os.path.getsize(fname) file_list.append(objdict({ 'name': t, 'url': '/'.join([site.templates_url, t]), 'file_mtime': mtime, 'file_size': fsize }) ) print "Adding/updating js..." for js in abswalk_with_simlinks(site.project_root): if self.invalid_file(site, js): continue relpath = relativepath(js, site.project_root) mtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(js))) fsize = os.path.getsize(js) file_list.append(objdict({'name': relpath, 'url': '/'.join([site.js_url, relpath]), 'file_mtime': mtime, 'file_size': fsize, }) ) print "Adding/updating models..." for app in site.app_names(): name = '/'.join([app, 'models.js']) #TODO: Check if the file exists file_list.append(objdict({'name': name, 'url': '/'.join([ site.js_url, name ]), }) ) print "Adding/updating lib..." for lib in abswalk_with_simlinks(site.protopy_root): if self.invalid_file(site, lib): continue relpath = relativepath(lib, site.protopy_root) mtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(lib))) fsize = os.path.getsize(lib) file_list.append(objdict({'name': relpath, 'url': '/'.join([site.lib_url, relpath]), 'file_mtime': mtime, 'file_size': fsize, }) ) print "Adding/updating media from %s..." % site.media_root for media in abswalk_with_simlinks(site.media_root): if self.invalid_file(site, js): continue relpath = relativepath(media, site.media_root) mtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(media))) fsize = os.path.getsize(media) #from ipdb import set_trace; set_trace() file_list.append(objdict({'name': relpath, 'url': '/'.join([site.media_url, relpath]), 'file_mtime': mtime, 'file_size': fsize, }) ) if not count: # New instance or empty, just create entries and add them print "Creating new manifest...", manifest.save() for f in file_list: entry = GearsManifestEntry(manifest = manifest, **f) entry.save() #manifest.save() print "OK" else: # Compraracion por modificaciones print "Checking for updates..." m_templates_qs = manifest.gearsmanifestentry_set.filter(url__startswith = site.templates_url) updated_templates, \ new_templates, \ deleted_templates = self.get_updates_for_entry_qs(m_templates_qs, file_list, site.templates_url, self.get_template_file, manifest = manifest) m_js_qs = manifest.gearsmanifestentry_set.filter(url__startswith = site.js_url) updated_js, \ new_js, \ deleted_js = self.get_updates_for_entry_qs(m_js_qs, file_list, site.js_url, self.get_js_file, manifest = manifest) m_lib_qs = manifest.gearsmanifestentry_set.filter(url__startswith = site.lib_url) updated_lib, \ new_lib, \ deleted_lib = self.get_updates_for_entry_qs(m_lib_qs, file_list, self.site.lib_url, self.get_lib_file, manifest = manifest) m_media_qs = manifest.gearsmanifestentry_set.filter(url__startswith = site.media_url) updated_media, \ new_media, \ deleted_media = self.get_updates_for_entry_qs(m_media_qs, file_list, self.site.media_url, self.get_media_file, manifest = manifest) templates_modified = updated_templates or new_templates or deleted_templates lib_modified = updated_lib or new_lib or deleted_lib js_modifed = updated_js or new_js or deleted_js media_modified = updated_media or new_media or deleted_media if templates_modified or lib_modified or js_modifed or media_modified: manifest.version = options.get('manifest_ver') or random_string(32) manifest.save() print "Manifest version updated to %s" % manifest.version return output