| 398 | DJANGO_VIEW_TEMPLATE = """\ |
| 399 | # from {{ model_qname }} import {{ model_name }} |
| 400 | from django.utils.httpwrappers import HttpResponse |
| 401 | |
| 402 | # Place here your methods |
| 403 | """ |
| 404 | |
| 405 | DJANGO_VIEW_METHOD_TEMPLATE = """ |
| 406 | def {{ method_name }}(request): |
| 407 | return HttpResponse("{{ method_name }} method") |
| 408 | """ |
| 409 | |
| 410 | def create_view(mod, filename, *methods): |
| 411 | "Generates a skeleton view module" |
| 412 | model_qname = mod.__name__ |
| 413 | model_name = model_qname.split('.')[-1] |
| 414 | |
| 415 | app_dir = os.path.normpath(os.path.join(mod.__file__, '../../')) |
| 416 | app_name = os.path.basename(app_dir) |
| 417 | url_file = os.path.join(app_dir, 'urls', app_name + '.py') |
| 418 | view_file = os.path.join(app_dir, 'views', filename + '.py') |
| 419 | |
| 420 | if os.path.exists(view_file): |
| 421 | sys.stderr.write("Error: %s exists\n" % view_file) |
| 422 | |
| 423 | try: |
| 424 | fp = open(view_file, 'w') |
| 425 | vt = DJANGO_VIEW_TEMPLATE |
| 426 | vt = vt.replace('{{ model_qname }}', model_qname) |
| 427 | vt = vt.replace('{{ model_name }}', model_name) |
| 428 | fp.write(vt) |
| 429 | |
| 430 | for method_name in methods: |
| 431 | fp.write(DJANGO_VIEW_METHOD_TEMPLATE.replace('{{ method_name }}', |
| 432 | method_name)) |
| 433 | except IOError: |
| 434 | sys.stderr.write("Error: I/O error while trying to create '%s' view\n" % filename) |
| 435 | |
| 436 | create_view.args = '[app viewFileName optionalMethodName ...]' |
| 437 | |