Guest User

Untitled

a guest
Jun 2nd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. from django.http import HttpResponse, Http404, HttpResponseRedirect
  2.  
  3. from django.contrib.auth.models import User
  4. from django.contrib.auth.decorators import login_required
  5. from django.contrib.auth import logout
  6.  
  7. from django.shortcuts import render_to_response, get_object_or_404
  8.  
  9. from django.template import RequestContext
  10.  
  11. from bookmarks.forms import *
  12. from bookmarks.models import *
  13.  
  14. from django.core.exceptions import ObjectDoesNotExist
  15.  
  16. def main_page(request):
  17. return render_to_response('main_page.html', RequestContext(request))
  18.  
  19. def user_page(request, username):
  20. user = get_object_or_404(User, username=username)
  21. bookmarks = user.bookmark_set.order_by('-id')
  22. variables = RequestContext(request, {
  23. 'username': username,
  24. 'bookmarks': bookmarks,
  25. 'show_tags': True,
  26. 'show_edit': username == request.user.username,
  27. })
  28. return render_to_response('user_page.html', variables)
  29.  
  30. def logout_page(request):
  31. logout(request)
  32. return HttpResponseRedirect('/')
  33.  
  34. def register_page(request):
  35. if request.method == 'POST':
  36. form = RegistrationForm(request.POST)
  37. if form.is_valid():
  38. user = User.objects.create_user(
  39. username=form.clean_data['username'],
  40. password=form.clean_data['password1'],
  41. email=form.clean_data['email']
  42. )
  43. return HttpResponseRedirect('/register/success/')
  44. else:
  45. form = RegistrationForm()
  46. variables = RequestContext(request, { 'form': form })
  47. return render_to_response('registration/register.html', variables)
  48.  
  49. @login_required
  50. def bookmark_save_page(request):
  51. ajax = request.GET.has_key('ajax')
  52. if request.method == 'POST':
  53. form = BookmarkSaveForm(request.POST)
  54. if form.is_valid():
  55. bookmark = _bookmark_save(request, form)
  56. if ajax:
  57. variables = RequestContext(request, {
  58. 'bookmarks': [bookmark],
  59. 'show_edit': True,
  60. 'show_tags': True
  61. })
  62. return render_to_response('bookmark_list.html', variables)
  63. else:
  64. return HttpResponseRedirect('/user/%s/' % request.user.username)
  65. else:
  66. if ajax:
  67. return HttpResponse('failure')
  68. elif request.GET.has_key('url'):
  69. url = request.GET['url']
  70. title = ''
  71. tags = ''
  72. try:
  73. link = Link.objects.get(url=url)
  74. bookmark = Bookmark.objects.get(
  75. link=link,user=request.user
  76. )
  77. title = bookmark.title
  78. tags = ' '.join(
  79. tag.name for tag in bookmark.tag_set.all()
  80. )
  81. except ObjectDoesNotExist:
  82. pass
  83. form = BookmarkSaveForm({
  84. 'url': url,
  85. 'title': title,
  86. 'tags': tags
  87. })
  88. else:
  89. form = BookmarkSaveForm()
  90. variables = RequestContext(request, {'form': form})
  91. if ajax:
  92. return render_to_response('bookmark_save_form.html', variables)
  93. else:
  94. return render_to_response('bookmark_save.html', variables)
  95.  
  96. def _bookmark_save(request, form):
  97. # Get or create link
  98. link, dummy = Link.objects.get_or_create(
  99. url=form.clean_data['url']
  100. )
  101. # Get or create a bookmark
  102. bookmark, created = Bookmark.objects.get_or_create(
  103. user = request.user,
  104. link=link
  105. )
  106. # Update bookmark title
  107. bookmark.title = form.clean_data['title']
  108. # If the bookmark is being updated, clean old tag list.
  109. if not created:
  110. bookmark.tag_set.clear()
  111. # Create new tag list.
  112. tag_names = form.clean_data['tags'].split()
  113. for tag_name in tag_names:
  114. tag, dummy = Tag.objects.get_or_create(name=tag_name)
  115. bookmark.tag_set.add(tag)
  116. # Save bookmark to the database
  117. bookmark.save()
  118. return bookmark
  119.  
  120. def tag_page(request, tag_name):
  121. tag = get_object_or_404(Tag, name=tag_name)
  122. bookmarks = tag.bookmarks.order_by('-id')
  123. variables = RequestContext(request, {
  124. 'bookmarks': bookmarks,
  125. 'tag_name': tag_name,
  126. 'show_tags': True,
  127. 'show_user': True
  128. })
  129. return render_to_response('tag_page.html', variables)
  130.  
  131. def tag_cloud_page(request):
  132. MAX_WEIGHT = 5
  133. tags = Tag.objects.order_by('name')
  134. # Calculate tag, min and max counts.
  135. min_count = max_count = tags[0].bookmarks.count()
  136. for tag in tags:
  137. tag.count = tag.bookmarks.count()
  138. if tag.count < min_count:
  139. min_count = tag.count
  140. if max_count < tag.count:
  141. max_count = tag.count
  142. # Calculate count range. Avoid dividing by zero.
  143. range = float(max_count - min_count)
  144. if range == 0.0:
  145. range = 1.0
  146. # Calculate tag weights
  147. for tag in tags:
  148. tag.weight = int(
  149. MAX_WEIGHT * (tag.count - min_count) / range
  150. )
  151. variables = RequestContext(request, {
  152. 'tags': tags
  153. })
  154. return render_to_response('tag_cloud_page.html', variables)
  155.  
  156. def search_page(request):
  157. form = SearchForm()
  158. bookmarks = []
  159. show_results = False
  160. if request.GET.has_key('query'):
  161. show_results = True
  162. query = request.GET['query'].strip()
  163. if query:
  164. form = SearchForm({'query': query})
  165. bookmarks = Bookmark.objects.filter(title__icontains=query)[:10]
  166. variables = RequestContext(request, {
  167. 'form': form,
  168. 'bookmarks': bookmarks,
  169. 'show_results': show_results,
  170. 'show_tags': True,
  171. 'show_user': True
  172. })
  173. if request.GET.has_key('ajax'):
  174. return render_to_response('bookmark_list.html', variables)
  175. else:
  176. return render_to_response('search.html', variables)
Add Comment
Please, Sign In to add comment