Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
'Massive refactor' - Manav 2017
Browse files Browse the repository at this point in the history
  • Loading branch information
omkarmoghe committed Jul 11, 2017
1 parent cc6ae30 commit 790b839
Show file tree
Hide file tree
Showing 41 changed files with 1,045 additions and 2,375 deletions.
28 changes: 11 additions & 17 deletions MHacks/admin.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from django.contrib import admin

from models import *
from announcements import AnnouncementModel
from events import EventModel
from locations import LocationModel
from floors import FloorModel
from scan_events import ScanEventModel, ScanEventUser
from users import MHacksUser


@admin.register(MHacksUser)
class UserAdmin(admin.ModelAdmin):
search_fields = ['first_name', 'last_name', 'email']


@admin.register(Location)
@admin.register(LocationModel)
class LocationAdmin(admin.ModelAdmin):
search_fields = ['name']


@admin.register(Event)
@admin.register(EventModel)
class EventAdmin(admin.ModelAdmin):
search_fields = ['name', 'info']
list_display = ['name', 'start', 'duration', 'category', 'deleted_string', 'location_list']
Expand All @@ -29,7 +34,7 @@ def location_list(self, obj):
location_list.short_description = 'LOCATIONS'


@admin.register(Announcement)
@admin.register(AnnouncementModel)
class AnnouncementAdmin(admin.ModelAdmin):
search_fields = ['title', 'info']
list_display = ['title', 'broadcast_at', 'category', 'sent', 'approved', 'deleted_string']
Expand All @@ -40,18 +45,7 @@ def deleted_string(self, obj):
deleted_string.short_description = 'DELETED'


@admin.register(Application)
class ApplicationAdmin(admin.ModelAdmin):
search_fields = ['user__first_name', 'user__last_name', 'user__email', 'decision']


@admin.register(Registration)
class RegistrationAdmin(admin.ModelAdmin):
search_fields = ['user__first_name', 'user__last_name', 'user__email', 'acceptance', 'dietary_restrictions',
'transportation']


@admin.register(ScanEvent)
@admin.register(ScanEventModel)
class ScanEventAdmin(admin.ModelAdmin):
search_fields = ['name']

Expand All @@ -61,6 +55,6 @@ class ScanEventUserAdmin(admin.ModelAdmin):
search_fields = ['scan_event__name']


@admin.register(Floor)
@admin.register(FloorModel)
class FloorAdmin(admin.ModelAdmin):
search_fields = ['name']
68 changes: 68 additions & 0 deletions MHacks/announcements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.db.models import Q
from django.utils import timezone
from rest_framework.fields import CharField

from utils import GenericListCreateModel, GenericUpdateDestroyModel, UnixEpochDateField
from models import Any, MHacksModelSerializer


class AnnouncementModel(Any):
title = models.CharField(max_length=60)
info = models.TextField(default='')
broadcast_at = models.DateTimeField()
category = models.PositiveIntegerField(validators=[MinValueValidator(0),
MaxValueValidator(31)], help_text="0 for none; 1 for emergency; 2 for logistics; 4 for food; 8 for event; Add 16 to make sponsored")
approved = models.BooleanField(default=False)
sent = models.BooleanField(default=False)

@staticmethod
def max_category():
return 31

def __unicode__(self):
return self.title


class AnnouncementSerializer(MHacksModelSerializer):
id = CharField(read_only=True)
broadcast_at = UnixEpochDateField()

class Meta:
model = AnnouncementModel
fields = ('id', 'title', 'info', 'broadcast_at', 'category', 'approved')


class AnnouncementAPIView(GenericUpdateDestroyModel):
serializer_class = AnnouncementSerializer
queryset = AnnouncementModel.objects.all()


class AnnouncementListAPIView(GenericListCreateModel):
"""
Announcements are what send push notifications and are useful for pushing updates to MHacks participants.
Anybody who is logged in can make a GET request where as only authorized users can create, update and delete them.
"""
serializer_class = AnnouncementSerializer
query_set = AnnouncementModel.objects.none()

def get_queryset(self):
date_last_updated = super(AnnouncementListAPIView, self).get_queryset()

