Esempio n. 1
0
 def template_list(self, request):
     return HttpResponse( html_output(full_template_list(), indent = 2))
Esempio n. 2
0
    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