def details(request, id, template_name="photos/details.html", **kwargs): """ show the photo details """ group, bridge = group_and_bridge(kwargs) photos = Image.objects.all() if group: photos = group.content_objects(photos) else: photos = photos.filter(object_id=None) photo = get_object_or_404(photos, id=id) # @@@: test if not photo.is_public and request.user != photo.member: raise Http404 photo_url = photo.get_display_url() host = "http://%s" % get_host(request) return { "TEMPLATE": template_name, "group": group, "host": host, "photo": photo, "photo_url": photo_url, "is_me": photo.member == request.user, }
def password_reset_from_key(request, uidb36, key, **kwargs): form_class = kwargs.get("form_class", ResetPasswordKeyForm) template_name = kwargs.get("template_name", "account/password_reset_from_key.html") token_generator = kwargs.get("token_generator", default_token_generator) group, bridge = group_and_bridge(kwargs) ctx = group_context(group, bridge) # pull out user try: uid_int = base36_to_int(uidb36) except ValueError: raise Http404 user = get_object_or_404(User, id=uid_int) if token_generator.check_token(user, key): if request.method == "POST": password_reset_key_form = form_class(request.POST, user=user, temp_key=key) if password_reset_key_form.is_valid(): password_reset_key_form.save() messages.add_message(request, messages.SUCCESS, ugettext(u"Password successfully changed.")) password_reset_key_form = None else: password_reset_key_form = form_class() ctx.update({"form": password_reset_key_form}) else: ctx.update({"token_fail": True}) return render_to_response(template_name, RequestContext(request, ctx))
def memberphotos(request, username, template_name="photos/memberphotos.html", **kwargs): """ Get the members photos and display them """ group, bridge = group_and_bridge(kwargs) # test user exists user = get_object_or_404(User, username=username) photos = Image.objects.filter( member=user, is_public=True, ) if group: photos = group.content_objects(photos) else: photos = photos.filter(object_id=None) photos = photos.order_by("-date_added") return { "TEMPLATE": template_name, "group": group, "photos": photos, }
def destroy(request, id, **kwargs): group, bridge = group_and_bridge(kwargs) photos = Image.objects.all() if group: photos = group.content_objects(photos) else: photos = photos.filter(object_id=None) photo = get_object_or_404(photos, pk=id) title = photo.title if group: redirect_to = bridge.reverse("photos_yours", group) else: redirect_to = reverse("photos_yours") if photo.member != request.user: messages.add_message(request, messages.ERROR, ugettext("You can't edit photos that aren't yours") ) return HttpResponseRedirect(redirect_to) if request.method == "POST" and request.POST["action"] == "delete": photo.delete() messages.add_message(request, messages.SUCCESS, ugettext("Successfully deleted photo '%s'") % title ) return HttpResponseRedirect(redirect_to)
def other_services(request, **kwargs): from microblogging.utils import twitter_verify_credentials template_name = kwargs.pop("template_name", "account/other_services.html") group, bridge = group_and_bridge(kwargs) twitter_form = TwitterForm(request.user) twitter_authorized = False if request.method == "POST": twitter_form = TwitterForm(request.user, request.POST) if request.POST["actionType"] == "saveTwitter": if twitter_form.is_valid(): from microblogging.utils import twitter_account_raw twitter_account = twitter_account_raw(request.POST["username"], request.POST["password"]) twitter_authorized = twitter_verify_credentials(twitter_account) if not twitter_authorized: messages.add_message(request, messages.ERROR, ugettext("Twitter authentication failed")) else: twitter_form.save() messages.add_message(request, messages.SUCCESS, ugettext(u"Successfully authenticated.")) else: from microblogging.utils import twitter_account_for_user twitter_account = twitter_account_for_user(request.user) twitter_authorized = twitter_verify_credentials(twitter_account) twitter_form = TwitterForm(request.user) ctx = group_context(group, bridge) ctx.update({"twitter_form": twitter_form, "twitter_authorized": twitter_authorized}) return render_to_response(template_name, RequestContext(request, ctx))
def photos(request, template_name="photos/latest.html", **kwargs): """ latest photos """ group, bridge = group_and_bridge(kwargs) photos = Image.objects.filter( Q(is_public=True) | Q(is_public=False, member=request.user) ) # filter by group, otherwise show only with no group set if group: photos = group.content_objects(photos) else: photos = photos.filter(object_id=None) photos = photos.order_by("-date_added") return { "group": group, "photos": photos, 'TEMPLATE': template_name, }
def password_reset_done(request, **kwargs): template_name = kwargs.pop("template_name", "account/password_reset_done.html") group, bridge = group_and_bridge(kwargs) ctx = group_context(group, bridge) return render_to_response(template_name, RequestContext(request, ctx))
def edit(request, id, form_class=PhotoEditForm, template_name="photos/edit.html", **kwargs): group, bridge = group_and_bridge(kwargs) photos = Image.objects.all() if group: photos = group.content_objects(photos) else: photos = photos.filter(object_id=None) photo = get_object_or_404(photos, pk=id) photo_url = photo.get_display_url() if request.method == "POST": if photo.member != request.user: messages.add_message(request, messages.ERROR, ugettext("You can't edit photos that aren't yours") ) include_kwargs = {"id": photo.id} if group: redirect_to = bridge.reverse("photo_details", group, kwargs=include_kwargs) else: redirect_to = reverse("photo_details", kwargs=include_kwargs) return HttpResponseRedirect(reverse('photo_details', args=(photo.id,))) if request.POST["action"] == "update": photo_form = form_class(request.user, request.POST, instance=photo) if photo_form.is_valid(): photoobj = photo_form.save(commit=False) photoobj.save() messages.add_message(request, messages.SUCCESS, ugettext("Successfully updated photo '%s'") % photo.title ) include_kwargs = {"id": photo.id} if group: redirect_to = bridge.reverse("photo_details", group, kwargs=include_kwargs) else: redirect_to = reverse("photo_details", kwargs=include_kwargs) return HttpResponseRedirect(redirect_to) else: photo_form = form_class(instance=photo) else: photo_form = form_class(instance=photo) return { "TEMPLATE": template_name, "group": group, "photo_form": photo_form, "photo": photo, "photo_url": photo_url, }
def other_services_remove(request, **kwargs): group, bridge = group_and_bridge(kwargs) # @@@ this is a bit coupled OtherServiceInfo.objects.filter(user=request.user).filter( Q(key="twitter_user") | Q(key="twitter_password") ).delete() messages.add_message(request, messages.SUCCESS, ugettext("Removed twitter account information successfully.")) return HttpResponseRedirect(reverse("acct_other_services"))
def email(request, **kwargs): form_class = kwargs.pop("form_class", AddEmailForm) template_name = kwargs.pop("template_name", "account/email.html") group, bridge = group_and_bridge(kwargs) if request.method == "POST" and request.user.is_authenticated(): if request.POST["action"] == "add": add_email_form = form_class(request.user, request.POST) if add_email_form.is_valid(): add_email_form.save() messages.add_message( request, messages.INFO, ugettext(u"Confirmation email sent to %(email)s") % {"email": add_email_form.cleaned_data["email"]}, ) add_email_form = form_class() # @@@ else: add_email_form = form_class() if request.POST["action"] == "send": email = request.POST["email"] try: email_address = EmailAddress.objects.get(user=request.user, email=email) messages.add_message( request, messages.INFO, ugettext("Confirmation email sent to %(email)s") % {"email": email} ) EmailConfirmation.objects.send_confirmation(email_address) except EmailAddress.DoesNotExist: pass elif request.POST["action"] == "remove": email = request.POST["email"] try: email_address = EmailAddress.objects.get(user=request.user, email=email) email_address.delete() messages.add_message( request, messages.SUCCESS, ugettext("Removed email address %(email)s") % {"email": email} ) except EmailAddress.DoesNotExist: pass elif request.POST["action"] == "primary": email = request.POST["email"] email_address = EmailAddress.objects.get(user=request.user, email=email) email_address.set_as_primary() else: add_email_form = form_class() ctx = group_context(group, bridge) ctx.update({"add_email_form": add_email_form}) return render_to_response(template_name, RequestContext(request, ctx))
def login(request, **kwargs): form_class = kwargs.pop("form_class", LoginForm) template_name = kwargs.pop("template_name", "account/login.html") success_url = kwargs.pop("success_url", None) associate_openid = kwargs.pop("associate_openid", False) openid_success_url = kwargs.pop("openid_success_url", None) url_required = kwargs.pop("url_required", False) extra_context = kwargs.pop("extra_context", {}) redirect_field_name = kwargs.pop("redirect_field_name", "next") group, bridge = group_and_bridge(kwargs) if extra_context is None: extra_context = {} if request.method == "POST" and not url_required: form = form_class(request.POST, group=group) if form.login(request): if associate_openid and association_model is not None: for openid in request.session.get("openids", []): assoc, created = UserOpenidAssociation.objects.get_or_create(user=form.user, openid=openid.openid) success_url = openid_success_url or success_url message = ugettext(u"Successfully logged in as %(user)s.") % {"user": user_display(form.user)} messages.add_message(request, messages.SUCCESS, message) extra_context["message"] = message if success_url is None: success_url = get_default_redirect(request, redirect_field_name) if request.is_ajax(): extra_context["TEMPLATE"] = "" return extra_context return HttpResponseRedirect(success_url) else: extra_context["message"] = "Login failed" else: form = form_class(group=group) ctx = group_context(group, bridge) ctx.update( { "form": form, "url_required": url_required, "redirect_field_name": redirect_field_name, "redirect_field_value": request.GET.get(redirect_field_name), "TEMPLATE": template_name, } ) ctx.update(extra_context) return ctx
def fbphotos(request, template_name="photos/facebook.html", **kwargs): """ Fetch photos from facebook """ group, bridge = group_and_bridge(kwargs) fb_user = request.fb.users.getInfo(request.fb.uid)[0] fb_albums = [album for album in request.fb.photos.getAlbums() if album['type'] != 'profile'] # since facebook doesn't give us the album cover image urls directly we need to retrieve them in batch cover_pids_csv = ', '.join([album['cover_pid'] for album in fb_albums]) cover_urls = [photo['src_small'] for photo in request.fb.photos.get(pids=cover_pids_csv)] for i, album in enumerate(fb_albums): album['thumb_url'] = cover_urls[i] if request.POST: # Form is being submitted albums_form = FacebookPhotosForm(objects=fb_albums, data=request.POST) if albums_form.is_valid(): selected_ids = albums_form.cleaned_data['selected_ids'] selected_albums = [album for album in fb_albums if album['aid'] in selected_ids] # create/associate/remove fb albums from the tribe as necessary add_or_remove_fb_albums(request.user, tribe=group, albums=selected_albums) if group: redirect_to = bridge.reverse("photos", group) else: redirect_to = reverse("photos") return HttpResponseRedirect(redirect_to) # Get current tracked albums for this user/tribe to pre-select the form user_fb_albums = FacebookPhotoAlbum.objects.filter(owner=request.user) if group: user_fb_albums = group.content_objects(user_fb_albums) albums_form = FacebookPhotosForm(objects=fb_albums, initial=user_fb_albums.values_list('aid', flat=True)) # provide a url for facebook logout. fb_logout_url = '%s?nextview=%s' % (request.build_absolute_uri(reverse("oshare_fblogout")), request.get_full_path()) # facebook js init crossdomain setup url return { "TEMPLATE": template_name, "group": group, "fb_user": fb_user, "fb_albums": fb_albums, "fb_form" : albums_form, "fb_logout_url": fb_logout_url, "fb_api_key": settings.FACEBOOK_API_KEY, }
def password_delete(request, **kwargs): template_name = kwargs.pop("template_name", "account/password_delete.html") # prevent this view when openids is not present or it is empty. if not request.user.password or (not hasattr(request, "openids") or not getattr(request, "openids", None)): return HttpResponseForbidden() group, bridge = group_and_bridge(kwargs) if request.method == "POST": request.user.password = u"" request.user.save() return HttpResponseRedirect(reverse("acct_passwd_delete_done")) ctx = group_context(group, bridge) return render_to_response(template_name, RequestContext(request, ctx))
def signup(request, **kwargs): form_class = kwargs.pop("form_class", SignupForm) template_name = kwargs.pop("template_name", "account/signup.html") redirect_field_name = kwargs.pop("redirect_field_name", "next") success_url = kwargs.pop("success_url", None) group, bridge = group_and_bridge(kwargs) if success_url is None: success_url = get_default_redirect(request, redirect_field_name) if request.method == "POST": form = form_class(request.POST, group=group) if form.is_valid(): credentials = form.save(request=request) if settings.ACCOUNT_EMAIL_VERIFICATION: return render_to_response( "account/verification_sent.html", {"email": form.cleaned_data["email"]}, context_instance=RequestContext(request), ) else: user = authenticate(**credentials) auth_login(request, user) messages.add_message( request, messages.SUCCESS, ugettext("Successfully logged in as %(user)s.") % {"user": user_display(user)}, ) return HttpResponseRedirect(success_url) else: form = form_class(group=group) ctx = group_context(group, bridge) ctx.update( { "form": form, "redirect_field_name": redirect_field_name, "redirect_field_value": request.GET.get(redirect_field_name), } ) return render_to_response(template_name, RequestContext(request, ctx))
def timezone_change(request, **kwargs): form_class = kwargs.pop("form_class", ChangeTimezoneForm) template_name = kwargs.pop("template_name", "account/timezone_change.html") group, bridge = group_and_bridge(kwargs) if request.method == "POST": form = form_class(request.user, request.POST) if form.is_valid(): form.save() messages.add_message(request, messages.SUCCESS, ugettext(u"Timezone successfully updated.")) else: form = form_class(request.user) ctx = group_context(group, bridge) ctx.update({"form": form}) return render_to_response(template_name, RequestContext(request, ctx))
def yourphotos(request, template_name="photos/yourphotos.html", **kwargs): """ photos for the currently authenticated user """ group, bridge = group_and_bridge(kwargs) photos = Image.objects.filter(member=request.user) # filter by group if group: photos = group.content_objects(photos) photos = photos.order_by("-date_added") return { "TEMPLATE": template_name, "group": group, "photos": photos, }
def language_change(request, **kwargs): form_class = kwargs.pop("form_class", ChangeLanguageForm) template_name = kwargs.pop("template_name", "account/language_change.html") group, bridge = group_and_bridge(kwargs) if request.method == "POST": form = form_class(request.user, request.POST) if form.is_valid(): form.save() messages.add_message(request, messages.SUCCESS, ugettext(u"Language successfully updated.")) next = request.META.get("HTTP_REFERER", None) return HttpResponseRedirect(next) else: form = form_class(request.user) ctx = group_context(group, bridge) ctx.update({"form": form}) return render_to_response(template_name, RequestContext(request, ctx))
def password_set(request, **kwargs): form_class = kwargs.pop("form_class", SetPasswordForm) template_name = kwargs.pop("template_name", "account/password_set.html") group, bridge = group_and_bridge(kwargs) if request.user.password: return HttpResponseRedirect(reverse("acct_passwd")) if request.method == "POST": password_set_form = form_class(request.user, request.POST) if password_set_form.is_valid(): password_set_form.save() messages.add_message(request, messages.SUCCESS, ugettext(u"Password successfully set.")) return HttpResponseRedirect(reverse("acct_passwd")) else: password_set_form = form_class(request.user) ctx = group_context(group, bridge) ctx.update({"password_set_form": password_set_form}) return render_to_response(template_name, RequestContext(request, ctx))
def upload(request, form_class=PhotoUploadForm, template_name="photos/upload.html", **kwargs): """ upload form for photos """ group, bridge = group_and_bridge(kwargs) photo_form = form_class(request.user, group) if request.method == "POST": if request.POST.get("action") == "upload": photo_form = form_class(user=request.user, group=group, data=request.POST, files=request.FILES) if photo_form.is_valid(): photo = photo_form.save(commit=False) photo.member = request.user if group: group.associate(photo, commit=False) photo.save() messages.add_message(request, messages.SUCCESS, ugettext("Successfully uploaded photo '%s'") % photo.title ) include_kwargs = {"id": photo.id} if group: redirect_to = bridge.reverse("photo_details", group, kwargs=include_kwargs) else: redirect_to = reverse("photo_details", kwargs=include_kwargs) return HttpResponseRedirect(redirect_to) return { "TEMPLATE": template_name, "group": group, "photo_form": photo_form, }
def password_reset(request, **kwargs): form_class = kwargs.pop("form_class", ResetPasswordForm) template_name = kwargs.pop("template_name", "account/password_reset.html") group, bridge = group_and_bridge(kwargs) ctx = group_context(group, bridge) if request.method == "POST": password_reset_form = form_class(request.POST) if password_reset_form.is_valid(): email = password_reset_form.save() if group: redirect_to = bridge.reverse("acct_passwd_reset_done", group) else: redirect_to = reverse("acct_passwd_reset_done") return HttpResponseRedirect(redirect_to) else: password_reset_form = form_class() ctx.update({"password_reset_form": password_reset_form}) return render_to_response(template_name, RequestContext(request, ctx))