Skip to content

Commit

Permalink
Merge branch 'master' of portal.softwarepublico.gov.br:softwarepublic…
Browse files Browse the repository at this point in the history
…o/colab
  • Loading branch information
seocam committed Aug 27, 2015
2 parents 0d143d6 + a3ff3d5 commit 5046caa
Show file tree
Hide file tree
Showing 18 changed files with 259 additions and 119 deletions.
5 changes: 3 additions & 2 deletions colab/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ class Meta:

class ListsForm(forms.Form):
LISTS_NAMES = ((
listname, u'{} ({})'.format(listname, description)
) for listname, description in mailman.all_lists(description=True))
mlist.get('listname'), u'{} ({})'.format(mlist.get('listname'),
mlist.get('description'))
) for mlist in mailman.all_lists())

lists = forms.MultipleChoiceField(label=_(u'Mailing lists'),
required=False,
Expand Down
2 changes: 1 addition & 1 deletion colab/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def mailinglists(self):
return mailman.user_lists(self)

def update_subscription(self, email, lists):
mailman.update_subscription(email, lists)
return mailman.update_subscription(email, lists)

def save(self, *args, **kwargs):

Expand Down
1 change: 0 additions & 1 deletion colab/accounts/templates/accounts/user_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ <h1>
{% endif %}
{% endif %}
</ul>

{% if user_.mailinglists %}
<b>{% trans 'Groups: ' %}</b>
{% for list in user_.mailinglists %}
Expand Down
95 changes: 69 additions & 26 deletions colab/accounts/utils/mailman.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,99 @@
import logging

from django.conf import settings
from django.contrib import messages

TIMEOUT = 1

LOGGER = logging.getLogger('colab.mailman')


def get_url(listname=None):
S = 'success'
I = 'info'
E = 'error'

MAILMAN_MSGS = {
0: (S, '%s: Success!'),
1: (S, '%s: An email confirmation was sent to you, please check your inbox.'),
2: (I, '%s: Your subscription was sent successfully! Please wait for the list\'s admin approval.'),
3: (I, '%s: You are already a member of this list.'),
4: (E, '%s: You are banned from this list!'),
5: (E, '%s: You appear to have an invalid email address.'),
6: (E, '%s: Your email address is considered to be hostile.'),
7: (E, '%s: You are not a member of this list.'),
8: (E, 'Missing information: `email_from`, `subject` and `body` are mandatory.'),
}


def get_url(path, listname=None):
url = urlparse.urljoin(settings.MAILMAN_API_URL, path)
if listname:
return urlparse.urljoin(settings.MAILMAN_API_URL, '/' + listname)

return settings.MAILMAN_API_URL
return urlparse.urljoin(url, listname)
return url


def subscribe(listname, address):
url = get_url(listname)
url = get_url('subscribe/', listname=listname)
try:
requests.put(url, timeout=TIMEOUT, data={'address': address})
result = requests.put(url, timeout=TIMEOUT, data={'address': address})
msg_type, message = MAILMAN_MSGS[result.json()]
return msg_type, message % listname
except:
LOGGER.exception('Unable to subscribe user')
return False
return True
return E, 'Error: Unable to subscribe user'


def unsubscribe(listname, address):
url = get_url(listname)
url = get_url('subscribe/', listname)
try:
requests.delete(url, timeout=TIMEOUT, data={'address': address})
result = requests.delete(url, timeout=TIMEOUT, data={'address': address})
msg_type, message = MAILMAN_MSGS[result.json()]
return msg_type, message % listname
except:
LOGGER.exception('Unable to unsubscribe user')
return False
return True
return E, 'Error: Unable to subscribe user'


def update_subscription(address, lists):
current_lists = mailing_lists(address=address)
current_lists = mailing_lists(address=address, names_only=True)
info_messages = []

for maillist in current_lists:
if maillist not in lists:
unsubscribe(maillist, address)
info_messages.append(unsubscribe(maillist, address))

for maillist in lists:
if maillist not in current_lists:
subscribe(maillist, address)
info_messages.append(subscribe(maillist, address))

return info_messages


def mailing_lists(**kwargs):
url = get_url()
url = get_url('lists/')

try:
lists = requests.get(url, timeout=TIMEOUT, params=kwargs)
lists = requests.get(url, timeout=TIMEOUT, params=kwargs).json()
if not isinstance(lists, (list, tuple)):
raise
except:
LOGGER.exception('Unable to list mailing lists')
return []

return lists.json()
if kwargs.get('names_only'):
names_only = []
for l in lists:
names_only.append(l['listname'])
return names_only
else:
return lists


def is_private_list(name):
try:
return dict(all_lists(private=True))[name]
privacy = {}
privacy.update({mlist.get('listname'): mlist.get('archive_private')
for mlist in all_lists()})
return privacy[name]
except KeyError:
return []

Expand All @@ -76,22 +109,25 @@ def user_lists(user):
list_set = set()

for email in user.emails.values_list('address', flat=True):
list_set.update(mailing_lists(address=email))
mlists = mailing_lists(address=email)
list_set.update(mlist.get('listname') for mlist in mlists)

return tuple(list_set)


def get_list_description(listname, lists=None):
if not lists:
lists = dict(all_lists(description=True))
elif not isinstance(lists, dict):
lists = dict(lists)
lists = all_lists()

desc = "".join(mlist.get('description') for mlist in lists
if isinstance(mlist, dict) and
mlist.get('listname') == listname)

return lists.get(listname)
return desc


def list_users(listname):
url = get_url(listname)
url = get_url('members/', listname)

params = {}

Expand All @@ -114,3 +150,10 @@ def get_user_mailinglists(user):
lists_for_user.extend(mailing_lists(address=email))

return lists_for_user


def extract_listname_from_list(lists):
try:
return [mlist.get('listname') for mlist in lists]
except ValueError:
return []
24 changes: 17 additions & 7 deletions colab/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ def signup(request):

user.is_active = False
user.save()
EmailAddressValidation.create(user.email, user)
email = EmailAddressValidation.create(user.email, user)

location = reverse('archive_email_view',
kwargs={'key': email.validation_key})
verification_url = request.build_absolute_uri(location)
EmailAddressValidation.verify_email(email, verification_url)

# Check if the user's email have been used previously
# in the mainling lists to link the user to old messages
Expand Down Expand Up @@ -139,7 +144,11 @@ def post(self, request, *args, **kwargs):
user = self.get_object()
for email in user.emails.values_list('address', flat=True):
lists = self.request.POST.getlist(email)
user.update_subscription(email, lists)
info_messages = user.update_subscription(email, lists)
for msg_type, message in info_messages:
show_message = getattr(messages, msg_type)
show_message(request, _(message))


return redirect('user_profile', username=user.username)

Expand All @@ -149,18 +158,19 @@ def get_context_data(self, **kwargs):

user = self.get_object()
emails = user.emails.values_list('address', flat=True)
all_lists = mailman.all_lists(description=True)
all_lists = mailman.all_lists()

for email in emails:
lists = []
lists_for_address = mailman.mailing_lists(address=email)
for listname, description in all_lists:
if listname in lists_for_address:
lists_for_address = mailman.mailing_lists(address=email, names_only=True)
for mlist in all_lists:
if mlist.get('listname') in lists_for_address:
checked = True
else:
checked = False
lists.append((
{'listname': listname, 'description': description},
{'listname': mlist.get('listname'),
'description': mlist.get('description')},
checked
))

Expand Down
3 changes: 2 additions & 1 deletion colab/home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

def get_user_threads(threads, lists_for_user, key):
visible_threads = []
listnames_for_user = mailman.extract_listname_from_list(lists_for_user)
for t in threads:
if not t.mailinglist.is_private or \
t.mailinglist.name in lists_for_user:
t.mailinglist.name in listnames_for_user:
visible_threads.append(key(t))

return visible_threads
Expand Down
24 changes: 24 additions & 0 deletions colab/plugins/gitlab/migrations/0005_auto_20150806_1230.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('gitlab', '0004_auto_20150630_1149'),
]

