# Path: attendance/models.py
# class SSIDName(models.Model):
#     name = models.CharField(max_length=30)
# 
#     def generate_random_name(self):
#         size = random.randint(10, 17)
#         chars = string.ascii_lowercase + string.digits
#         self.name = ''.join(random.choice(chars) for _ in range(size))
# 
#     def __str__(self):
#         return self.name
# 
# class DailyAttendance(models.Model):
#     """
#     attendance = { "2016": {"user_id": [1, s_time, e_time], "user_id": [0, s_time, e_time]}  }
#     """
#     date = models.DateField()
#     attendance = JSONField()
# 
#     def __str__(self):
#         return str(self.date)
# 
# Path: attendance/serializers.py
# class SSIDNameSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = SSIDName
#         fields = ['name']
#         read_only_fields = ['name']


from datetime import datetime, timedelta
from django.contrib.auth.models import User
from django.http import Http404
from django.shortcuts import redirect, get_object_or_404, render
from django.utils import timezone
from django.utils.datetime_safe import date
from django.views.generic.base import View
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from attendance.models import SSIDName, DailyAttendance
from attendance.serializers import SSIDNameSerializer
from registration.models import UserInfo
    def get_student_years(self):
        batches = []
        year = timezone.now().year
        if timezone.now().month < self.month_new_year:
            year -= 1
        for i in range(self.year_to_display):
            batches += [year - i]
        return batches

    def create_daily_attendance_json_data(self):
        # attendance = { "2016": {"user_id": [1, s_time, e_time], "user_id": [0, s_time, e_time]}  }
        batch_list = self.get_student_years()
        print(batch_list)
        attendance_dict = {}

        for batch in batch_list:
            user_info_list = UserInfo.objects.filter(year=batch).order_by(
                'user__first_name')
            # display the current attendance for this date and batch
            attendance_list_batch = {}
            for user_info in user_info_list:
                user_attendance = [0, str(timezone.now().strftime('%X')), str(timezone.now().strftime('%X'))]
                attendance_list_batch[str(user_info.user.id)] = user_attendance

            # attendance list contains all the Attendance objects of the batch with date = d
            year = str(batch)
            attendance_dict[year] = attendance_list_batch
        return attendance_dict

    def post(self, request, format=None):

# Path: attendance/models.py
# class SSIDName(models.Model):
#     name = models.CharField(max_length=30)
# 
#     def generate_random_name(self):
#         size = random.randint(10, 17)
#         chars = string.ascii_lowercase + string.digits
#         self.name = ''.join(random.choice(chars) for _ in range(size))
# 
#     def __str__(self):
#         return self.name
# 
# class DailyAttendance(models.Model):
#     """
#     attendance = { "2016": {"user_id": [1, s_time, e_time], "user_id": [0, s_time, e_time]}  }
#     """
#     date = models.DateField()
#     attendance = JSONField()
# 
#     def __str__(self):
#         return str(self.date)
# 
# Path: attendance/serializers.py
# class SSIDNameSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = SSIDName
#         fields = ['name']
#         read_only_fields = ['name']


# Path: attendance/views.py
from datetime import datetime, timedelta
from django.contrib.auth.models import User
from django.http import Http404
from django.shortcuts import redirect, get_object_or_404, render
from django.utils import timezone
from django.utils.datetime_safe import date
from django.views.generic.base import View
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from attendance.models import SSIDName, DailyAttendance
from attendance.serializers import SSIDNameSerializer
from registration.models import UserInfo

    def get_student_years(self):
        batches = []
        year = timezone.now().year
        if timezone.now().month < self.month_new_year:
            year -= 1
        for i in range(self.year_to_display):
            batches += [year - i]
        return batches

    def create_daily_attendance_json_data(self):
        # attendance = { "2016": {"user_id": [1, s_time, e_time], "user_id": [0, s_time, e_time]}  }
        batch_list = self.get_student_years()
        print(batch_list)
        attendance_dict = {}

        for batch in batch_list:
            user_info_list = UserInfo.objects.filter(year=batch).order_by(
                'user__first_name')
            # display the current attendance for this date and batch
            attendance_list_batch = {}
            for user_info in user_info_list:
                user_attendance = [0, str(timezone.now().strftime('%X')), str(timezone.now().strftime('%X'))]
                attendance_list_batch[str(user_info.user.id)] = user_attendance

            # attendance list contains all the Attendance objects of the batch with date = d
            year = str(batch)
            attendance_dict[year] = attendance_list_batch
        return attendance_dict

    def post(self, request, format=None):

        ssid_name = SSIDName.objects.first().name