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

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
monty5811 committed Dec 22, 2015
1 parent fc06226 commit 11bb9ee
Show file tree
Hide file tree
Showing 34 changed files with 284 additions and 245 deletions.
14 changes: 7 additions & 7 deletions api/drf_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@


class CanSeeGroups(permissions.BasePermission):
"""Checks if a user should see Groups."""
"""Check if a user should see Groups."""
def has_permission(self, request, view):
return request.user.profile.can_see_groups


class CanSeeContactNames(permissions.BasePermission):
"""Checks if a user should see names of Contacts."""
"""Check if a user should see names of Contacts."""
def has_permission(self, request, view):
return request.user.profile.can_see_contact_names


class CanSeeKeywords(permissions.BasePermission):
"""Checks if a user should have access to keywords."""
"""Check if a user should have access to keywords."""
def has_permission(self, request, view):
return request.user.profile.can_see_keywords


class CanSeeOutgoing(permissions.BasePermission):
"""Checks if a user should have access to outgoing log."""
"""Check if a user should have access to outgoing log."""
def has_permission(self, request, view):
return request.user.profile.can_see_outgoing


class CanSeeIncoming(permissions.BasePermission):
"""Checks if a user should have access to incoming log."""
"""Check if a user should have access to incoming log."""
def has_permission(self, request, view):
return request.user.profile.can_see_incoming


class CanSeeKeyword(permissions.BasePermission):
"""Checks if a user should have access to a single keyword"""
"""Check if a user should have access to a single keyword."""
def has_object_permission(self, request, view, obj):
if not obj.is_locked():
if not obj.is_locked:
return True

return obj.can_user_access(request.user)
28 changes: 7 additions & 21 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@


class RecipientSerializer(serializers.ModelSerializer):
"""
Serializes apostello.models.Recipient for use in table.
"""
"""Serialize apostello.models.Recipient for use in table."""
url = serializers.CharField(source='get_absolute_url')

class Meta:
Expand All @@ -22,9 +20,7 @@ class Meta:


class RecipientGroupSerializer(serializers.ModelSerializer):
"""
Serializes apostello.models.RecipientGroup for use in table.
"""
"""Serialize apostello.models.RecipientGroup for use in table."""
cost = serializers.CharField(source='calculate_cost')
url = serializers.CharField(source='get_absolute_url')
members = serializers.ListField(source='all_recipients_names')
Expand All @@ -40,9 +36,7 @@ class Meta:


class ElvantoGroupSerializer(serializers.ModelSerializer):
"""
Serializers apostello.models.ElvantoGroup
"""
"""Serialize apostello.models.ElvantoGroup."""
last_synced = serializers.DateTimeField(format='%d %b %H:%M')

class Meta:
Expand All @@ -56,9 +50,7 @@ class Meta:


class KeywordSerializer(serializers.ModelSerializer):
"""
Serializes apostello.models.Keyword for use in table.
"""
"""Serialize apostello.models.Keyword for use in table."""
url = serializers.CharField(source='get_absolute_url')
responses_url = serializers.CharField(source='get_responses_url')
num_replies = serializers.CharField(source='num_matches')
Expand All @@ -77,9 +69,7 @@ class Meta:


class SmsInboundSerializer(serializers.ModelSerializer):
"""
Serializes apostello.models.SmsInbound for use in log and wall.
"""
"""Serialize apostello.models.SmsInbound for use in logs and wall."""
time_received = serializers.DateTimeField(format='%d %b %H:%M')

class Meta:
Expand All @@ -95,9 +85,7 @@ class Meta:


class SmsInboundSimpleSerializer(serializers.ModelSerializer):
"""
Serializes apostello.models.SmsInbound for use in log and wall.
"""
"""Serialize apostello.models.SmsInbound for use in log and wall."""
time_received = serializers.DateTimeField(format='%d %b %H:%M')

class Meta:
Expand All @@ -111,9 +99,7 @@ class Meta:


class SmsOutboundSerializer(serializers.ModelSerializer):
"""
Serializes apostello.models.SmsOutbound for use in log.
"""
"""Serialize apostello.models.SmsOutbound for use in log."""
time_sent = serializers.DateTimeField(format='%d %b %H:%M')
recipient = serializers.StringRelatedField()

