Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
tidy up some issues highlighted by quantifiedcode
Browse files Browse the repository at this point in the history
  • Loading branch information
monty5811 committed May 3, 2016
1 parent 1a77384 commit e629d48
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 8 deletions.
8 changes: 8 additions & 0 deletions api/drf_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,47 @@ class CanSeeGroups(permissions.BasePermission):
"""Check if a user should see Groups."""

def has_permission(self, request, view):
"""Check permission."""
return request.user.profile.can_see_groups


class CanSeeContactNames(permissions.BasePermission):
"""Check if a user should see names of Contacts."""

def has_permission(self, request, view):
"""Check permission."""
return request.user.profile.can_see_contact_names


class CanSeeKeywords(permissions.BasePermission):
"""Check if a user should have access to keywords."""

def has_permission(self, request, view):
"""Check permission."""
return request.user.profile.can_see_keywords


class CanSeeOutgoing(permissions.BasePermission):
"""Check if a user should have access to outgoing log."""

def has_permission(self, request, view):
"""Check permission."""
return request.user.profile.can_see_outgoing


class CanSeeIncoming(permissions.BasePermission):
"""Check if a user should have access to incoming log."""

def has_permission(self, request, view):
"""Check permission."""
return request.user.profile.can_see_incoming


class CanSeeKeyword(permissions.BasePermission):
"""Check if a user should have access to a single keyword."""

def has_object_permission(self, request, view, obj):
"""Check permission."""
if not obj.is_locked:
return True

Expand All @@ -50,11 +56,13 @@ class CanImport(permissions.BasePermission):
"""Check if a user can import."""

def has_permission(self, request, view):
"""Check permission."""
return request.user.is_staff or request.user.profile.can_import


class IsStaff(permissions.BasePermission):
"""Check if a user has staff privileges."""

def has_permission(self, request, view):
"""Check permission."""
return request.user.is_staff or request.user.is_superuser
4 changes: 4 additions & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,13 @@ def _split_args(sched_args):
return ast.literal_eval(sched_args)

def get_message_body(self, obj):
"""Fetch the message body arg."""
if obj.args is None:
return None
return self._split_args(obj.args)[1]

def get_recipient(self, obj):
"""Fetch the recipient arg and serialize."""
if obj.args is None:
return None
pk = self._split_args(obj.args)[0]
Expand All @@ -202,6 +204,7 @@ def get_recipient(self, obj):
return {'url': '#', 'full_name': 'n/a'}

def get_recipient_group(self, obj):
"""Fetch the recipient group arg and serialize."""
if obj.args is None:
return None
grp_name = self._split_args(obj.args)[2]
Expand All @@ -213,6 +216,7 @@ def get_recipient_group(self, obj):
return {'url': '#', 'name': 'n/a'}

def get_queued_by(self, obj):
"""Fetch the queued by arg."""
if obj.args is None:
return None
return self._split_args(obj.args)[3]
Expand Down
5 changes: 5 additions & 0 deletions apostello/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@


class ApostelloAccountAdapter(DefaultAccountAdapter):
"""Custom dapter for django-allauth.
Overrides `get_from_email` to load from db then fall back to settings.
"""

def get_from_email(self):
"""Override to use SiteConfiguration, then fall back to settings."""
from site_config.models import SiteConfiguration
Expand Down
2 changes: 2 additions & 0 deletions apostello/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SendAdhocRecipientsForm(forms.Form):
)

def clean(self):
"""Override clean method to check SMS cost limit."""
cleaned_data = super(SendAdhocRecipientsForm, self).clean()
if 'recipients' in cleaned_data and 'content' in cleaned_data:
# if we have no recipients, we don't need to check cost limit
Expand Down Expand Up @@ -82,6 +83,7 @@ class SendRecipientGroupForm(forms.Form):
)

def clean(self):
"""Override clean method to check SMS cost limit."""
cleaned_data = super(SendRecipientGroupForm, self).clean()
if 'recipient_group' in cleaned_data and 'content' in cleaned_data:
# if we have no recipient group, we don't need to check cost limit
Expand Down
1 change: 1 addition & 0 deletions apostello/management/commands/import_incoming_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class Command(BaseCommand):
help = 'Import incoming messages from twilio'

def handle(self, *args, **options):
"""Handle the command."""
check_incoming_log(fetch_all=True)
1 change: 1 addition & 0 deletions apostello/management/commands/import_outgoing_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class Command(BaseCommand):
help = 'Import outgoing messages from twilio'

def handle(self, *args, **options):
"""Handle the command."""
check_outgoing_log(fetch_all=True)
2 changes: 2 additions & 0 deletions apostello/management/commands/setup_periodic_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


def add_day_if_req(dt):
"""Check to see if the time has passsed already and adds a day if so."""
if dt > datetime.datetime.now():
return dt

Expand All @@ -21,6 +22,7 @@ class Command(BaseCommand):
help = 'Import incoming messages from twilio'

def handle(self, *args, **options):
"""Handle the command."""
now = datetime.datetime.now()
now = now.replace(minute=0, second=0)
next_3am = add_day_if_req(now.replace(hour=3))
Expand Down
1 change: 1 addition & 0 deletions apostello/management/commands/update_sms_name_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class Command(BaseCommand):
help = 'Update from_name fields'

