def create_tag(request): """Create a new tag """ if request.method == 'POST': form = TagForm(request.POST, request.FILES) if form.is_valid(): tag = form.save(commit=False) tag.user = request.user tag.save() # Get hashtags from tag.description or from POST['hashtags'] hashtags = tag.get_hashtags('description', request.POST['hashtags']) Hashtag.objects.update_tags(tag, hashtags) # Subscribe tag owner to tag try: track_tag(request, tag.id, silent=True) except Exception as ex: # TODO: Catch a real exception logging.debug( "Exception raised when subscribing tag owner '%s' to tag '%s': %s" % (request.user, tag, ex)) return HttpResponseRedirect(reverse(view_tag, kwargs={'id': tag.id})) else: form = TagForm() template = 'web/tag_form.html' if not is_mobile_browser(request) else 'mobile/tag_form.html' return render_to_response(template, {'form': form}, context_instance=RequestContext(request))
def view_flag(request, id): """Open the tag page, and populate panel with flag """ flag = get_object_or_404(Flag, pk=id) if not flag.is_visible_to(request.user): raise Http404 template = 'web/flag.html' if not is_mobile_browser(request) else 'mobile/flag.html' return render_to_response(template, {'flag': flag, 'tag': flag.tag}, context_instance=RequestContext(request))
def update_tag(request, code): """Flag a tag """ tag = get_object_or_404(Tag, pk=base62_decode(code.upper())) if tag.is_deleted: raise Http404 if request.method == 'POST': flag_form = FlagForm(request.POST, request.FILES) position_form = PositionForm(request.POST) if flag_form.is_valid() and position_form.is_valid(): # Save position position = position_form.save() # Save flag flag = flag_form.save(commit=False) flag.tag = tag flag.position = position if request.user.is_authenticated(): flag.user = request.user else: # Flags of unauthenticated users are always public flag.visibility = 'pub' flag.points = flag.calc_points() flag.save() # Get hashtags from flag.note or from POST['hashtags'] hashtags = flag.get_hashtags('note', request.POST['hashtags']) Hashtag.objects.update_tags(flag, hashtags) if request.user.is_authenticated(): if flag.points > 0: profile = request.user.get_profile() profile.points = profile.points + flag.points profile.save() # If logged in, redirect to the user's profile, or to the tag's redirect URL url = tag.redirect_url if tag.redirect_url else reverse(view_user, kwargs={'username': request.user.username}) return HttpResponseRedirect(url) # Otherwise view the tag (or go to its redirect URL) url = tag.redirect_url if tag.redirect_url else reverse(view_tag, kwargs={'id': tag.id}) return HttpResponseRedirect(url) else: flag_form = FlagForm() position_form = PositionForm() authform = PrettyAuthenticationForm(request) regform = RegistrationForm() next = reverse('trc_me.web.views.update_tag', kwargs={'code': code}) template = 'web/tag_update.html' if not is_mobile_browser(request) else 'mobile/tag_update.html' return render_to_response(template, {'tag': tag, 'authform': authform, 'regform': regform, 'flagform': flag_form, 'positionform': position_form, 'next': next}, context_instance=RequestContext(request))
def view_user(request, username=None): """Show account details """ from subhub.models import Subscription, SubscriptionTask # TODO: Add mobile if username == None: # Redirect to authenticated user's profile if request.user.is_authenticated(): return HttpResponseRedirect( reverse(view_user, kwargs={'username': request.user.username})) # User isn't authenticated. Redirect to landing page return HttpResponseRedirect(reverse(index)) if request.user.is_authenticated() and request.user.username == username: # It's the user's own profile u = request.user tags = Tag.objects.filter(user=request.user).filter(is_deleted=False) flags = Flag.objects.filter(user=request.user).order_by('-created_at') notis = Notification.objects.filter(user=request.user).order_by('-created_at')[:10] else: u = User.objects.get(username=username) tags = Tag.objects.get_visible_to(request.user).filter(user=u) flags = Flag.objects.get_visible_to(request.user).filter(user=u) notis = [] # Find out whether request.user is following user if request.user.is_authenticated(): callback = request.build_absolute_uri( reverse(follow_user_callback, kwargs={'username': request.user.username, 'tracked_username': u.username})) is_following = ( Subscription.objects.filter(callback=callback).count() \ + SubscriptionTask.objects.filter(callback=callback).filter(mode='subscribe').count() \ - SubscriptionTask.objects.filter(callback=callback).filter(mode='unsubscribe').count()) > 0 else: is_following = False profile = u.get_profile() profileform = ProfileForm(instance=profile) passwordform = PrettyPasswordChangeForm(user=u) template = 'web/user_home.html' if not is_mobile_browser(request) else 'mobile/user_home.html' return render_to_response(template, {'u': u, 'profile': profile, 'is_following': is_following, 'profileform': profileform, 'passwordform': passwordform, 'tags': tags, 'flags': flags, 'notis': notis}, context_instance=RequestContext(request))
def edit_user(request): profile = request.user.get_profile() if request.method == 'POST': profileform = ProfileForm(request.POST, request.FILES, instance=profile) if profileform.is_valid(): profileform.save() return HttpResponseRedirect(reverse(view_user, kwargs={'username': request.user.username})) else: profileform = ProfileForm(instance=profile) template = 'web/user_edit.html' if not is_mobile_browser(request) else 'mobile/user_edit.html' return render_to_response(template, {'profile': profile, 'profileform': profileform}, context_instance=RequestContext(request))
def search(request): if 'q' not in request.GET: # User came directly here with no search query return HttpResponseRedirect(reverse(index)) q = request.GET['q'].replace('#', '') flags = HashtaggedItem.objects.get_by_model(Flag.objects.get_visible_to(request.user), q) tags = HashtaggedItem.objects.get_by_model(Tag.objects.get_visible_to(request.user), q) # TODO: Find users whose full name is like q # TODO: Allow users to set their full names! #users = ... CONCAT(first_name, ' ', last_name) LIKE '%q%'; template = 'web/search_results.html' if not is_mobile_browser(request) else 'mobile/search_results.html' return render_to_response(template, {'tags': tags, 'flags': flags, 'q': request.GET['q']}, context_instance=RequestContext(request))
def index(request): """Landing page """ authform = PrettyAuthenticationForm() regform = RegistrationForm() # Featured flags flags = Flag.objects.filter(is_featured=True).all() #if request.device is not None \ # and request.device['resolution_width'] < 1024: template = 'web/index.html' if not is_mobile_browser(request) else 'mobile/index.html' next = reverse(index) return render_to_response(template, {'authform': authform, 'regform': regform, 'flags': flags, 'next': next}, context_instance=RequestContext(request))
def view_tag(request, id): """View a tag's details The creator of a tag can see everything about it. If a tag is public, anyone can see it and all its public flags. If a tag is not public, a user can see it only if they have flagged it, and then they can only see their flags. """ from subhub.models import Subscription, SubscriptionTask tag = get_object_or_404(Tag, pk=id) if tag.is_deleted: raise Http404 if not tag.is_visible_to(request.user): #return HttpResponseForbidden raise Http404 flags = tag.flag_set.get_visible_to(request.user) if request.user == tag.user and len(flags) == 0: offer_initial_flag = True else: offer_initial_flag = False form = TagForm(instance=tag) # Find out whether user is tracking tag if request.user.is_authenticated(): callback = request.build_absolute_uri( reverse(track_tag_callback, kwargs={'username': request.user.username, 'tag_id': tag.id})) is_tracking = ( Subscription.objects.filter(callback=callback).count() \ + SubscriptionTask.objects.filter(callback=callback).filter(mode='subscribe').count() \ - SubscriptionTask.objects.filter(callback=callback).filter(mode='unsubscribe').count()) > 0 else: is_tracking = False template = 'web/tag.html' if not is_mobile_browser(request) else 'mobile/tag.html' return render_to_response(template, {'tag': tag, 'form': form, 'flags': flags, 'initial': offer_initial_flag, 'is_tracking': is_tracking}, context_instance=RequestContext(request))
def edit_tag(request, id): """Edit a tag's details """ tag = get_object_or_404(Tag, pk=id) if tag.is_deleted: raise Http404 if request.method == 'POST': form = TagForm(request.POST, request.FILES, instance=tag) if form.is_valid(): tag = form.save() Hashtag.objects.update_tags(tag, request.POST['hashtags'].replace('#', '')) return HttpResponseRedirect(reverse(view_tag, kwargs={'id': tag.id})) else: form = TagForm(instance=tag) hashtags = ' '.join([u'#%s' % (t, ) for t in Hashtag.objects.get_for_object(tag)]) template = 'web/tag_form.html' if not is_mobile_browser(request) else 'mobile/tag_form.html' return render_to_response(template, {'form': form, 'hashtags': hashtags}, context_instance=RequestContext(request))