Expand Down
15 changes: 13 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ApiCollection(APIView):
filters = {}

def get(self, request, format=None):
"""Handle get requests."""
if self.related_field is None:
objs = self.model_class.objects.all()
else:
Expand All @@ -43,13 +44,13 @@ class ApiMember(APIView):
serializer_class = None

def get(self, request, format=None, **kwargs):
"""Handle get requests."""
obj = get_object_or_404(self.model_class, pk=kwargs['pk'])

serializer = self.serializer_class(obj)
return Response(serializer.data)

def post(self, request, format=None, **kwargs):
"""Handles all the toggle buttons."""
"""Handle toggle buttons."""
pk = kwargs['pk']
reingest_sms = True if request.data.get('reingest', False) == 'true' else False
deal_with_sms = request.data.get('deal_with')
Expand Down Expand Up @@ -87,19 +88,23 @@ def post(self, request, format=None, **kwargs):


class ApiCollectionRecentSms(APIView):
"""SMS collection for a single recipient."""
permission_classes = (IsAuthenticated,)

def get(self, request, format=None, **kwargs):
"""Handle get requests."""
objs = SmsInbound.objects.filter(sender_num=Recipient.objects.get(pk=kwargs['pk']).number)
serializer = SmsInboundSerializer(objs, many=True)
return Response(serializer.data)


class ApiCollectionKeywordSms(APIView):
"""SMS collection for a single keyword."""
permission_classes = (IsAuthenticated,)
archive = False