operations = [
migrations.RemoveField(
model_name='gitlabcomment',
name='iid',
),
migrations.AddField(
model_name='gitlabissue',
name='iid',
field=models.IntegerField(null=True),
preserve_default=True,
),
]
6 changes: 3 additions & 3 deletions colab/plugins/gitlab/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Meta:
class GitlabIssue(Collaboration):

id = models.IntegerField(primary_key=True)
iid = models.IntegerField(null=True)
project = models.ForeignKey(GitlabProject, null=True,
on_delete=models.SET_NULL)
title = models.TextField()
Expand All @@ -82,7 +83,7 @@ def modified(self):
@property
def url(self):
return u'/gitlab/{}/issues/{}'.format(
self.project.path_with_namespace, self.id)
self.project.path_with_namespace, self.iid)

class Meta:
verbose_name = _('Gitlab Issue')
Expand All @@ -92,7 +93,6 @@ class Meta:
class GitlabComment(Collaboration):

id = models.IntegerField(primary_key=True)
iid = models.IntegerField(null=True)
body = models.TextField()
created_at = models.DateTimeField(blank=True, null=True)
issue_comment = models.BooleanField(default=True)
Expand Down Expand Up @@ -139,7 +139,7 @@ def url(self):
url_str = u'/gitlab/{}/merge_requests/{}#notes_{}'

return url_str.format(self.project.path_with_namespace,
self.parent_id, self.iid)
self.parent_id, self.id)

class Meta:
verbose_name = _('Gitlab Comments')
Expand Down

0 comments on commit 5046caa

Please sign in to comment.