if not self.request.user or not self.request.user.has_perm('MHacks.change_announcement'):
query_set = AnnouncementModel.objects.all().filter(approved=True).filter(broadcast_at__lte=timezone.now())
if date_last_updated:
query_set = query_set.filter(Q(last_updated__gte=date_last_updated) | Q(broadcast_at__gte=date_last_updated))
else:
query_set = query_set.filter(deleted=False)
else:
query_set = AnnouncementModel.objects.all()
if date_last_updated:
query_set = query_set.filter(last_updated__gte=date_last_updated)
else:
query_set = query_set.filter(deleted=False)
return query_set



155 changes: 0 additions & 155 deletions MHacks/application_lists.py

This file was deleted.

39 changes: 36 additions & 3 deletions MHacks/v1/auth.py → MHacks/authentication.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from django.views.decorators.csrf import csrf_exempt

from push_notifications.models import APNSDevice, GCMDevice
from rest_framework import serializers
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.response import Response
from rest_framework.authtoken import views
from rest_framework.authtoken.models import Token
from rest_framework.authentication import SessionAuthentication

from MHacks.v1.serializers import AuthSerializer
from MHacks.v1.util import serialized_user
from announcements import AnnouncementModel as AnnouncementModel
from utils import serialized_user


class CsrfExemptSessionAuthentication(SessionAuthentication):
def enforce_csrf(self, request):
return


class Authentication(views.ObtainAuthToken):
class AuthenticationAPIView(views.ObtainAuthToken):
"""
An easy convenient way to log a user in to get his/her token and the groups they are in.
It also returns other basic information about the user like their name, etc.
Expand Down Expand Up @@ -57,3 +59,34 @@ def post(self, request, *args, **kwargs):
if push_notification:
self.save_device(push_notification, user)
return Response({'token': token.key, 'user': serialized_user(user)})


class AuthSerializer(AuthTokenSerializer):
# Extends auth token serializer to accommodate push notifs

token = serializers.CharField(required=False)
is_gcm = serializers.BooleanField(required=False)

def validate(self, attributes):
attributes = super(AuthSerializer, self).validate(attributes)

# Optionally add the token if it exists
if 'registration_id' in attributes.keys() and 'is_gcm' in attributes.keys():
token = attributes.get('registration_id')
is_gcm = attributes.get('is_gcm')
preference = attributes.get('name', attributes.get('preference', '63'))
if not isinstance(preference, str):
preference = str(AnnouncementModel.max_category())
attributes['push_notification'] = {
'registration_id': token,
'is_gcm': is_gcm,
'name': preference
}

return attributes

def create(self, validated_data):
pass

def update(self, instance, validated_data):
pass
32 changes: 0 additions & 32 deletions MHacks/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@
from django.shortcuts import resolve_url


def application_reader_required(view_function, redirect_to=None):
return ApplicationReaderRequired(view_function, redirect_to)


class ApplicationReaderRequired(object):
def __init__(self, view_function, redirect_to):
self.view_function = view_function
self.redirect_to = redirect_to

def __call__(self, request, *args, **kwargs):
if request.user is None or not request.user.is_application_reader:
from django.conf import settings
return HttpResponseRedirect(resolve_url(self.redirect_to or settings.LOGIN_REDIRECT_URL))
return self.view_function(request, *args, **kwargs)


def anonymous_required(view_function, redirect_to=None):
return AnonymousRequired(view_function, redirect_to)

Expand All @@ -32,19 +16,3 @@ def __call__(self, request, *args, **kwargs):
from django.conf import settings
return HttpResponseRedirect(resolve_url(self.redirect_to or settings.LOGIN_REDIRECT_URL))
return self.view_function(request, *args, **kwargs)


def stats_team_required(view_function, redirect_to=None):
return StatsTeamRequired(view_function, redirect_to)


class StatsTeamRequired(object):
def __init__(self, view_function, redirect_to):
self.view_function = view_function
self.redirect_to = redirect_to

def __call__(self, request, *args, **kwargs):
if not request.user or not request.user.groups.filter(name='stats_team').exists():
from django.conf import settings
return HttpResponseRedirect(resolve_url(self.redirect_to or settings.LOGIN_REDIRECT_URL))
return self.view_function(request, *args, **kwargs)
Loading

1 comment on commit 790b839

@TristanWiley
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plz, Refactor was last year.

Please sign in to comment.