def get(self, request, format=None, **kwargs):
"""Handle get requests."""
keyword_obj = Keyword.objects.get(pk=kwargs['pk'])
self.check_object_permissions(request, keyword_obj)
objs = SmsInbound.objects.filter(
Expand All @@ -114,9 +119,11 @@ def get(self, request, format=None, **kwargs):


class ApiCollectionAllWall(APIView):
"""SMS collection for a live wall."""
permission_classes = (IsAuthenticated,)

def get(self, request, format=None, **kwargs):
"""Handle get requests."""
cache_key = 'live_wall'
data = cache.get(cache_key)
if data is None:
Expand All @@ -131,16 +138,20 @@ def get(self, request, format=None, **kwargs):


class ElvantoPullButton(LoginRequiredMixin, ProfilePermsMixin, View):
"""View for elvanto pull button."""
required_perms = ['can_see_groups']

def post(self, request, format=None, **kwargs):
"""Handle post requests."""
pull_elvanto_groups.delay(force=True)
return JsonResponse({'status': 'pulling'})


class ElvantoFetchButton(LoginRequiredMixin, ProfilePermsMixin, View):
"""View for elvanto fetch button."""
required_perms = ['can_see_groups']

def post(self, request, format=None, **kwargs):
"""Handle post requests."""
fetch_elvanto_groups.delay(force=True)
return JsonResponse({'status': 'fetching'})
8 changes: 8 additions & 0 deletions apostello/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@admin.register(models.SmsOutbound)
class SmsOutboundAdmin(admin.ModelAdmin):
"""Admin class for apostello.models.SmsOutbound."""
list_display = (
'content',
'recipient',
Expand All @@ -24,6 +25,7 @@ class SmsOutboundAdmin(admin.ModelAdmin):

@admin.register(models.SmsInbound)
class SmsInboundAdmin(admin.ModelAdmin):
"""Admin class for apostello.models.SmsInbound."""
list_display = (
'content',
'sender_name',
Expand All @@ -35,6 +37,7 @@ class SmsInboundAdmin(admin.ModelAdmin):

@admin.register(models.Keyword)
class KeywordAdmin(admin.ModelAdmin):
"""Admin class for apostello.models.Keyword."""
list_display = (
'keyword',
'is_live',
Expand All @@ -50,6 +53,7 @@ class KeywordAdmin(admin.ModelAdmin):

@admin.register(models.Recipient)
class RecipientAdmin(admin.ModelAdmin):
"""Admin class for apostello.models.Recipient."""
list_display = (
'full_name',
'number',
Expand All @@ -60,6 +64,7 @@ class RecipientAdmin(admin.ModelAdmin):

@admin.register(models.RecipientGroup)
class RecipientGroupAdmin(admin.ModelAdmin):
"""Admin class for apostello.models.RecipientGroup."""
list_display = (
'name',
'description',
Expand All @@ -69,6 +74,7 @@ class RecipientGroupAdmin(admin.ModelAdmin):

@admin.register(models.ElvantoGroup)
class ElvantoGroupAdmin(admin.ModelAdmin):
"""Admin class for apostello.models.ElvantoGroup."""
list_display = (
'name',
'sync',
Expand All @@ -80,10 +86,12 @@ class ElvantoGroupAdmin(admin.ModelAdmin):


class UserProfileInline(admin.StackedInline):
"""Inline for apostello.models.UserProfile."""
model = models.UserProfile


class UserProfileAdmin(UserAdmin):
"""Admin class for apostello.models.UserProfile."""
inlines = [UserProfileInline, ]

admin.site.register(User, UserProfileAdmin)
2 changes: 1 addition & 1 deletion apostello/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@


def global_settings(request):
"""Expose TWILIO_FROM_NUM and DEBUG in templates"""
"""Expose TWILIO_FROM_NUM and DEBUG in templates."""
return {'TWILIO_FROM_NUM': settings.TWILIO_FROM_NUM,
'DEBUG': settings.DEBUG}
7 changes: 3 additions & 4 deletions apostello/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@


def keyword_access_check(method):
"""Checks a user can access a specific keyword."""

"""Check a user can access a specific keyword."""
@wraps(method)
def wrapper(request, *args, **kwargs):
if request.user.is_staff:
return method(request, *args, **kwargs)
try:
keyword = Keyword.objects.get(pk=kwargs['pk'])
if keyword.is_locked():
if keyword.is_locked:
if not keyword.can_user_access(request.user):
messages.warning(request, settings.NO_ACCESS_WARNING)
return redirect(reverse('keywords'))
Expand All @@ -31,7 +30,7 @@ def wrapper(request, *args, **kwargs):


def check_user_perms(view=None, require=None):
"""Checks a user has the specified permissions."""
"""Check a user has the specified permissions."""
if view is None:
return partial(check_user_perms, require=require)

Expand Down
11 changes: 4 additions & 7 deletions apostello/elvanto.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@


def elvanto():
"""Shortcut to create Elvanto API instance"""
"""Shortcut to create Elvanto API instance."""
return ElvantoAPI.Connection(APIKey=settings.ELVANTO_KEY)


def fix_elvanto_numbers(num_string):
"""
Checks if number starts with +447 or 077 and normalises
Check if number starts with +447 or 077 and normalises.
TODO: modify for other locales
TODO: modify for other locales.
"""
number = re.sub("[^0-9]", "", num_string)
if number.startswith(settings.COUNTRY_CODE + '7'):
Expand All @@ -30,10 +30,7 @@ def fix_elvanto_numbers(num_string):


def try_both_num_fields(mobile, phone):
"""
Returns a person's phone number by checking both the
'mobile' and 'phone' fields.
"""
"""Return a person's phone number by checking both the 'mobile' and 'phone' fields."""
try:
number = fix_elvanto_numbers(mobile)
except NotValidPhoneNumber:
Expand Down
18 changes: 5 additions & 13 deletions apostello/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@


class ApostelloException(Exception):
"""apostello base exception class"""
"""apostello base exception class."""
pass


class NoKeywordMatchException(ApostelloException):
"""
SMS matches no keywords.
"""
"""SMS matches no keywords."""
pass


class ArchivedItemException(ApostelloException):
"""
Item already exists in the archive.
"""
"""Item already exists in the archive."""
pass


class ElvantoException(ApostelloException):
"""
Elvanto API issue.
"""
"""Elvanto API issue."""
pass


class NotValidPhoneNumber(ApostelloException):
"""
Not a valid phone number.
"""
"""Not a valid phone number."""
pass

0 comments on commit 11bb9ee

Please sign in to comment.