Django Library PyGal
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

__init__.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import binascii
  2. from django.conf import settings
  3. from django.urls.base import reverse
  4. import os
  5. SHUFFLE_DISABLE = 0
  6. SHUFFLE_KEEP = 1
  7. SHUFFLE_ENABLE = 2
  8. REPEAT_DISABLE = 0
  9. REPEAT_ENABLE = 1
  10. SEARCH_DISABLE = 0
  11. SEARCH_KEEP = 1
  12. RESP_TYPE_RAWITEM = 'raw'
  13. RESP_TYPE_WEBNAIL = 'webnail'
  14. RESP_TYPE_THUMBNAIL = 'thumbnail'
  15. RESP_TYPE_USERVIEW = 'userview'
  16. RESP_TYPE_INFOVIEW = 'infoview'
  17. RESP_TYPE_DOWNLOAD = 'download'
  18. RESP_TYPE_SEARCH = 'search'
  19. DATA_TYPE_ITEM = 'item'
  20. DATA_TYPE_SEARCH = 'search'
  21. def suspend_public():
  22. return settings.SUSPEND_PUBLIC
  23. def show_image():
  24. return settings.SHOW_IMAGE
  25. def show_video():
  26. return settings.SHOW_VIDEO
  27. def show_audio():
  28. return settings.SHOW_AUDIO
  29. def show_other():
  30. return settings.SHOW_OTHER
  31. def sort_by_date():
  32. return settings.SORT_BY_DATE
  33. def dict2args(d):
  34. if len(d) == 0:
  35. return ''
  36. al = []
  37. for key in d:
  38. if d[key] is None:
  39. al.append('%s' % key)
  40. else:
  41. al.append('%s=%s' % (key, d[key]))
  42. return '?' + '&'.join(al)
  43. def pygal_responses_kwargs(responsetype, datatype, rel_path):
  44. kwargs = {'responsetype': responsetype, 'datatype': datatype}
  45. if rel_path:
  46. kwargs['rel_path'] = rel_path
  47. return kwargs
  48. def url_args(request, repeat=REPEAT_DISABLE, shuffle=SHUFFLE_KEEP, search=SEARCH_DISABLE, flat=False):
  49. args_d = {}
  50. if repeat == REPEAT_ENABLE:
  51. args_d['repeat'] = None
  52. if shuffle == SHUFFLE_ENABLE:
  53. args_d['shuffle'] = binascii.hexlify(os.urandom(24)).decode('utf-8')
  54. elif shuffle == SHUFFLE_KEEP:
  55. shuffle_id = get_shuffle_id(request)
  56. if shuffle_id is not None:
  57. args_d['shuffle'] = shuffle_id
  58. if search == SEARCH_KEEP:
  59. search_query = get_search_query(request)
  60. if search_query is not None:
  61. args_d['q'] = search_query
  62. elif type(search) == str:
  63. args_d['q'] = search
  64. if flat:
  65. args_d['flat'] = None
  66. return args_d
  67. def get_full_path(rel_path):
  68. return os.path.join(settings.ITEM_ROOT, rel_path.replace('/', os.path.sep))
  69. def get_rel_path(full_path):
  70. if full_path.startswith(settings.ITEM_ROOT):
  71. return full_path[len(settings.ITEM_ROOT) + 1:].replace(os.path.sep, '/')
  72. def set_thumbnail_size(request, value):
  73. request.session[settings.SESSION_KEY_THUMBNAIL_SIZE] = value
  74. def get_thumbnail_size(request):
  75. return request.session.get(settings.SESSION_KEY_THUMBNAIL_SIZE, max(settings.THUMBNAIL_SIZES))
  76. def get_thumbnail_max_size(request):
  77. return max(settings.THUMBNAIL_SIZES)
  78. def set_webnail_size(request, value):
  79. request.session[settings.SESSION_KEY_WEBNAIL_SIZE] = value
  80. def get_webnail_size(request):
  81. return request.session.get(settings.SESSION_KEY_WEBNAIL_SIZE, max(settings.WEBNAIL_SIZES))
  82. def get_datatype(request):
  83. try:
  84. return request.META['PATH_INFO'].split('/')[3]
  85. except IndexError:
  86. return None
  87. def get_responsetype(request):
  88. try:
  89. return request.META['PATH_INFO'].split('/')[2]
  90. except IndexError:
  91. return None
  92. def url_download(request, rel_path, flat=False):
  93. return reverse('pygal-responses', kwargs=pygal_responses_kwargs(RESP_TYPE_DOWNLOAD, get_datatype(request), rel_path)) + dict2args(url_args(request, flat=flat, search=SEARCH_KEEP))
  94. def url_helpview(request, page):
  95. return reverse('pygal-helpview', kwargs={'page': page})
  96. def url_infoview(request, rel_path, repeat=REPEAT_DISABLE, shuffle=SHUFFLE_KEEP, search=SEARCH_DISABLE):
  97. return reverse('pygal-responses', kwargs=pygal_responses_kwargs(RESP_TYPE_INFOVIEW, get_datatype(request), rel_path)) + dict2args(url_args(request, repeat=repeat, shuffle=shuffle, search=search))
  98. def url_item(request, rel_path):
  99. return reverse('pygal-responses', kwargs=pygal_responses_kwargs(RESP_TYPE_RAWITEM, DATA_TYPE_ITEM, rel_path))
  100. def url_profile(request):
  101. nxt = request.GET.get('next', request.get_full_path())
  102. return reverse('pygal-profile') + '?next=%s' % nxt
  103. def url_repeatview(request, rel_path):
  104. return url_userview(request, rel_path, repeat=REPEAT_ENABLE, search=SEARCH_KEEP) + '#openModal'
  105. def url_search(request):
  106. return reverse('pygal-responses', kwargs={'responsetype': RESP_TYPE_USERVIEW, 'datatype': DATA_TYPE_SEARCH})
  107. def url_thumbnail(request, rel_path):
  108. return reverse('pygal-responses', kwargs=pygal_responses_kwargs(RESP_TYPE_THUMBNAIL, DATA_TYPE_ITEM, rel_path))
  109. def url_userview(request, rel_path, repeat=REPEAT_DISABLE, shuffle=SHUFFLE_KEEP, search=SEARCH_DISABLE):
  110. if shuffle in [SHUFFLE_DISABLE, SHUFFLE_ENABLE]:
  111. return request.META.get('PATH_INFO') + dict2args(url_args(request, repeat=repeat, shuffle=shuffle, search=search))
  112. return reverse('pygal-responses', kwargs=pygal_responses_kwargs(RESP_TYPE_USERVIEW, get_datatype(request), rel_path)) + dict2args(url_args(request, repeat=repeat, shuffle=shuffle, search=search))
  113. def url_webnail(request, rel_path):
  114. return reverse('pygal-responses', kwargs=pygal_responses_kwargs(RESP_TYPE_WEBNAIL, DATA_TYPE_ITEM, rel_path))
  115. def url_favouriteview(request):
  116. return url_search(request) + dict2args(url_args(request, repeat=REPEAT_DISABLE, shuffle=SHUFFLE_DISABLE, search='favourite_of:%s' % request.user.username))
  117. def url_favourite_set(request, rel_path):
  118. nxt = request.GET.get('next', request.get_full_path())
  119. return reverse('pygal-setfavourite', args=(1, rel_path, )) + '?next=%s' % nxt
  120. def url_favourite_unset(request, rel_path):
  121. nxt = request.GET.get('next', request.get_full_path())
  122. return reverse('pygal-setfavourite', args=(0, rel_path, )) + '?next=%s' % nxt
  123. def url_addtag(request, rel_path):
  124. nxt = request.GET.get('next', request.get_full_path())
  125. return reverse('pygal-addtag', args=(rel_path, )) + '?next=%s' % nxt
  126. def url_tagedit(request, tag_id):
  127. nxt = request.GET.get('next', request.get_full_path())
  128. return reverse('pygal-edittag', args=(tag_id, )) + '?next=%s' % nxt
  129. def is_helpview(request):
  130. return request.META['PATH_INFO'].startswith(reverse('pygal-helpview', kwargs={'page': '_'})[:-1])
  131. def is_infoview(request):
  132. return get_responsetype(request) == RESP_TYPE_INFOVIEW
  133. def is_repeatview(request):
  134. return 'repeat' in request.GET
  135. def is_favouriteview(request):
  136. return get_datatype(request) == DATA_TYPE_SEARCH and get_search_query(request) == 'favourite_of:%s' % request.user.username
  137. def is_searchview(request):
  138. return not is_favouriteview(request) and get_datatype(request) == DATA_TYPE_SEARCH
  139. def is_flat(request):
  140. return 'flat' in request.GET
  141. def get_shuffle_id(request):
  142. return request.GET.get('shuffle')
  143. def get_search_query(request):
  144. return request.GET.get('q')