summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAurélien Bompard <aurelien@bompard.org>2013-08-10 17:47:49 -0400
committerAurélien Bompard <aurelien@bompard.org>2013-08-15 11:11:22 +0000
commit6103bcf05feeddace63fcc5857665d3762e25c8b (patch)
tree8c025f22fa12d9927850d04e628c379e66eb9f91
parent5ad794b6425a08550a7d68f76e201ebfdad13dfc (diff)
downloadhyperkitty-6103bcf05feeddace63fcc5857665d3762e25c8b.tar.gz
hyperkitty-6103bcf05feeddace63fcc5857665d3762e25c8b.tar.xz
hyperkitty-6103bcf05feeddace63fcc5857665d3762e25c8b.zip
Add email and subscriptions to the user profile
-rw-r--r--hyperkitty/lib/mailman.py45
-rw-r--r--hyperkitty/templates/fragments/user_subscriptions.html39
-rw-r--r--hyperkitty/templates/user_profile.html16
-rw-r--r--hyperkitty/templates/user_public_profile.html36
-rw-r--r--hyperkitty/views/accounts.py68
5 files changed, 133 insertions, 71 deletions
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 %}
+ <table class="table table-striped table-bordered table-condensed subscriptions">
+ <thead>
+ <tr>
+ <th>List</th>
+ <th>Time of first activity</th>
+ <th>First post</th>
+ <th>Posts to this list</th>
+ <th>Votes</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for sub in subscriptions %}
+ <tr>
+ <td>
+ <a href="{% url 'list_overview' sub.list_name %}">{{ sub.list_name }}</a>
+ </td>
+ {% if sub.first_post %}
+ <td>
+ {{ sub.first_post|get_date|date:"l, j F Y H:i:s" }}
+ {{ sub.first_post|get_date|timesince }}
+ </td>
+ <td>{{ sub.first_post.subject }}</td>
+ <td><a href="{{ sub.all_posts_url }}">{{ sub.posts_count }} post{{ sub.posts_count|pluralize }}</a></td>
+ <td><span class="likestatus {{ sub.likestatus }}">+{{ sub.likes }}/-{{ sub.dislikes }}</span></td>
+ {% else %}
+ <td colspan="4" style="text-align:center"><em>no post yet</em></td>
+ {% endif %}
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ {% else %}
+ <p><em>no subscriptions</em></p>
+ {% 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 @@
<li><a href="#favorites">Favorites</a></li>
<li><a href="#views">Threads you have read</a></li>
<li><a href="#votes">Votes</a></li>
+ <li><a href="#subscriptions">Subscriptions</a></li>
</ul>
<div id="account">
@@ -37,6 +38,16 @@
<th>{% trans 'Email:' %}</th>
<td>{{ user.email }}</td>
</tr>
+ {% if emails %}
+ <tr>
+ <th>{% trans 'Other emails:' %}</th>
+ <td><ul>
+ {% for email in emails %}
+ <li>{{ email }}</li>
+ {% endfor %}
+ </ul></td>
+ </tr>
+ {% endif %}
{% if use_mockups %}
<tr>
<th>{% trans 'Karma:' %}</th>
@@ -91,6 +102,11 @@
<div class="ajaxcontent"></div>
</div>
+
+ <div id="subscriptions">
+ {% include "fragments/user_subscriptions.html" %}
+ </div>
+
</div>
</div>
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 @@
<div class="clearfix"></div>
<h3>Subscriptions</h3>
- {% if subscriptions %}
- <table class="table table-striped table-bordered table-condensed subscriptions">
- <thead>
- <tr>
- <th>List</th>
- <th>Time of first activity</th>
- <th>First post</th>
- <th>Posts to this list</th>
- <th>Votes</th>
- </tr>
- </thead>
- <tbody>
- {% for sub in subscriptions %}
- <tr>
- <td>
- <a href="{% url 'list_overview' sub.list_name %}">{{ sub.list_name }}</a>
- </td>
- {% if sub.first_post %}
- <td>
- {{ sub.first_post|get_date|date:"l, j F Y H:i:s" }}
- {{ sub.first_post|get_date|timesince }}
- </td>
- <td>{{ sub.first_post.subject }}</td>
- <td><a href="{{ sub.all_posts_url }}">{{ sub.posts_count }} post{{ sub.posts_count|pluralize }}</a></td>
- <td><span class="likestatus {{ sub.likestatus }}">+{{ sub.likes }}/-{{ sub.dislikes }}</span></td>
- {% else %}
- <td colspan="4" style="text-align:center"><em>no post yet</em></td>
- {% endif %}
- </tr>
- {% endfor %}
- </tbody>
- </table>
- {% else %}
- <p><em>no subscriptions</em></p>
- {% endif %}
+ {% include "fragments/user_subscriptions.html" %}
</div>
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"