2012-03-29 2 views
4

동적 모듈을로드해야하는 Django 응용 프로그램을 개발 중입니다 (런타임에). 이제 (클라이언트 브라우저에서 서버로) "플러그인"을 업로드하고 데이터베이스에 플러그인 모델을 등록 할 수 있습니다. 그러나 각 플러그인의 url 패턴을 처리 할 방법이 필요합니다. 현재 모델을 등록하고 (이론적으로) 업로드 된 플러그인의 url 패턴을 webapp urls.py에 포함시킴으로써 모델을 등록하는 webapp의 "코어"에 함수를 작성했습니다. 이 함수는 다음Django에서 런타임에 urlpattern을 수정합니다.

def register_plugin_model(model,codename): 
# Standard syncdb expects models to be in reliable locations, 
# so dynamic models need to bypass django.core.management.syncdb. 
# On the plus side, this allows individual models to be installed 
# without installing the entire project structure. 
# On the other hand, this means that things like relationships and 
# indexes will have to be handled manually. 
# This installs only the basic table definition. 

if model is not None: 
    style = color.no_style() 
    cursor = connection.cursor() 
    tables = connection.introspection.table_names() 
    seen_models = connection.introspection.installed_models(tables) 
    statements,trsh = connection.creation.sql_create_model(model, style, seen_models) 
    for sql in statements: 
     cursor.execute(sql) 

# add urlpatterns 
from django.conf.urls.defaults import patterns, url,include 
from project.plugins.urls import urlpatterns 
urlpatterns += patterns(url(r'^' + codename + '/' , include ('media.plugins.' + codename + '.urls'))) 

플러그인 추출한 후 TGZ 형식 "미디어/TMP"에 업로드되고이 "미디어/플러그인 /"어디 플러그인의 코드 명이고, 사용자 업로드 플러그인에 의해 관리되는 " project.plugins "를 참조하십시오.

모든 플러그인 로직이 잘 작동하지만 업로드 된 플러그인 urls.py 파일을 webapp (project.plugins.urls)에 포함 시키려고해도 효과가 없습니다. "project.plugins.urls.urlpatterns"의 값을 인쇄했으며 "urlpatterns + = pat ...."다음에 수정되지 않았습니다.

내가 할 수있는 방법이 있습니까?

안부

답변

3

당신이 직면하고있는 문제는 urlpatterns이 프로젝트에서 url.py 파일을 정의하고 register_plugin 파일에 정의 된 urlpatterns 다른 변수이다. 모듈에 로컬입니다. 다음 시나리오를 상상해보십시오.

#math.py 
pi = 3.14 

#some_nasty_module.py 
from math import pi 
pi = 'for_teh_luls' 

#your_module.py 
from math import pi 
pi * 2 
>>> 'for_teh_lulsfor_teh_luls' 
# wtf? 

분명히 허용되지 않습니다. 아마도 당신이해야 할 일은 원래 urls.py에게 당신의 플러그인 폴더에있는 URL을 발견하도록 시도하는 것입니다.

# urls.py 
urlpatterns += (...) 

def load_plugin_urls(): 
    for module in get_plugin_modules(): 
     try: 
      from module.urls import urlpatterns as u 
      urlpatterns += u 
     except: 
      pass 

불행하게도, 웹 서버는 실행하려면이 코드에 대한 과정을 재활용해야합니다, 그래서 그렇게되면 플러그인 업로드에만 발효됩니다.

1

urls.py 및 urls.py를 수정하는 기능이 동일한 모듈에 속합니다. 내가 "빈 패턴"을 추가하여 해결했습니다은 즉시 적용되지 않습니다,

BEFORE: 
<RegexURLPattern plugins ^$> 
<RegexURLPattern plugin_new ^new/$> 
<RegexURLPattern plugin_profile ^profile/(?P<plugin>\w+)$> 
AFTER 
<RegexURLPattern plugins ^$> 
<RegexURLPattern plugin_new ^new/$> 
<RegexURLPattern plugin_profile ^profile/(?P<plugin>\w+)$> 
<RegexURLResolver media.plugins.sampleplugin.urls (None:None) ^sampleplugin/> 

을하지만 당신은 말했다 : 이제

urlpatterns += patterns('',url(r'^' + codename + '/' , include ('media.plugins.' + codename + '.urls'))) 

를, 그것은 말한다/

* .pyc 파일을 삭제하지 않고 응용 프로그램을 다시 시작했지만 변경 사항이 적용되지 않았습니다. 뭐가 문제 야?

PD는 : 플러그인 urls.py 파일에는 다음이 포함 답장을 보내

from django.conf.urls.defaults import patterns, url 
from .views import index_view 

urlpatterns = patterns('' , url(r'^.*' , index_view)) 

감사를

안부

관련 문제