rapid_crud.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # coding=utf-8
  2. __author__ = 'marcos.medeiros'
  3. from django import template
  4. from django.template import loader, Context
  5. import random
  6. from django.utils.safestring import mark_safe
  7. register = template.Library()
  8. @register.inclusion_tag('rapid/bare/list.html', takes_context=True)
  9. def crud_list(context):
  10. return context
  11. @register.inclusion_tag('rapid/bare/select.html', takes_context=True)
  12. def crud_select(context):
  13. return context
  14. @register.inclusion_tag('rapid/bare/detail.html', takes_context=True)
  15. def crud_view(context):
  16. return context
  17. @register.inclusion_tag('rapid/bare/update.html', takes_context=True)
  18. def crud_update(context):
  19. return context
  20. @register.inclusion_tag('rapid/bare/create.html', takes_context=True)
  21. def crud_create(context):
  22. return context
  23. @register.inclusion_tag('rapid/bare/delete.html', takes_context=True)
  24. def crud_delete(context):
  25. return context
  26. def random_name():
  27. s = "abcdefghijklmnopqrustuvwxyz"
  28. # noinspection PyUnusedLocal
  29. return "".join([random.choice(s) for x in range(30)])
  30. def render_to_javascript_string(template_name, context=None):
  31. context = context if context else {}
  32. t = loader.get_template(template_name)
  33. c = Context(context)
  34. txt = t.render(c)
  35. txt = txt.replace("\"", "\\\"")
  36. txt = txt.replace("\n", "\\n")
  37. return mark_safe(txt)
  38. @register.filter(name="jsid")
  39. def jsid(value):
  40. v = value.replace('"', "").replace("-", "_").replace("'", "")
  41. return v
  42. @register.filter(name="jsstr")
  43. def jsstr(txt):
  44. txt = txt.replace("\"", "\\\"")
  45. txt = txt.replace("\n", "\\n")
  46. return mark_safe(txt)
  47. @register.inclusion_tag('rapid/overlay/register.html')
  48. def register_overlay():
  49. overlay_text = render_to_javascript_string('rapid/overlay/text.html')
  50. return {'overlay_text': overlay_text}
  51. @register.inclusion_tag('rapid/overlay/call.html')
  52. def overlay(target_url, on_commit=None, on_close=None):
  53. if not on_commit:
  54. on_commit = 'function(){}'
  55. if not on_close:
  56. on_close = 'function(){}'
  57. return {
  58. 'target_url': target_url,
  59. 'on_commit': on_commit,
  60. 'on_close': on_close,
  61. }