def newsletter_subscribe(): password = request.form['password'] email = request.form['mail'] if password == app.config["NEWSLETTER_PASSWORD"]: if not validate_email(email, verify=True): app.logger.warn("Email verification failed for '%s'" % email) return redirect(app.config["NEWSLETTER_VERIFY_FAILED_PATH"]) app.logger.info("Subscribing %s" % email) if not app.debug: client = mailmanclient.Client(app.config["MAILMAN_REST_URL"], app.config["MAILMAN_REST_USER"], app.config["MAILMAN_REST_PASS"]) newsletterList = client.get_list( app.config["NEWSLETTER_LIST_NAME"]) try: newsletterList.get_member(email) except ValueError: newsletterList.subscribe(email, pre_approved=True) else: app.logger.warn("Already subscribed: %s", email) return redirect( app.config["NEWSLETTER_ALREADY_SUBSCRIBED_PATH"]) return redirect(app.config["NEWSLETTER_SUCCESS_PATH"]) else: return redirect(app.config["NEWSLETTER_FAIL_PATH"])
def _store_mailman_user(self, address): try: mm_client = mailmanclient.Client( '%s/3.0' % self.settings.MAILMAN_REST_SERVER, self.settings.MAILMAN_API_USER, self.settings.MAILMAN_API_PASS) mm_user = mm_client.get_user(address) except (HTTPError, mailmanclient.MailmanConnectionError), e: if self.debug: print "Can't get the user from Mailman: %s" % e
def public_profile(request, user_id): class FakeMailmanUser(object): display_name = None created_on = None addresses = [] subscription_list_ids = [] store = get_store(request) try: client = mailmanclient.Client('%s/3.0' % settings.MAILMAN_REST_SERVER, settings.MAILMAN_API_USER, settings.MAILMAN_API_PASS) mm_user = client.get_user(user_id) except HTTPError: raise Http404("No user with this ID: %s" % user_id) except mailmanclient.MailmanConnectionError: db_user = store.get_user(user_id) if db_user is None: return HttpResponse("Can't connect to Mailman", content_type="text/plain", status=500) mm_user = FakeMailmanUser() mm_user.display_name = list(db_user.senders)[0].name mm_user.addresses = db_user.addresses fullname = mm_user.display_name if not fullname: fullname = store.get_sender_name(user_id) # Subscriptions 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" if likes - dislikes >= 10: likestatus = "likealot" elif likes - dislikes > 0: likestatus = "like" try: email = unicode(mm_user.addresses[0]) except KeyError: email = None if mm_user.created_on is not None: creation = dateutil.parser.parse(mm_user.created_on) else: creation = None context = { "fullname": fullname, "mm_user": mm_user, "email": email, "creation": creation, "subscriptions": subscriptions, "posts_count": sum([s["posts_count"] for s in subscriptions]), "likes": likes, "dislikes": dislikes, "likestatus": likestatus, } return render(request, "user_public_profile.html", context)
def posts(request, user_id): store = get_store(request) mlist_fqdn = request.GET.get("list") if mlist_fqdn is None: mlist = None return HttpResponse("Not implemented yet", status=500) else: mlist = store.get_list(mlist_fqdn) if mlist is None: raise Http404("No archived mailing-list by that name.") if not is_mlist_authorized(request, mlist): return render(request, "errors/private.html", { "mlist": mlist, }, status=403) # Get the user's full name try: client = mailmanclient.Client('%s/3.0' % settings.MAILMAN_REST_SERVER, settings.MAILMAN_API_USER, settings.MAILMAN_API_PASS) mm_user = client.get_user(user_id) except HTTPError: raise Http404("No user with this ID: %s" % user_id) except mailmanclient.MailmanConnectionError: fullname = None else: fullname = mm_user.display_name if not fullname: fullname = store.get_sender_name(user_id) # Get the messages and paginate them messages = store.get_messages_by_user_id(user_id, mlist_fqdn) try: page_num = int(request.GET.get('page', "1")) except ValueError: page_num = 1 messages = paginate(messages, page_num) for message in messages: message.myvote = message.get_vote_by_user_id( request.session.get("user_id")) context = { 'user_id': user_id, 'mlist' : mlist, 'messages': messages, 'fullname': fullname, } return render(request, "user_posts.html", context)
def subscriptions(request): store = get_store(request) # 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 # Subscriptions subscriptions = get_subscriptions(store, mm_client, mm_user) return render(request, 'fragments/user_subscriptions.html', { "subscriptions": subscriptions, })
def __init__(self, hash_storage_cls=None): """Creates a new connection to Mailman's REST API. Can be provided with a subclass of tenca.HashStorage to lookup scrambled hashes, identifying a mailing list in the invite links. If hash_storage_cls is None, the class specified in settings.HASH_STORAGE_CLASS will be used. """ self.client = mailmanclient.Client(BASE_URL(), settings.ADMIN_USER, settings.ADMIN_PASS) domains = self.client.domains assert len(domains), 1 self.domain = domains[0] if hash_storage_cls is None: hash_storage_cls = pipelines.get_func(settings.HASH_STORAGE_CLASS) assert hash_storage_cls is not None self.hash_storage = hash_storage_cls(self)
def user_profile(request): if not request.user.is_authenticated(): return redirect('user_login') store = get_store(request) # try to render the user profile. try: user_profile = request.user.get_profile() 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(): request.user.first_name = form.cleaned_data["first_name"] request.user.last_name = form.cleaned_data["last_name"] 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) else: form = UserProfileForm(initial={ "first_name": request.user.first_name, "last_name": request.user.last_name, "timezone": get_current_timezone(), }) # Favorites try: favorites = Favorite.objects.filter(user=request.user) except Favorite.DoesNotExist: favorites = [] for fav in favorites: thread = store.get_thread(fav.list_address, fav.threadid) fav.thread = thread if thread is 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) # Flash messages flash_messages = [] flash_msg = request.GET.get("msg") if flash_msg: flash_msg = { "type": FLASH_MESSAGES[flash_msg][0], "msg": FLASH_MESSAGES[flash_msg][1] } flash_messages.append(flash_msg) # Extract the gravatar_url used by django_gravatar2. The site # administrator could alternatively set this to http://cdn.libravatar.org/ gravatar_url = getattr(settings, 'GRAVATAR_URL', 'http://www.gravatar.com') gravatar_shortname = '.'.join(gravatar_url.split('.')[-2:]).strip('/') context = { 'user_profile' : user_profile, 'form': form, 'emails': emails, 'favorites': favorites, 'flash_messages': flash_messages, 'gravatar_url': gravatar_url, 'gravatar_shortname': gravatar_shortname, } return render(request, "user_profile.html", context)
def public_profile(request, user_id): try: client = mailmanclient.Client('%s/3.0' % settings.MAILMAN_REST_SERVER, settings.MAILMAN_API_USER, settings.MAILMAN_API_PASS) mm_user = client.get_user(user_id) except HTTPError: raise Http404("No user with this ID: %s" % user_id) except mailmanclient.MailmanConnectionError: return HttpResponse("Can't connect to Mailman", content_type="text/plain", status=500) fullname = mm_user.display_name if not fullname: fullname = store.get_sender_name(user_id) subscriptions = [] store = get_store(request) # 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), }) likes = sum([s["likes"] for s in subscriptions]) dislikes = sum([s["dislikes"] for s in subscriptions]) likestatus = "neutral" if likes - dislikes >= 10: likestatus = "likealot" elif likes - dislikes > 0: likestatus = "like" try: email = unicode(mm_user.addresses[0]) except KeyError: email = None context = { "fullname": fullname, "mm_user": mm_user, "email": email, "creation": dateutil.parser.parse(mm_user.created_on), "subscriptions": subscriptions, "posts_count": sum([s["posts_count"] for s in subscriptions]), "likes": likes, "dislikes": dislikes, "likestatus": likestatus, } return render(request, "user_public_profile.html", context)