From 6103bcf05feeddace63fcc5857665d3762e25c8b Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Sat, 10 Aug 2013 17:47:49 -0400 Subject: Add email and subscriptions to the user profile --- hyperkitty/lib/mailman.py | 45 ++++++++++++++ .../templates/fragments/user_subscriptions.html | 39 +++++++++++++ hyperkitty/templates/user_profile.html | 16 +++++ hyperkitty/templates/user_public_profile.html | 36 +----------- hyperkitty/views/accounts.py | 68 ++++++++++------------ 5 files changed, 133 insertions(+), 71 deletions(-) create mode 100644 hyperkitty/templates/fragments/user_subscriptions.html diff --git a/hyperkitty/lib/mailman.py b/hyperkitty/lib/mailman.py index 185f9b1..f0c970f 100644 --- a/hyperkitty/lib/mailman.py +++ b/hyperkitty/lib/mailman.py @@ -20,8 +20,12 @@ # from django.conf import settings +from django.core.urlresolvers import reverse +from django.utils.http import urlquote from mailmanclient import Client +from hyperkitty.models import Rating + def subscribe(list_address, user): client = Client('%s/3.0' % settings.MAILMAN_REST_SERVER, @@ -35,3 +39,44 @@ def subscribe(list_address, user): "%s %s" % (user.first_name, user.last_name)) member.preferences["delivery_status"] = "by_user" member.preferences.save() + + +def get_subscriptions(store, client, mm_user): + if not mm_user: + return [] + subscriptions = [] + for mlist_id in mm_user.subscription_list_ids: + mlist = client.get_list(mlist_id).fqdn_listname + # de-duplicate subscriptions + if mlist in [ s["list_name"] for s in subscriptions ]: + continue + email_hashes = store.get_message_hashes_by_user_id( + mm_user.user_id, mlist) + try: # Compute the average vote value + votes = Rating.objects.filter(list_address=mlist, + messageid__in=email_hashes) + except Rating.DoesNotExist: + votes = [] + likes = dislikes = 0 + for v in votes: + if v.vote == 1: + likes += 1 + elif v.vote == -1: + dislikes += 1 + all_posts_url = "%s?list=%s&query=user_id:%s" % \ + (reverse("search"), mlist, urlquote(mm_user.user_id)) + likestatus = "neutral" + if likes - dislikes >= 10: + likestatus = "likealot" + elif likes - dislikes > 0: + likestatus = "like" + subscriptions.append({ + "list_name": mlist, + "first_post": store.get_first_post(mlist, mm_user.user_id), + "likes": likes, + "dislikes": dislikes, + "likestatus": likestatus, + "all_posts_url": all_posts_url, + "posts_count": len(email_hashes), + }) + return subscriptions diff --git a/hyperkitty/templates/fragments/user_subscriptions.html b/hyperkitty/templates/fragments/user_subscriptions.html new file mode 100644 index 0000000..ef661c2 --- /dev/null +++ b/hyperkitty/templates/fragments/user_subscriptions.html @@ -0,0 +1,39 @@ +{% load url from future %} +{% load hk_generic %} + + {% if subscriptions %} + + + + + + + + + + + + {% for sub in subscriptions %} + + + {% if sub.first_post %} + + + + + {% else %} + + {% endif %} + + {% endfor %} + +
ListTime of first activityFirst postPosts to this listVotes
+ {{ sub.list_name }} + + {{ sub.first_post|get_date|date:"l, j F Y H:i:s" }} + {{ sub.first_post|get_date|timesince }} + {{ sub.first_post.subject }}{{ sub.posts_count }} post{{ sub.posts_count|pluralize }}+{{ sub.likes }}/-{{ sub.dislikes }}no post yet
+ {% else %} +

no subscriptions

