Django Library PyGal
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

__init__.py 6.7KB

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