def handle(self, *args, **options):
"""Handle the command."""
for c in Recipient.objects.all():
update_msgs_name(c.id)
3 changes: 3 additions & 0 deletions apostello/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def archive(self):
self.save()

def check_user_cost_limit(self, limit, msg):
"""Check the user has not exceeded their per SMS cost limit."""
num_sms = ceil(len(msg) / 160)
if limit == 0:
return
Expand Down Expand Up @@ -178,6 +179,7 @@ def archive(self):

@staticmethod
def check_user_cost_limit(recipients, limit, msg):
"""Check the user has not exceeded their per SMS cost limit."""
num_sms = ceil(len(msg) / 160)
if limit == 0:
return
Expand Down Expand Up @@ -662,6 +664,7 @@ class UserProfile(models.Model):
can_archive = models.BooleanField(default=True)

def get_absolute_url(self):
"""Url for this user profile."""
return reverse('user_profile_form', args=[str(self.pk)])

def __str__(self):
Expand Down
2 changes: 1 addition & 1 deletion apostello/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ def email_admin_on_signup(request, user, **kwargs):
body = body.format(str(user))
from site_config.models import SiteConfiguration
to_ = SiteConfiguration.get_solo().office_email
if len(to_) > 0:
if to_:
send_async_mail("[apostello] New User", body, [to_], )
1 change: 1 addition & 0 deletions apostello/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def fetch_default_reply(msg=''):


def retry_request(url, http_method, *args, **kwargs):
"""Make a http request and retry 3 times if it fails."""
assert http_method in ['get', 'post', 'delete', 'patch', 'put']
MAX_TRIES = 3
r_func = getattr(requests, http_method)
Expand Down
3 changes: 3 additions & 0 deletions apostello/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def get(self, request, *args, **kwargs):


class NotApprovedView(TemplateView):
"""Simple view that presents the not approved page."""
template_name = 'apostello/not_approved.html'

def get_context_data(self, **kwargs):
"""Inject not approved message into context."""
context = super(NotApprovedView, self).get_context_data(**kwargs)
s = SiteConfiguration.get_solo()
context['msg'] = s.not_approved_msg
Expand Down Expand Up @@ -342,6 +344,7 @@ class CreateAllGroupView(LoginRequiredMixin, ProfilePermsMixin, FormView):
success_url = '/group/all/'

def get_context_data(self, **kwargs):
"""Inject intro and button text into context."""
context = super(CreateAllGroupView, self).get_context_data(**kwargs)
context['submit_text'] = 'Create'
context['intro_text'] = 'You can use this form to create a new group' \
Expand Down
14 changes: 7 additions & 7 deletions graphs/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)
from graphs.sms_freq import sms_graph_data

CleanStyleLargeText = CleanStyle(legend_font_size=30, tooltip_font_size=30, )
clean_style_large_text = CleanStyle(legend_font_size=30, tooltip_font_size=30,)


def recent():
Expand Down Expand Up @@ -38,7 +38,7 @@ def contacts():
"""Render pie chart for contacts."""
pie_chart = pygal.Pie(
inner_radius=.6,
style=CleanStyleLargeText,
style=clean_style_large_text,
margin=0,
value_formatter=lambda x: '{}'.format(x),
)
Expand All @@ -59,7 +59,7 @@ def contacts():

def groups():
"""Render tree map of group size."""
treemap = pygal.Treemap(style=CleanStyleLargeText, margin=0, )
treemap = pygal.Treemap(style=clean_style_large_text, margin=0, )
for grp in RecipientGroup.objects.filter(is_archived=False):
treemap.add(str(grp), [grp.recipient_set.all().count()])

Expand All @@ -70,7 +70,7 @@ def keywords():
"""Render pie chart for keywords."""
pie_chart = pygal.Pie(
inner_radius=.6,
style=CleanStyleLargeText,
style=clean_style_large_text,
margin=0,
value_formatter=lambda x: '{}'.format(x),
)
Expand All @@ -86,7 +86,7 @@ def keywords():

def incoming_by_contact():
"""Render tree map of incoming messages, grouped by user."""
treemap = pygal.Treemap(style=CleanStyleLargeText, margin=0, )
treemap = pygal.Treemap(style=clean_style_large_text, margin=0, )
for con in Recipient.objects.filter(is_archived=False):
treemap.add(
str(con),
Expand All @@ -98,7 +98,7 @@ def incoming_by_contact():

def outgoing_by_contact():
"""Render tree map of outgoing messages, grouped by user."""
treemap = pygal.Treemap(style=CleanStyleLargeText, margin=0, )
treemap = pygal.Treemap(style=clean_style_large_text, margin=0, )
for con in Recipient.objects.filter(is_archived=False):
treemap.add(
str(con),
Expand All @@ -112,7 +112,7 @@ def sms_totals():
"""Render pie chart for sms totals."""
pie_chart = pygal.Pie(
inner_radius=.6,
style=CleanStyleLargeText,
style=clean_style_large_text,
margin=0,
value_formatter=lambda x: '{}'.format(x),
)
Expand Down

0 comments on commit e629d48

Please sign in to comment.