+ {% endif %} + diff --git a/hyperkitty/templates/user_profile.html b/hyperkitty/templates/user_profile.html index c7d7550..1d25499 100644 --- a/hyperkitty/templates/user_profile.html +++ b/hyperkitty/templates/user_profile.html @@ -21,6 +21,7 @@
  • Favorites
  • Threads you have read
  • Votes
  • +
  • Subscriptions
  • @@ -37,6 +38,16 @@ {% trans 'Email:' %} {{ user.email }} + {% if emails %} + + {% trans 'Other emails:' %} + + + {% endif %} {% if use_mockups %} {% trans 'Karma:' %} @@ -91,6 +102,11 @@
    + +
    + {% include "fragments/user_subscriptions.html" %} +
    + diff --git a/hyperkitty/templates/user_public_profile.html b/hyperkitty/templates/user_public_profile.html index 4172969..d949270 100644 --- a/hyperkitty/templates/user_public_profile.html +++ b/hyperkitty/templates/user_public_profile.html @@ -41,41 +41,7 @@

    Subscriptions

    - {% if subscriptions %} - - - - - - - - - - - - {% for sub in subscriptions %} - - - {% if sub.first_post %} - - - - - {% else %} - - {% endif %} - - {% endfor %} - -
    ListTime of first activityFirst postPosts to this listVotes
    - {{ sub.list_name }} - - {{ sub.first_post|get_date|date:"l, j F Y H:i:s" }} - {{ sub.first_post|get_date|timesince }} - {{ sub.first_post.subject }}{{ sub.posts_count }} post{{ sub.posts_count|pluralize }}+{{ sub.likes }}/-{{ sub.dislikes }}no post yet
    - {% else %} -

    no subscriptions

    - {% endif %} + {% include "fragments/user_subscriptions.html" %} diff --git a/hyperkitty/views/accounts.py b/hyperkitty/views/accounts.py index 7a18ca9..b4c8759 100644 --- a/hyperkitty/views/accounts.py +++ b/hyperkitty/views/accounts.py @@ -30,7 +30,7 @@ from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from django.contrib.auth.views import login as django_login_view from django.shortcuts import render, redirect -from django.utils.http import is_safe_url, urlquote +from django.utils.http import is_safe_url from django.utils.timezone import utc, get_current_timezone from django.http import Http404, HttpResponse #from django.utils.translation import gettext as _ @@ -42,6 +42,7 @@ from hyperkitty.models import UserProfile, Rating, Favorite, LastView from hyperkitty.views.forms import RegistrationForm, UserProfileForm from hyperkitty.lib import get_store from hyperkitty.lib.view_helpers import FLASH_MESSAGES, paginate +from hyperkitty.lib.mailman import get_subscriptions logger = logging.getLogger(__name__) @@ -62,7 +63,7 @@ def login_view(request, *args, **kwargs): @login_required -def user_profile(request, user_email=None): +def user_profile(request): if not request.user.is_authenticated(): return redirect('user_login') @@ -74,6 +75,16 @@ def user_profile(request, user_email=None): except ObjectDoesNotExist: user_profile = UserProfile.objects.create(user=request.user) + # get the Mailman user + try: + mm_client = mailmanclient.Client('%s/3.0' % + settings.MAILMAN_REST_SERVER, + settings.MAILMAN_API_USER, + settings.MAILMAN_API_PASS) + mm_user = mm_client.get_user(request.user.email) + except (HTTPError, mailmanclient.MailmanConnectionError): + mm_client = mm_user = None + if request.method == 'POST': form = UserProfileForm(request.POST) if form.is_valid(): @@ -82,6 +93,11 @@ def user_profile(request, user_email=None): user_profile.timezone = form.cleaned_data["timezone"] request.user.save() user_profile.save() + # Now update the display name in Mailman + if mm_user is not None: + mm_user.display_name = "%s %s" % ( + request.user.first_name, request.user.last_name) + mm_user.save() redirect_url = reverse('user_profile') redirect_url += "?msg=updated-ok" return redirect(redirect_url) @@ -104,6 +120,17 @@ def user_profile(request, user_email=None): fav.delete() # thread has gone away? favorites = [ f for f in favorites if f.thread is not None ] + # Emails + emails = [] + if mm_user is not None: + for addr in mm_user.addresses: + addr = unicode(addr) + if addr != request.user.email: + emails.append(addr) + + # Subscriptions + subscriptions = get_subscriptions(store, mm_client, mm_user) + # Flash messages flash_messages = [] flash_msg = request.GET.get("msg") @@ -115,7 +142,9 @@ def user_profile(request, user_email=None): context = { 'user_profile' : user_profile, 'form': form, + 'emails': emails, 'favorites': favorites, + 'subscriptions': subscriptions, 'flash_messages': flash_messages, } return render(request, "user_profile.html", context) @@ -225,41 +254,8 @@ def public_profile(request, user_id): fullname = mm_user.display_name if not fullname: fullname = store.get_sender_name(user_id) - subscriptions = [] # Subscriptions - for mlist_id in mm_user.subscription_list_ids: - mlist = client.get_list(mlist_id).fqdn_listname - # de-duplicate subscriptions - if mlist in [ s["list_name"] for s in subscriptions ]: - continue - email_hashes = store.get_message_hashes_by_user_id(user_id, mlist) - try: # Compute the average vote value - votes = Rating.objects.filter(list_address=mlist, - messageid__in=email_hashes) - except Rating.DoesNotExist: - votes = [] - likes = dislikes = 0 - for v in votes: - if v.vote == 1: - likes += 1 - elif v.vote == -1: - dislikes += 1 - all_posts_url = "%s?list=%s&query=user_id:%s" % \ - (reverse("search"), mlist, urlquote(user_id)) - likestatus = "neutral" - if likes - dislikes >= 10: - likestatus = "likealot" - elif likes - dislikes > 0: - likestatus = "like" - subscriptions.append({ - "list_name": mlist, - "first_post": store.get_first_post(mlist, user_id), - "likes": likes, - "dislikes": dislikes, - "likestatus": likestatus, - "all_posts_url": all_posts_url, - "posts_count": len(email_hashes), - }) + subscriptions = get_subscriptions(store, client, mm_user) likes = sum([s["likes"] for s in subscriptions]) dislikes = sum([s["dislikes"] for s in subscriptions]) likestatus = "neutral" -- cgit