Created
April 30, 2018 10:02
-
-
Save BuildWithLal/f9f5043b713288b02ad3e076ebef098f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
views.py | |
from rest_framework_mongoengine import generics | |
from rest_framework.response import Response | |
from .serializers import LoginSerializer | |
from configuration.models import User | |
#from captive_portal.models import SessionHistory, Voucher | |
from .models import SessionHistory | |
from rest_framework import serializers | |
from rest_framework import status | |
class LoginView(generics.GenericAPIView): | |
serializer_class = LoginSerializer | |
authentication_classes = () | |
permission_classes = () | |
def post(self, request): | |
serializer = self.get_serializer(data=request.data) | |
# validate username, password, session_start and end date | |
serializer.is_valid(raise_exception=True) | |
# validate voucher data limit | |
serializer.validate_voucher_data_limit() | |
# create new session history | |
serializer.create_session_history() | |
user = User.objects.get(username=serializer.data.get('username')) | |
serializer = self.get_serializer(user) | |
return Response(serializer.data) | |
serializer.py | |
from rest_framework.exceptions import AuthenticationFailed | |
from rest_framework import serializers | |
from django.contrib.auth import authenticate | |
from .models import Token, SessionHistory | |
from configuration.models import User, UserRole, RolePermission | |
from rest_framework_mongoengine.serializers import DocumentSerializer | |
from django.utils import timezone | |
from captive_portal.models import Voucher | |
import datetime | |
from rest_framework.exceptions import ValidationError | |
class LoginSerializer(serializers.Serializer): | |
username = serializers.CharField() | |
password = serializers.CharField(write_only=True) | |
first_name = serializers.CharField(read_only=True) | |
last_name = serializers.CharField(read_only=True) | |
email = serializers.EmailField(read_only=True) | |
phone = serializers.CharField(read_only=True) | |
token = serializers.CharField(read_only=True) | |
is_superuser = serializers.BooleanField(read_only=True) | |
roles = serializers.SerializerMethodField(read_only=True) | |
address = serializers.CharField(read_only=True) | |
state = serializers.CharField(read_only=True) | |
city = serializers.CharField(read_only=True) | |
country = serializers.CharField(read_only=True) | |
start_datetime = serializers.DateTimeField() | |
end_datetime = serializers.DateTimeField() | |
voucher_id = serializers.CharField() | |
class Meta: | |
fields = '__all__' | |
def __init__(self, *args, **kwargs): | |
try: | |
self.user = args[0] | |
except: | |
self.user = None | |
super().__init__(*args, **kwargs) | |
def validate(self, data): | |
super().validate(data) | |
username = data.get('username') | |
password = data.get('password') | |
if data['start_datetime'] >= data['end_datetime']: | |
raise ValidationError('session history start date must be behind end date') | |
if username and password: | |
user = authenticate(self.context['request'], username=username, password=password) | |
if not user: | |
raise AuthenticationFailed(detail='invalid username or password') | |
if not user.token: | |
user.token = Token() | |
user.save() | |
self.user = User.objects.get(username=username) | |
return data | |
# check data usage.... | |
def validate_voucher_data_limit(self): | |
data = self.validated_data | |
voucher = Voucher.objects.get(id=data['voucher_id']) | |
user_sessions = SessionHistory.objects.filter(user=self.user, voucher=voucher) | |
data_usage = 0 | |
for session in user_sessions: | |
data_usage = data_usage + session['data_usage'] | |
if data_usage >= voucher.data_limit: | |
raise ValidationError('voucher data limit expired.') | |
def create_session_history(self): | |
data = self.validated_data | |
voucher = Voucher.objects.get(id=data['voucher_id']) | |
# pata ni ... sessionHistory k liye device kahan se ayega... baqi fields tu mil rahe | |
SessionHistory.objects.create(user=self.user, voucher=voucher, start_datetime=data['start_datetime'], end_datetime=data['end_datetime']) | |
@staticmethod | |
def get_permissions(role): | |
return [perm.get_perm_dict() for perm in RolePermission.objects.filter(role=role.to_dbref())] | |
def get_role_dict(self, role): | |
return { | |
"name": role.name, | |
"permissions": self.get_permissions(role) | |
} | |
def get_roles(self, obj): | |
return [self.get_role_dict(userRole.role) for userRole in UserRole.objects().filter(user=self.user.to_dbref())] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment