def invite_friend(request, username, redirect_to_view=None, message=_("I would like to add you to my friends.")): """ Invite user to be user friend. """ friend = get_object_or_404(User, username=username) if request.method == "POST": form = InviteFriendForm(data=request.POST, user=request.user) if form.is_valid(): form.save() to_user = User.objects.get(username=request.POST["to_user"]) inviting_friend.send(FriendshipInvitation, request=request, to_user=to_user) messages.success( request, _("Friendship invitation for %(username)s was created.") % {'username': username}, fail_silently=True) if not redirect_to_view: redirect_to_view = list_sent_invitations return redirect(redirect_to_view) else: form = InviteFriendForm(initial={ 'to_user': username, 'message': message }) return render_to_response('friends/friend_invite.html', { 'form': form, 'friend': friend }, context_instance=RequestContext(request))
def post(self, *args, **kw): if self.request.POST.get("action") == "invite": username = self.request.POST.get("to_user") other_user = get_object_or_404(User, username=username) invite_form = InviteFriendForm(self.request.user, self.request.POST) if invite_form.is_valid(): invite_form.save() messages.success( self.request, _("Friendship requested with %(username)s") % {'username': invite_form.cleaned_data['to_user']}) elif self.request.POST.get("action") == "remove": username = kw['username'] other_user = get_object_or_404(User, username=username) Friendship.objects.remove(self.request.user, other_user) messages.add_message( self.request, messages.SUCCESS, ugettext("You have removed %(from_user)s from friends") % {"from_user": other_user}) else: username = kw['username'] other_user = get_object_or_404(User, username=username) invite_form = InviteFriendForm( self.request.user, { "to_user": username, "message": ugettext("Let's be friends!"), }) invitation_id = self.request.POST.get("invitation", None) if self.request.POST.get("action") == "accept": try: invitation = FriendshipInvitation.objects.get( id=invitation_id) if invitation.to_user == self.equest.user: invitation.accept() messages.add_message( self.request, messages.SUCCESS, ugettext( "You have accepted the friendship request from %(from_user)s" ) % {"from_user": invitation.from_user}) except FriendshipInvitation.DoesNotExist: pass elif self.request.POST.get("action") == "decline": try: invitation = FriendshipInvitation.objects.get( id=invitation_id) if invitation.to_user == self.request.user: invitation.decline() messages.add_message( self.request, messages.SUCCESS, ugettext( "You have declined the friendship request from %(from_user)s" ) % {"from_user": invitation.from_user}) except FriendshipInvitation.DoesNotExist: pass return HttpResponseRedirect( reverse("timeline.views.user_home", kwargs={"username": username}))
def post(self, *args, **kw): if self.request.POST.get("action") == "invite": username = self.request.POST.get("to_user") other_user = get_object_or_404(User, username=username) invite_form = InviteFriendForm(self.request.user, self.request.POST) if invite_form.is_valid(): invite_form.save() messages.success(self.request, _("Friendship requested with %(username)s") % { 'username': invite_form.cleaned_data['to_user'] }) elif self.request.POST.get("action") == "remove": username = kw['username'] other_user = get_object_or_404(User, username=username) Friendship.objects.remove(self.request.user, other_user) messages.add_message(self.request, messages.SUCCESS, ugettext("You have removed %(from_user)s from friends") % { "from_user": other_user } ) else: username = kw['username'] other_user = get_object_or_404(User, username=username) invite_form = InviteFriendForm(self.request.user, { "to_user": username, "message": ugettext("Let's be friends!"), }) invitation_id = self.request.POST.get("invitation", None) if self.request.POST.get("action") == "accept": try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == self.equest.user: invitation.accept() messages.add_message(self.request, messages.SUCCESS, ugettext("You have accepted the friendship request from %(from_user)s") % { "from_user": invitation.from_user } ) except FriendshipInvitation.DoesNotExist: pass elif self.request.POST.get("action") == "decline": try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == self.request.user: invitation.decline() messages.add_message(self.request, messages.SUCCESS, ugettext("You have declined the friendship request from %(from_user)s") % { "from_user": invitation.from_user } ) except FriendshipInvitation.DoesNotExist: pass return HttpResponseRedirect(reverse("profile_detail", kwargs={"username": username}))
def invite_friend(request, username, redirect_to_view=None, message=_("I would like to add you to my friends.")): """ Invite user to be user friend. """ if request.method == "POST": form = InviteFriendForm(data=request.POST, user=request.user) if form.is_valid(): form.save() messages.success(request, _("Friendship invitation for %(username)s was created.") % {'username': username}, fail_silently=True) if not redirect_to_view: redirect_to_view = list_sent_invitations return redirect(redirect_to_view) else: form = InviteFriendForm(initial={'to_user': username, 'message': message}) return render_to_response('friends/friend_invite.html', {'form': form, 'username': username}, context_instance=RequestContext(request))
def request_friendship(request, username, template_name="friends_app/request.html"): if request.user.is_authenticated() and request.method == "POST": if request.POST["action"] == "invite": invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() referer = request.META.get('HTTP_REFERER', None) if referer: return HttpResponseRedirect(referer) else: return render_to_response(template_name, {"invite_form": invite_form}, context_instance=RequestContext(request)) else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': "", }) if request.POST["action"] == "accept": # @@@ perhaps the form should just post to friends and be redirected here invitation_id = request.POST["invitation"] try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: if request.user.is_authenticated(): invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': "", }) return render_to_response(template_name, {"to_user": username, "invite_form": invite_form}, context_instance=RequestContext(request)) else: return HttpResponse(content="<h3>You are not logged in</h3>")
def profile(request, username, template_name="profiles/profile.html"): other_user = get_object_or_404(User, username=username) other_user.save() p = other_user.get_profile() p.save() if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) is_following = Following.objects.is_following(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False is_following = False if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None else: if request.user.is_authenticated() and request.method == "POST": if request.POST["action"] == "invite": invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) if request.POST["action"] == "accept": # @@@ perhaps the form should just post to friends and be redirected here invitation_id = request.POST["invitation"] try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) previous_invitations_to = FriendshipInvitation.objects.filter(to_user=other_user, from_user=request.user) previous_invitations_from = FriendshipInvitation.objects.filter(to_user=request.user, from_user=other_user) if is_me: if request.method == "POST": if request.POST["action"] == "update": profile_form = ProfileForm(request.POST, instance=other_user.get_profile()) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = other_user profile.save() else: profile_form = ProfileForm(instance=other_user.get_profile()) else: profile_form = ProfileForm(instance=other_user.get_profile()) else: profile_form = None interests = get_tags(tagged=other_user.get_profile(), tagged_for=other_user, tag_type='interest') skills = get_tags(tagged = other_user.get_profile(), tagged_for=other_user, tag_type='skill') needs = get_tags(tagged = other_user.get_profile(), tagged_for=other_user, tag_type='need') profile = other_user.get_profile() user = request.user if not user.is_authenticated(): user = get_anon_user() user_type = ContentType.objects.get_for_model(other_user) other_user_tweets = Tweet.objects.filter(sender_type=user_type, sender_id=other_user.id).order_by("-sent") # other_user if other_user_tweets : latest_status = other_user_tweets[0] dummy_status = DisplayStatus( defaultfilters.safe( defaultfilters.urlize(latest_status.html())), defaultfilters.timesince(latest_status.sent) ) else : dummy_status = DisplayStatus('No status', '') profile = secure_wrap(profile, user) try: profile.get_all_sliders perms_bool = True except PlusPermissionsNoAccessException: perms_bool = False profile = TemplateSecureWrapper(profile) search_type = 'profile_list' search_types = narrow_search_types() search_types_len = len(search_types) search_type_label = search_types[0][1][2] return render_to_response(template_name, { "profile_form": profile_form, "is_me": is_me, "is_friend": is_friend, "is_following": is_following, "other_user": other_user, "profile":profile, "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, "head_title" : "%s" % other_user.get_profile().get_display_name(), "head_title_status" : dummy_status, "host_info" : other_user.get_profile().get_host_info(), "skills" : skills, "needs" : needs, "interests" : interests, "other_user_tweets" : other_user_tweets, "permissions":perms_bool, "search_type":search_type, "search_type_label":search_type_label, "search_types_len":search_types_len }, context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html", extra_context=None): if extra_context is None: extra_context = {} other_user = get_object_or_404(User, username=username) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) is_following = Following.objects.is_following(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False is_following = False if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None if request.method == "POST": if request.POST.get( "action" ) == "remove": # @@@ perhaps the form should just post to friends and be redirected here Friendship.objects.remove(request.user, other_user) messages.add_message( request, messages.SUCCESS, ugettext("You have removed %(from_user)s from friends") % {"from_user": other_user}) is_friend = False invite_form = InviteFriendForm( request.user, { "to_user": username, "message": ugettext("Let's be friends!"), }) else: if request.user.is_authenticated() and request.method == "POST": if request.POST.get( "action" ) == "invite": # @@@ perhaps the form should just post to friends and be redirected here invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: invite_form = InviteFriendForm( request.user, { "to_user": username, "message": ugettext("Let's be friends!"), }) invitation_id = request.POST.get("invitation", None) if request.POST.get( "action" ) == "accept": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get( id=invitation_id) if invitation.to_user == request.user: invitation.accept() messages.add_message( request, messages.SUCCESS, ugettext( "You have accepted the friendship request from %(from_user)s" ) % {"from_user": invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user( other_user) except FriendshipInvitation.DoesNotExist: pass elif request.POST.get( "action" ) == "decline": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get( id=invitation_id) if invitation.to_user == request.user: invitation.decline() messages.add_message( request, messages.SUCCESS, ugettext( "You have declined the friendship request from %(from_user)s" ) % {"from_user": invitation.from_user}) other_friends = Friendship.objects.friends_for_user( other_user) except FriendshipInvitation.DoesNotExist: pass previous_invitations_to = FriendshipInvitation.objects.invitations( to_user=other_user, from_user=request.user) previous_invitations_from = FriendshipInvitation.objects.invitations( to_user=request.user, from_user=other_user) else: invite_form = InviteFriendForm( request.user, { "to_user": username, "message": ugettext("Let's be friends!"), }) previous_invitations_to = None previous_invitations_from = None return render_to_response( template_name, dict( { "is_me": is_me, "is_friend": is_friend, "is_following": is_following, "other_user": other_user, "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, }, **extra_context), context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html", extra_context=None): if extra_context is None: extra_context = {} other_user = get_object_or_404(User, username=username) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False previous_invitations_to = FriendshipInvitation.objects.invitations(to_user=other_user, from_user=request.user) previous_invitations_from = FriendshipInvitation.objects.invitations(to_user=request.user, from_user=other_user) else: other_friends = [] is_friend = False is_me = False previous_invitations_to = None previous_invitations_from = None if is_friend: invite_form = None if request.method == "POST": if request.POST.get("action") == "remove": # @@@ perhaps the form should just post to friends and be redirected here Friendship.objects.remove(request.user, other_user) request.user.message_set.create(message=_("You have removed %(from_user)s from friends") % {'from_user': other_user}) is_friend = False invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's Connect!"), }) else: if request.user.is_authenticated() and request.method == "POST": if request.POST.get("action") == "invite": # @@@ perhaps the form should just post to friends and be redirected here invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be Connect!"), }) invitation_id = request.POST.get("invitation", None) if request.POST.get("action") == "accept": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_("You have accepted the connection request from %(from_user)s") % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass elif request.POST.get("action") == "decline": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.decline() request.user.message_set.create(message=_("You have declined the connection request from %(from_user)s") % {'from_user': invitation.from_user}) other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's Connect!"), }) return render_to_response(template_name, dict({ "is_me": is_me, "is_friend": is_friend, "other_user": other_user, "datasets": other_user.datasets.filter(PermissionsRegistry.get_filter("dataset.can_view", request.user)), "exhibits": other_user.exhibits.filter(PermissionsRegistry.get_filter("exhibit.can_view", request.user)), "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, }, **extra_context), context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html", extra_context=None): if extra_context is None: extra_context = {} other_user = get_object_or_404(User, username=username) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) is_following = Following.objects.is_following(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False is_following = False if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None else: if request.user.is_authenticated() and request.method == "POST": if request.POST["action"] == "invite": invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) if request.POST["action"] == "accept": # @@@ perhaps the form should just post to friends and be redirected here invitation_id = request.POST["invitation"] try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) previous_invitations_to = FriendshipInvitation.objects.filter(to_user=other_user, from_user=request.user).exclude(status=8) previous_invitations_from = FriendshipInvitation.objects.filter(to_user=request.user, from_user=other_user).exclude(status=8) if is_me: if request.method == "POST": if request.POST["action"] == "update": profile_form = ProfileForm(request.POST, instance=other_user.get_profile()) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = other_user profile.save() else: profile_form = ProfileForm(instance=other_user.get_profile()) else: profile_form = ProfileForm(instance=other_user.get_profile()) else: profile_form = None return render_to_response(template_name, dict({ "profile_form": profile_form, "is_me": is_me, "is_friend": is_friend, "is_following": is_following, "other_user": other_user, "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, }, **extra_context), context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html", extra_context=None): if extra_context is None: extra_context = {} other_user = get_object_or_404(User, username=username) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) is_following = Following.objects.is_following(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False is_following = False if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None if request.method == "POST": if request.POST["action"] == "remove": Friendship.objects.remove(request.user, other_user) request.user.message_set.create(message=_("You have removed %(from_user)s from friends") % {'from_user': other_user}) is_friend = False invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) else: if request.user.is_authenticated() and request.method == "POST": if request.POST["action"] == "invite": invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) invitation_id = request.POST.get("invitation", "") if request.POST["action"] == "accept": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass elif request.POST["action"] == "decline": try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.decline() request.user.message_set.create(message=_("You have declined the friendship request from %(from_user)s") % {'from_user': invitation.from_user}) other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) if request.user.is_authenticated(): previous_invitations_to = FriendshipInvitation.objects.filter(to_user=other_user, from_user=request.user).exclude(status=8).exclude(status=6) previous_invitations_from = FriendshipInvitation.objects.filter(to_user=request.user, from_user=other_user).exclude(status=8).exclude(status=6) if is_me: if request.method == "POST": if request.POST["action"] == "update": profile_form = ProfileForm(request.POST, instance=other_user.get_profile()) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = other_user profile.save() else: profile_form = ProfileForm(instance=other_user.get_profile()) else: profile_form = ProfileForm(instance=other_user.get_profile()) else: profile_form = None total_duration = timedelta() total_distance = 0 total_avg_speed = 0 total_avg_hr = 0 nr_trips = 0 nr_hr_trips = 0 longest_trip = 0 avg_length = 0 avg_duration = 0 bmidataseries = '' bmiline = '' pulsedataseries = "" tripdataseries = "" avgspeeddataseries = "" avghrdataseries = "" ftpdataseries = '' height = other_user.get_profile().height if height: height = float(other_user.get_profile().height)/100 weightqs = other_user.get_profile().userprofiledetail_set.filter(weight__isnull=False).order_by('time') for wtuple in weightqs.values_list('time', 'weight'): bmidataseries += '[%s, %s],' % (datetime2jstimestamp(wtuple[0]), wtuple[1]/(height*height)) bmiline += '[%s, 25],' %datetime2jstimestamp(wtuple[0]) pulseqs = other_user.get_profile().userprofiledetail_set.filter(resting_hr__isnull=False).order_by('time') for hrtuple in pulseqs.values_list('time', 'resting_hr'): pulsedataseries += '[%s, %s],' % (datetime2jstimestamp(hrtuple[0]), hrtuple[1]) ftpqs = other_user.get_profile().userprofiledetail_set.filter(ftp__isnull=False).order_by('time') for ftuple in ftpqs.values_list('time', 'ftp'): ftpdataseries += '[%s, %s],' % (datetime2jstimestamp(ftuple[0]), ftuple[1]) exerciseqs = other_user.exercise_set.select_related('route','exercise_type').order_by('date') exerciseqs_dataseries = other_user.exercise_set.select_related('route','exercise_type').order_by('date')[:7] i = 0 for trip in reversed(exerciseqs): if trip.avg_speed and trip.exercise_type.name=="Cycling": # only increase counter if trip has speed avgspeeddataseries += '[%s, %s],' % (datetime2jstimestamp(trip.date), trip.avg_speed) i += 1 if i > 7: break i = 0 for trip in reversed(exerciseqs): if trip.avg_hr: avghrdataseries += '[%s, %s],' % (datetime2jstimestamp(trip.date), trip.avg_hr) i += 1 if i > 7: break for trip in exerciseqs: if trip.route: tripdataseries += '[%s, %s],' % ( nr_trips, trip.route.distance) if trip.route.distance > longest_trip: longest_trip = round(trip.route.distance) total_distance += trip.route.distance if trip.duration: total_duration += trip.duration if trip.avg_speed: # only increase counter if trip has speed total_avg_speed += trip.avg_speed nr_trips += 1 if total_avg_speed: total_avg_speed = total_avg_speed/nr_trips for event in exerciseqs: if event.avg_hr: total_avg_hr += event.avg_hr nr_hr_trips += 1 if total_avg_hr: total_avg_hr = total_avg_hr/nr_hr_trips total_kcals = max(0, other_user.exercise_set.aggregate(Sum('kcal'))['kcal__sum']) # TODO fix total_duration for hike and otherexercise # Todo filter ? for cycletrip in exerciseqs: if exerciseqs[0].date: days_since_start = (datetime.now() - datetime(exerciseqs[0].date.year, exerciseqs[0].date.month, exerciseqs[0].date.day, 0, 0, 0)).days # TODO check in exercise and hike for first if days_since_start == 0: # if they only have one trip and it was today days_since_start = 1 km_per_day = total_distance / days_since_start kcal_per_day = total_kcals / days_since_start time_per_day = total_duration / days_since_start latest_exercises = other_user.exercise_set.select_related('route','exercise_type', 'user').order_by('-date')[:20] return render_to_response(template_name, locals(), context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html", extra_context=None): if extra_context is None: extra_context = {} other_user = get_object_or_404(User, username=username) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) is_following = Following.objects.is_following(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False is_following = False if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None if request.method == "POST": if request.POST.get("action") == "remove": # @@@ perhaps the form should just post to friends and be redirected here Friendship.objects.remove(request.user, other_user) messages.add_message(request, messages.SUCCESS, ugettext("You have removed %(from_user)s from friends") % { "from_user": other_user } ) is_friend = False invite_form = InviteFriendForm(request.user, { "to_user": username, "message": ugettext("Let's be friends!"), }) else: if request.user.is_authenticated() and request.method == "POST": if request.POST.get("action") == "invite": # @@@ perhaps the form should just post to friends and be redirected here invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: invite_form = InviteFriendForm(request.user, { "to_user": username, "message": ugettext("Let's be friends!"), }) invitation_id = request.POST.get("invitation", None) if request.POST.get("action") == "accept": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() messages.add_message(request, messages.SUCCESS, ugettext("You have accepted the friendship request from %(from_user)s") % { "from_user": invitation.from_user } ) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass elif request.POST.get("action") == "decline": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.decline() messages.add_message(request, messages.SUCCESS, ugettext("You have declined the friendship request from %(from_user)s") % { "from_user": invitation.from_user } ) other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: invite_form = InviteFriendForm(request.user, { "to_user": username, "message": ugettext("Let's be friends!"), }) # Me user_auth=request.user.is_authenticated() if user_auth: previous_invitations_from=FriendshipInvitation.objects.invitations(to_user=request.user, from_user=other_user) previous_invitations_to = FriendshipInvitation.objects.invitations(to_user=other_user, from_user=request.user) else: previous_invitations_from=user_auth previous_invitations_to = user_auth return render_to_response(template_name, dict({ "is_me": is_me, "is_friend": is_friend, "is_following": is_following, "other_user": other_user, "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, }, **extra_context), context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html"): other_user = get_object_or_404(User, username=username) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) is_following = Following.objects.is_following(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False is_following = False if request.user.is_authenticated() and request.method == "POST" and not is_me: # @@@ some of this should go in zwitschern itself if request.POST["action"] == "follow": Following.objects.follow(request.user, other_user) is_following = True request.user.message_set.create(message=_("You are now following %(other_user)s") % {'other_user': other_user}) if notification: notification.send([other_user], "tweet_follow", {"user": request.user}) elif request.POST["action"] == "unfollow": Following.objects.unfollow(request.user, other_user) is_following = False request.user.message_set.create(message=_("You have stopped following %(other_user)s") % {'other_user': other_user}) if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None else: if request.user.is_authenticated() and request.method == "POST": if request.POST["action"] == "delete": file_form = UploadFileForm() fname = request.POST['file'] delete_uploaded_file(fname, request.user) invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) request.user.message_set.create(message=_("You have deleted %(file)s") % {'file': fname}) elif request.POST["action"] == "upload": invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) file_form = UploadFileForm(request.POST, request.FILES) if file_form.is_valid(): uFile = handle_uploaded_file(request.FILES['file'], request.user) myAccount = Account.objects.get(user = request.user) myAccount.files.add(uFile) myAccount.save() request.user.message_set.create(message="uploaded %(file)s"%{'file':request.FILES['file'].name}) elif request.POST["action"] == "invite": file_form = UploadFileForm() invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: file_form = UploadFileForm() invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) if request.POST["action"] == "accept": # @@@ perhaps the form should just post to friends and be redirected here invitation_id = request.POST["invitation"] try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: file_form = UploadFileForm() invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) previous_invitations_to = FriendshipInvitation.objects.filter(to_user=other_user, from_user=request.user) previous_invitations_from = FriendshipInvitation.objects.filter(to_user=request.user, from_user=other_user) if is_me: file_form = UploadFileForm() myAccount = Account.objects.get(user = request.user) file_list = myAccount.files.all() if request.method == "POST": if request.POST["action"] == "update": profile_form = ProfileForm(request.POST, instance=other_user.get_profile()) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = other_user profile.save() else: profile_form = ProfileForm(instance=other_user.get_profile()) else: profile_form = ProfileForm(instance=other_user.get_profile()) else: file_form = None file_list = None profile_form = None return render_to_response(template_name, { "profile_form": profile_form, "is_me": is_me, "is_friend": is_friend, "is_following": is_following, "other_user": other_user, "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, "file_list":file_list, "file_form":file_form }, context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html"): other_user = get_object_or_404(User, username=username) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) is_following = Following.objects.is_following(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False is_following = False if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None else: if request.user.is_authenticated() and request.method == "POST": if request.POST["action"] == "invite": invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: invite_form = InviteFriendForm( request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) if request.POST[ "action"] == "accept": # @@@ perhaps the form should just post to friends and be redirected here invitation_id = request.POST["invitation"] try: invitation = FriendshipInvitation.objects.get( id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_( "You have accepted the friendship request from %(from_user)s" ) % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user( other_user) except FriendshipInvitation.DoesNotExist: pass else: invite_form = InviteFriendForm( request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) previous_invitations_to = FriendshipInvitation.objects.filter( to_user=other_user, from_user=request.user) previous_invitations_from = FriendshipInvitation.objects.filter( to_user=request.user, from_user=other_user) if is_me: if request.method == "POST": if request.POST["action"] == "update": profile_form = ProfileForm(request.POST, instance=other_user.get_profile()) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = other_user profile.save() else: profile_form = ProfileForm(instance=other_user.get_profile()) else: profile_form = ProfileForm(instance=other_user.get_profile()) else: profile_form = None return render_to_response( template_name, { "profile_form": profile_form, "is_me": is_me, "is_friend": is_friend, "is_following": is_following, "other_user": other_user, "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, }, context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html"): other_user = get_object_or_404(User, username=username) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None else: if request.user.is_authenticated() and request.method == "POST": if request.POST["action"] == "invite": invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) if request.POST["action"] == "accept": # @@@ perhaps the form should just post to friends and be redirected here invitation_id = request.POST["invitation"] try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) previous_invitations_to = FriendshipInvitation.objects.filter(to_user=other_user, from_user=request.user) previous_invitations_from = FriendshipInvitation.objects.filter(to_user=request.user, from_user=other_user) profile_form = None extended_form = None if is_me: baseprofile = other_user.get_profile() extended_profile = None if hasattr(baseprofile, 'get_extended_profile'): extended_profile = baseprofile.get_extended_profile() if request.method == "POST": if request.POST["action"] == "update": profile_form = ProfileForm(request.POST, instance=baseprofile) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = other_user profile.save() else: if extended_profile: extended_form = baseprofile.get_extended_profile_form()(instance=extended_profile) profile_form = ProfileForm(instance=baseprofile) else: if extended_profile: extended_form = baseprofile.get_extended_profile_form()(instance=extended_profile) profile_form = ProfileForm(instance=baseprofile) participated = None return render_to_response(template_name, { "profile_form": profile_form, "extended_form": extended_form, "is_me": is_me, "participated": participated, "unread_messages_num": other_user.received_messages.filter(read_at__isnull=True).count(), #"friendship_requests_num": other_user.invitations_to.filter(status__in=[1,2]).count(), "is_friend": is_friend, "other_user": other_user, "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, }, context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html", extra_context=None): if extra_context is None: extra_context = {} other_user = get_object_or_none(User, username=username) if other_user == None or other_user.is_staff or (not other_user.get_profile().active and request.user != other_user and not request.user.is_staff) or (not other_user.is_active and not request.user.is_staff): return render_to_response('profiles/profile_404.html', context_instance=RequestContext(request)) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) is_following = Following.objects.is_following(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False is_following = False if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None if request.method == "POST": if request.POST.get("action") == "remove": # @@@ perhaps the form should just post to friends and be redirected here Friendship.objects.remove(request.user, other_user) request.user.message_set.create(message=_("You have removed %(from_user)s from mentor contacts") % {'from_user': other_user}) is_friend = False invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Please review my cv and accept my request!"), }) else: if request.user.is_authenticated() and request.method == "POST": if request.POST.get("action") == "invite": # @@@ perhaps the form should just post to friends and be redirected here invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() elif request.POST.get("action") == "renounce": invitation = FriendshipInvitation.objects.sent_invitations(to_user=other_user, from_user=request.user)[0] invitation.renounce() request.user.message_set.create(message=_("You have chosen to renounce this request %(from_user)s") % {'from_user': invitation.from_user}) invite_form = None else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Please review my cv and accept my request!"), }) invitation_id = request.POST.get("invitation", None) if request.POST.get("action") == "accept": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_("You have accepted the mentorship request from %(from_user)s") % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass elif request.POST.get("action") == "decline": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.decline() request.user.message_set.create(message=_("You have declined the mentorship request from %(from_user)s. Please write a message to this user motivating your decision.") % {'from_user': invitation.from_user}) other_friends = Friendship.objects.friends_for_user(other_user) return HttpResponseRedirect(reverse('messages.views.compose', kwargs={'recipient':invitation.from_user})) except FriendshipInvitation.DoesNotExist: pass elif request.POST.get("action") == "pending": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.pending() request.user.message_set.create(message=_("You have chosen to review the mentorship request from %(from_user)s") % {'from_user': invitation.from_user}) other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Please review my cv and accept my request!"), }) previous_invitations_to = FriendshipInvitation.objects.invitations(to_user=other_user, from_user=request.user) previous_invitations_from = FriendshipInvitation.objects.invitations(to_user=request.user, from_user=other_user) previous_denied_invitation_to = FriendshipInvitation.objects.invitationsDenied(to_user=other_user, from_user=request.user) deny_mentor_request = False consumed_all_requests = False mentor_can_accept = False if request.user.is_authenticated and not request.user.is_staff: if request.user.get_profile().as_student() == None or other_user.get_profile().as_mentor() == None: deny_mentor_request = True consumed_all_requests = FriendshipInvitation.objects.countRequests(from_user = request.user) >= 3 if request.user.get_profile().as_mentor() != None and FriendshipInvitation.objects.countAccepts(to_user = request.user) < 3: mentor_can_accept = True mentor = None if not request.user.is_staff and not request.user.is_superuser: mentor = request.user.get_profile().as_mentor() other_mentor = other_user.get_profile().as_mentor() if mentor != None and other_mentor != None and mentor != other_mentor: if not mentor.visible_to_mentors or not other_mentor.visible_to_mentors: raise Http404 users_know_each_other = Friendship.objects.are_friends(request.user, other_user) print 'Friends:', users_know_each_other allow_private = is_me or request.user.is_staff allow_restricted = is_me or request.user.is_staff or users_know_each_other return render_to_response(template_name, dict({ "invitations_active_on_platform": settings.ALLOW_MENTORING_REQUESTS, "is_me": is_me, "is_friend": is_friend, "is_following": is_following, "other_user": other_user, "allow_private": allow_private, # can see private fields "allow_restricted": allow_restricted, # can see restricted fields "has_mentor": FriendshipInvitation.objects.hasMentor(from_user = request.user), "deny_mentor_request": deny_mentor_request, #can see the 'add as a friend' field "consumed_all_requests": consumed_all_requests, "student": other_user.get_profile().as_student(), "mentor": other_user.get_profile().as_mentor(), "mentor_can_accept": mentor_can_accept, "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, "previous_denied_invitation_to": previous_denied_invitation_to, "confirmed": mail.utils.is_confirmed(other_user) }, **extra_context), context_instance=RequestContext(request))
def profile(request, username, template_name="profiles/profile.html", extra_context=None): if extra_context is None: extra_context = {} other_user = get_object_or_404(User, username=username) if request.user.is_authenticated(): is_friend = Friendship.objects.are_friends(request.user, other_user) is_following = Following.objects.is_following(request.user, other_user) other_friends = Friendship.objects.friends_for_user(other_user) if request.user == other_user: is_me = True else: is_me = False else: other_friends = [] is_friend = False is_me = False is_following = False if is_friend: invite_form = None previous_invitations_to = None previous_invitations_from = None if request.method == "POST": if request.POST.get("action") == "remove": # @@@ perhaps the form should just post to friends and be redirected here Friendship.objects.remove(request.user, other_user) request.user.message_set.create(message=_("You have removed %(from_user)s from friends") % {'from_user': other_user}) is_friend = False invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) else: if request.user.is_authenticated() and request.method == "POST": if request.POST.get("action") == "invite": # @@@ perhaps the form should just post to friends and be redirected here invite_form = InviteFriendForm(request.user, request.POST) if invite_form.is_valid(): invite_form.save() else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) invitation_id = request.POST.get("invitation", None) if request.POST.get("action") == "accept": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.accept() request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user}) is_friend = True other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass elif request.POST.get("action") == "decline": # @@@ perhaps the form should just post to friends and be redirected here try: invitation = FriendshipInvitation.objects.get(id=invitation_id) if invitation.to_user == request.user: invitation.decline() request.user.message_set.create(message=_("You have declined the friendship request from %(from_user)s") % {'from_user': invitation.from_user}) other_friends = Friendship.objects.friends_for_user(other_user) except FriendshipInvitation.DoesNotExist: pass else: invite_form = InviteFriendForm(request.user, { 'to_user': username, 'message': ugettext("Let's be friends!"), }) previous_invitations_to = FriendshipInvitation.objects.invitations(to_user=other_user, from_user=request.user) previous_invitations_from = FriendshipInvitation.objects.invitations(to_user=request.user, from_user=other_user) notice_types = NoticeType.objects.all() notices = Notice.objects.notices_for(request.user, on_site=True) settings_table = [] for notice_type in notice_types: settings_row = [] for medium_id, medium_display in NOTICE_MEDIA: form_label = "%s_%s" % (notice_type.label, medium_id) setting = get_notification_setting(request.user, notice_type, medium_id) if request.method == "POST": if request.POST.get(form_label) == "on": setting.send = True else: setting.send = False setting.save() settings_row.append((form_label, setting.send)) settings_table.append({"notice_type": notice_type, "cells": settings_row}) return render_to_response(template_name, dict({ "notices": notices, "notice_types": notice_types, "is_me": is_me, "is_friend": is_friend, "is_following": is_following, "other_user": other_user, "other_friends": other_friends, "invite_form": invite_form, "previous_invitations_to": previous_invitations_to, "previous_invitations_from": previous_invitations_from, }, **extra_context), context_instance=RequestContext(request))