def toggle_fav(request, slug): from favs.models import Favourite wallpaper = get_object_or_404(Wallpaper, slug=slug, forgiven=True) fav = is_this_fav(wallpaper, request.user) fav.toggle() return HttpResponseRedirect( reverse('view_wallpaper', kwargs={'slug': slug}))
def wallpaper(request, slug=None): # letmeknow when_saved, send_wallpaper = None, None post = {} if request.method == "POST": post = request.POST.copy() # FIXME: # FIXME: Se llama esta misma función tanto cuando se ve un wallpaper como # FIXME: cuando se va a descargarlo. Esto no debería ser así porque está # FIXME: creando mucha incoherencia y contorciones de código. if slug: wallpaper = get_object_or_404(Wallpaper, slug=slug) else: # Cuando quiero descargarlo debe haber sido perdonado previamente. wallpaper = get_object_or_404(Wallpaper, slug=post.get('wallpaper', None), forgiven=True) # Valido acciones extras que pueda querer el usuario. if 'action' in post: if post.get('action') == 'tellafriend': send_wallpaper = SendWallpaperForm(post, wallpaper=wallpaper) if send_wallpaper.is_valid(): return tell_a_friend(request, wallpaper) elif post.get('action') == 'letmeknow': when_saved = EmailMeWhenSavedWallpaperForm(post, wallpaper=wallpaper) if when_saved.is_valid(): return let_me_know(request, wallpaper) # Si el wallpaper está perdonado es muy probable que esté buscando # descargarlo. if wallpaper.forgiven and request.method == "POST" and \ post.get('action') == 'download': form = DownloadWallpaperForm(post) if form.is_valid(): # Hack alert! width, height = post['resolutions'].split('x') resolution = wallpaper.resolution.__class__.objects.get( width=width, height=height) resolution.downloads += 1 resolution.save() return get_response_and_file(wallpaper, str(resolution)) else: form = DownloadWallpaperForm(wallpaper=wallpaper) if not wallpaper.uploader == request.user: wallpaper.viewed += 1 wallpaper.save() # canadopt? # FIXME: # FIXME: can_adopt va en el profile. can_adopt = True if request.user.is_authenticated(): last_adoption = Wallpaper.objects.filter( uploader=request.user.id).exclude( adopted=None).order_by( '-adopted')[:1] if last_adoption: if (datetime.now() - last_adoption[0].adopted).days < 1: can_adopt = False # tellafriend if not send_wallpaper: send_wallpaper = SendWallpaperForm(user=request.user, wallpaper=wallpaper) # letmeknow if Waiting.objects.filter(wallpaper=wallpaper, user=request.user.id, sent=False).count() == 0: when_saved = EmailMeWhenSavedWallpaperForm(user=request.user, wallpaper=wallpaper) # rating # FIXME: # FIXME: Debería esto ir en el usuario? rates, my_rating = get_rate(wallpaper, request.user) # FIXME: # FIXME: Sacar esto de aca. # FIXME: Este código es para saber si el usuario ya votó este wallpaper. # FIXME: El problema es que corresponde a userprofile, pero todavía no lo # FIXME: puedo modificar. from limbo.models import Decision # FIXME: # FIXME: Debería mostrarle al usuario lo que eligió, no simplemente # FIXME: informarle que votó además de permitirle cambiar el voto. i_voted = Decision.objects.filter( judge=request.user.id, wallpaper=wallpaper).count() return 'wallpaper.html', \ {'wallpaper': wallpaper, 'resolutions_form': form, 'was_voted_on': i_voted, 'uploaded_by': _by(wallpaper), 'random_slug': wallpaper.random_slug( forgiven=wallpaper.forgiven, user=request.user, exclude=wallpaper.slug), 'let_me_know': when_saved, 'send_form': send_wallpaper, 'see_also': get_related(request, wallpaper), 'can_adopt': can_adopt, 'possible_rates': rates, 'i_rated': my_rating, 'is_fav': is_this_fav(wallpaper, request.user, bool=True)}