# Path: cognitive/app/models.py
# class User(models.Model):
#     username = models.CharField(max_length=50)
#     full_name = models.CharField(max_length=50)
#     email = models.CharField(max_length=100)
#     password = models.CharField(max_length=100)
#     token = models.CharField(max_length=100, blank=True, null=True)
#     max_experiments = models.IntegerField(default=50)
# 
#     def __str__(self):
#         return self.username
# 
#     @classmethod
#     def authenticate(cls, username_or_email, password):
#         try:
#             user = User.objects.get(username=username_or_email)
#         except:
#             try:
#                 user = User.objects.get(email=username_or_email)
#             except:
#                 return None
#         if not user.confirm_password(password):
#             return None
#         return user
# 
#     @classmethod
#     def validate(cls, username, email, password):
#         err_msg = ""
#         if not username:
#             err_msg = "Empty username field"
#             raise ValidationError(err_msg)
#         if not password:
#             err_msg = "Empty password field"
#             raise ValidationError(err_msg)
#         if not email:
#             err_msg = "Empty email field"
#             raise ValidationError(err_msg)
#         validate_email(email)
#         if User.objects.filter(username=username).count() > 0:
#             err_msg = "Username already exists"
#             raise ValidationError(err_msg)
#         if User.objects.filter(email=email).count() > 0:
#             err_msg = "Email already exists"
#             raise ValidationError(err_msg)
#         return True
# 
#     def confirm_password(self, password):
#         # TODO: password must be encrypted
#         if self.password == password:
#             return True
#         else:
#             return False
# 
#     # TODO: password encryption
# 
#     @classmethod
#     def generate_token(cls):
#         def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
#             return ''.join(random.choice(chars) for _ in range(size))
#         return id_generator()
# 
#     def has_correct_token(self, token):
#         return self.token == token
# 
# Path: cognitive/app/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         # fields = ['id','username','full_name']
# 
# Path: cognitive/app/views.py
# def send_response(method, serializer):
#     if method == 'GET':
#         return Response(serializer.data)
#     elif method == 'POST':
#         if serializer.is_valid():
#             return Response(serializer.data, status=status.HTTP_201_CREATED)
#         else:
#             return Response(
#                 serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     elif method == 'PUT':
#         if serializer.is_valid():
#             return Response(serializer.data)
#         else:
#             return Response(
#                 serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     elif method == 'DELETE':
#         return Response(status=status.HTTP_204_NO_CONTENT)


from ..models import User
from ..serializers import UserSerializer
from ..views import send_response
from rest_framework import viewsets
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from django.http import HttpResponse
    """
    A simple ViewSet that for listing or retrieving users.

    user_list = UserViewSet.as_view({
        'get': 'list',
        'post': 'create'
    })

    user_detail = UserViewSet.as_view({
        'get': 'retrieve',
        'put': 'update',
        'patch': 'partial_update',
        'delete': 'destroy'
    })
    """
    def list(self, request):
        """
        Lists all users in the system
        ---
        """
        user = User.objects.all()
        serializer = UserSerializer(user, many=True)
        return send_response(request.method, serializer)

    def retrieve(self, request, pk=None):
        """
        Retrieve a user in the system
        ---
        """
        user = User.objects.get(pk=pk)

# Path: cognitive/app/models.py
# class User(models.Model):
#     username = models.CharField(max_length=50)
#     full_name = models.CharField(max_length=50)
#     email = models.CharField(max_length=100)
#     password = models.CharField(max_length=100)
#     token = models.CharField(max_length=100, blank=True, null=True)
#     max_experiments = models.IntegerField(default=50)
# 
#     def __str__(self):
#         return self.username
# 
#     @classmethod
#     def authenticate(cls, username_or_email, password):
#         try:
#             user = User.objects.get(username=username_or_email)
#         except:
#             try:
#                 user = User.objects.get(email=username_or_email)
#             except:
#                 return None
#         if not user.confirm_password(password):
#             return None
#         return user
# 
#     @classmethod
#     def validate(cls, username, email, password):
#         err_msg = ""
#         if not username:
#             err_msg = "Empty username field"
#             raise ValidationError(err_msg)
#         if not password:
#             err_msg = "Empty password field"
#             raise ValidationError(err_msg)
#         if not email:
#             err_msg = "Empty email field"
#             raise ValidationError(err_msg)
#         validate_email(email)
#         if User.objects.filter(username=username).count() > 0:
#             err_msg = "Username already exists"
#             raise ValidationError(err_msg)
#         if User.objects.filter(email=email).count() > 0:
#             err_msg = "Email already exists"
#             raise ValidationError(err_msg)
#         return True
# 
#     def confirm_password(self, password):
#         # TODO: password must be encrypted
#         if self.password == password:
#             return True
#         else:
#             return False
# 
#     # TODO: password encryption
# 
#     @classmethod
#     def generate_token(cls):
#         def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
#             return ''.join(random.choice(chars) for _ in range(size))
#         return id_generator()
# 
#     def has_correct_token(self, token):
#         return self.token == token
# 
# Path: cognitive/app/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         # fields = ['id','username','full_name']
# 
# Path: cognitive/app/views.py
# def send_response(method, serializer):
#     if method == 'GET':
#         return Response(serializer.data)
#     elif method == 'POST':
#         if serializer.is_valid():
#             return Response(serializer.data, status=status.HTTP_201_CREATED)
#         else:
#             return Response(
#                 serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     elif method == 'PUT':
#         if serializer.is_valid():
#             return Response(serializer.data)
#         else:
#             return Response(
#                 serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     elif method == 'DELETE':
#         return Response(status=status.HTTP_204_NO_CONTENT)


# Path: cognitive/app/api/users.py
from ..models import User
from ..serializers import UserSerializer
from ..views import send_response
from rest_framework import viewsets
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from django.http import HttpResponse

    """
    A simple ViewSet that for listing or retrieving users.

    user_list = UserViewSet.as_view({
        'get': 'list',
        'post': 'create'
    })

    user_detail = UserViewSet.as_view({
        'get': 'retrieve',
        'put': 'update',
        'patch': 'partial_update',
        'delete': 'destroy'
    })
    """
    def list(self, request):
        """
        Lists all users in the system
        ---
        """
        user = User.objects.all()
        serializer = UserSerializer(user, many=True)
        return send_response(request.method, serializer)

    def retrieve(self, request, pk=None):
        """
        Retrieve a user in the system
        ---
        """
        user = User.objects.get(pk=pk)

        serializer = UserSerializer(user)