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.8KB

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