Skip to content
This repository has been archived by the owner on Jul 24, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2 from fedora-infra/feature/comma-delimited-detai…
Browse files Browse the repository at this point in the history
…l-value

Feature/comma delimited detail value
  • Loading branch information
ralphbean committed Jan 20, 2014
2 parents aa6f56d + 940a098 commit 1d434f2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
27 changes: 26 additions & 1 deletion fmn/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import inspect
import logging
import re

import bs4
import docutils.examples
Expand All @@ -16,6 +17,10 @@

log = logging.getLogger(__name__)

irc_regex = r'[a-zA-Z_\-\[\]\\^{}|`][a-zA-Z0-9_\-\[\]\\^{}|`]*'
email_regex = r'^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$'
gcm_regex = r'^[\w-]+$'


def recipients(session, config, valid_paths, message):
""" The main API function.
Expand Down Expand Up @@ -58,7 +63,7 @@ def load_rules(root='fmn.rules'):
obj = getattr(module, name)
if not callable(obj):
continue
log.info("Found rule %r %r" % (name, obj))
log.debug("Found rule %r %r" % (name, obj))

doc = inspect.getdoc(obj)

Expand All @@ -67,6 +72,10 @@ def load_rules(root='fmn.rules'):
doc = doc.decode('utf-8')

if doc:
# If we have a docstring, then mark it up beautifully for display
# in the web app.
# FWIW, this should probably be moved into fmn.web since nowhere
# else are we going to want HTML... we'll still want raw .rst.
title, doc_as_rst = doc.split('\n', 1)
doc = docutils.examples.html_parts(doc_as_rst)['body']
soup = bs4.BeautifulSoup(doc)
Expand Down Expand Up @@ -102,3 +111,19 @@ def strip_anchor_tags(soup):
yield tag.string
else:
yield tag


def validate_detail_value(ctx, value):
if ctx.name == 'irc':
if re.match(irc_regex, value) is None:
raise ValueError("value must be a valid irc nick")
elif ctx.name == 'email':
if re.match(email_regex, value) is None:
raise ValueError("value must be an email address")
elif ctx.name == 'gcm':
if re.match(gcm_regex, value) is None:
raise ValueError("not a valid android registration id")
else:
raise NotImplementedError("No validation scheme for %r" % ctx.name)
# Happy.
return
14 changes: 10 additions & 4 deletions fmn/lib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ def _recipients(self, session, config, valid_paths, message):
""" Returns the list of recipients for a message. """
for user in User.all(session):
pref = Preference.load(session, user, self)
if pref:
if pref and pref.detail_value:
filter = pref.prefers(session, config, valid_paths, message)
if filter:
yield {
'user': user.openid,
pref.context.detail_name: pref.detail_value,
pref.context.detail_name: pref.detail_value.split(','),
'filter': filter.name,
}

Expand Down Expand Up @@ -465,7 +465,13 @@ def load(cls, session, user, context):
.first()

def update_details(self, session, value):
self.detail_value = value
value = value.strip()
if self.detail_value:
tokens = self.detail_value.split(',')
if value not in tokens:
self.detail_value = ','.join(tokens + [value])
else:
self.detail_value = value
session.flush()
session.commit()

Expand Down Expand Up @@ -632,7 +638,7 @@ def set_status(self, session, status):
# Propagate back to the Preference if everything is good.
if self.status == 'accepted':
pref = Preference.load(session, self.openid, self.context_name)
pref.detail_value = self.detail_value
pref.update_details(session, self.detail_value)

session.flush()
session.commit()
Expand Down
59 changes: 59 additions & 0 deletions fmn/lib/tests/test_confirmations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from nose.tools import eq_, assert_not_equals

import os
import fmn.lib.models
import fmn.lib.tests


class TestConfirmations(fmn.lib.tests.Base):
def create_user_and_context_data(self):
user1 = fmn.lib.models.User.get_or_create(
self.sess, openid="ralph.id.fedoraproject.org",
openid_url="http://ralph.id.fedoraproject.org/",
)
context1 = fmn.lib.models.Context.create(
self.sess, name="irc", description="Internet Relay Chat",
detail_name="irc nick", icon="user",
)

def create_preference_data_empty(self):
user = fmn.lib.models.User.get(
self.sess, openid="ralph.id.fedoraproject.org")
context = fmn.lib.models.Context.get(self.sess, name="irc")
preference = fmn.lib.models.Preference.create(
self.sess,
user=user,
context=context,
detail_value=None,
)

def test_updating_details(self):
self.create_user_and_context_data()
self.create_preference_data_empty()
user = fmn.lib.models.User.get(
self.sess, openid="ralph.id.fedoraproject.org")
context = fmn.lib.models.Context.get(self.sess, name="irc")
preference = fmn.lib.models.Preference.load(self.sess, user, context)
preference.update_details(self.sess, 'wat')
eq_(preference.detail_value, 'wat')
preference.update_details(self.sess, 'wat2')
eq_(preference.detail_value, 'wat,wat2')
preference.update_details(self.sess, 'wat')
eq_(preference.detail_value, 'wat,wat2')

def test_confirmation(self):
self.create_user_and_context_data()
self.create_preference_data_empty()
user = fmn.lib.models.User.get(
self.sess, openid="ralph.id.fedoraproject.org")
context = fmn.lib.models.Context.get(self.sess, name="irc")
preference = fmn.lib.models.Preference.load(self.sess, user, context)
confirmation = fmn.lib.models.Confirmation.create(
self.sess,
user,
context,
detail_value='awesome',
)
eq_(preference.detail_value, None)
confirmation.set_status(self.sess, 'accepted')
eq_(preference.detail_value, 'awesome')
10 changes: 5 additions & 5 deletions fmn/lib/tests/test_recipients.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create_preference_data_empty(self):
self.sess,
user=user,
context=context,
detail_value="threebean",
detail_value="threebean,threebean2",
)

def create_preference_data_basic(self, code_path):
Expand Down Expand Up @@ -79,10 +79,10 @@ def test_basic_recipients_list(self):
recipients = fmn.lib.recipients_for_context(
self.sess, self.config, self.valid_paths, 'irc', msg)
eq_(list(recipients), [{
'irc nick': 'threebean',
'irc nick': ['threebean', 'threebean2'],
'user': 'ralph.id.fedoraproject.org',
'filter': 'test filter',
}])
}])

def test_miss_recipients_list(self):
self.create_user_and_context_data()
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_multiple_identical_filters_hit(self):
recipients = fmn.lib.recipients_for_context(
self.sess, self.config, self.valid_paths, 'irc', msg)
eq_(list(recipients), [{
'irc nick': 'threebean',
'irc nick': ['threebean', 'threebean2'],
'user': 'ralph.id.fedoraproject.org',
'filter': 'test filter',
}])
Expand All @@ -164,7 +164,7 @@ def test_multiple_different_filters_hit(self):
recipients = fmn.lib.recipients_for_context(
self.sess, self.config, self.valid_paths, 'irc', msg)
eq_(list(recipients), [{
'irc nick': 'threebean',
'irc nick': ['threebean', 'threebean2'],
'user': 'ralph.id.fedoraproject.org',
'filter': 'test filter',
}])

0 comments on commit 1d434f2

Please sign in to comment.