Beispiel #1
0
    def contribute_to_class(self, remote, name = 'remotes'):
        self.remote = remote
        try:
            self.model_manager = getattr(remote._meta, 'manager')
        except AttributeError:
            self.model_manager = getattr(remote._meta.model, '_default_manager')

        rpc = SimpleJSONRPCDispatcher(allow_none=False, encoding=None)
        rpc.register_introspection_functions()
        rpc.register_instance(self)
        setattr(remote, '_default_manager', self)
        setattr(remote, name, rpc)
Beispiel #2
0
    def __init__(self, name, protopy_root = None):

        global REMOTE_SITES
        if name in REMOTE_SITES:
            raise Exception("You can't define two RemoteSites with the same name")
        else:
            self.name = name
            REMOTE_SITES[ self.name ] = self

        if not protopy_root:
            from os.path import abspath, dirname, join
            protopy_root = getattr(get_app('offline'), '__file__')
            protopy_root = join(abspath(dirname(protopy_root)), 'protopy')
        self.protopy_root = protopy_root

        # Create a Dispatcher; this handles the calls and translates info to function maps
        #self.rpc_dispatcher = SimpleJSONRPCDispatcher() # Python 2.4
        self.rpc_dispatcher = SimpleJSONRPCDispatcher(allow_none=False, encoding=None) # Python 2.5
        self.rpc_dispatcher.register_introspection_functions() #Un poco de azucar
        self.rpc_dispatcher.register_instance(self)
        self._registry = {}
Beispiel #3
0
class RemoteSite(RemoteBaseSite):
    '''
    Manages offline project support.
    @expose decorator indicates how URLs are mapped
    '''
    TEMPLATES_PREFIX = 'templates'
    JS_PREFIX = 'js'
    LIB_PREFIX = 'lib'
    #OFFLINE_ROOT = 'offline'
    OFFLINE_ROOT = settings.OFFLINE_BASE
    
    TEMPLATE_RE_EXCLUDE = map(re.compile, (r'\.svn', r'\.hg', r'\.*~$'))
    #TODO: (nahuel) es necesario?
    TEMPLATE_CALLBACK_EXCLUDE = None
    
    def __init__(self, name, protopy_root = None):

        global REMOTE_SITES
        if name in REMOTE_SITES:
            raise Exception("You can't define two RemoteSites with the same name")
        else:
            self.name = name
            REMOTE_SITES[ self.name ] = self

        if not protopy_root:
            from os.path import abspath, dirname, join
            protopy_root = getattr(get_app('offline'), '__file__')
            protopy_root = join(abspath(dirname(protopy_root)), 'protopy')
        self.protopy_root = protopy_root

        # Create a Dispatcher; this handles the calls and translates info to function maps
        #self.rpc_dispatcher = SimpleJSONRPCDispatcher() # Python 2.4
        self.rpc_dispatcher = SimpleJSONRPCDispatcher(allow_none=False, encoding=None) # Python 2.5
        self.rpc_dispatcher.register_introspection_functions() #Un poco de azucar
        self.rpc_dispatcher.register_instance(self)
        self._registry = {}

    def _get_project_root(self):
        return os.sep.join([get_project_root(), self.OFFLINE_ROOT, self.name])
    project_root = property(_get_project_root, doc = "File system offline location")

    def _get_url(self):
        names = settings.OFFLINE_BASE.split("/")
        names.append(self.name)
        return "/" + "/".join(names)
    url = property(_get_url, doc = "Absolute URL to the remote site")

    def _get_urlregex(self):
        if not self.url.startswith('/'):
            return self.url
        return self.url[1:]
    urlregex = property(_get_urlregex, doc = "For regex in url.py")

    def _get_js_url(self):
        return '/'.join([self.url, self.JS_PREFIX])
    js_url = property(_get_js_url, doc = "For something")

    def _get_lib_url(self):
        return '/'.join([self.url, self.LIB_PREFIX])
    lib_url = property(_get_lib_url, doc = "For lib")

    def _get_templates_url(self):
        return '/'.join([self.url, self.TEMPLATES_PREFIX])
    templates_url = property(_get_templates_url, doc = "Base url for templates")

    def _get_media_url(self):
        if settings.MEDIA_URL and settings.MEDIA_URL[-1] == '/':
            return settings.MEDIA_URL[:-1]
        return settings.MEDIA_URL
    media_url = property(_get_media_url, doc = "Media url")
    
    def _get_media_root(self):
        if not settings.MEDIA_ROOT:
            raise Exception("MEDIA_ROOT not definded!")
        return os.path.abspath(settings.MEDIA_ROOT)
    media_root = property(_get_media_root, doc = "Media root")
    
    
    #===========================================================================
    # View methods
    #===========================================================================
    @expose(r'^$')
    def index(self, request):

        content = '''
            <html>
            <head>
                <script type="text/javascript;version=1.7" src="{{ site.lib_url }}/protopy.js"></script>
                <script type="text/javascript;version=1.7">
                    require('doff.core.project', 'new_project');
                    var {{ site.name }} = new_project('{{ site.name }}', '{{ site.js_url }}');
                    {{ site.name }}.bootstrap();
                </script>
            </head>
            <body>
            </body>
            </html>
        '''

        template = Template(content);
        print self.name, self.url 
        return HttpResponse(template.render(Context({'site': self})));

    @expose(r'^%s/(.*)$' % TEMPLATES_PREFIX)
    def templates_static_serve(self, request, path):
        from django.template.loader import find_template_source
        try:
            template_source, _origin = find_template_source(path)
        except TemplateDoesNotExist:
            return HttpResponseNotFound(u'404: template not found: \"%s\"' % path)
        return HttpResponse(template_source)

    @expose(r'^template_list/?$')
    def template_list(self, request):
        return HttpResponse( html_output(full_template_list(), indent = 2))

    @expose('^%s/(.*)$' % LIB_PREFIX)
    def system_static_serve(self, request, path):
        from django.views.static import serve
        return serve(request, path, self.protopy_root, show_indexes = True)

    @expose('^%s/(.*)$' % JS_PREFIX)
    def project_static_serve(self, request, path):
        from django.views.static import serve
        try:
            return serve(request, path, self.project_root, show_indexes = False)
        except Http404, e:
            match = re.compile(r'^(?P<app_name>.*)/models.js$').match(path)
            if match:
                app = match.groupdict()['app_name']
                return self.export_models(app)
            raise e