# Path: apps/core/models.py
# class Service(models.Model):
#     """
#     denotes single sso client
#     - name:               unique name
#     - is_shown:           decides to show in SSO main page
#     - alias:              user friendly name
#     - scope:              scope; TEST / SPARCS / PUBLIC
#     - main_url:           main page url
#     - login_callback_url: login callback url
#     - unregister_url:     unregister page url
#     - secret_key:         secret key
#     - admin_user:         admin of this client
#     - cooltime:           cooltime (days) to re-register; < 60
#     - icon:               icon of this service
#     """
# 
#     name = models.CharField(max_length=40, primary_key=True)
#     is_shown = models.BooleanField(default=True)
#     alias = models.CharField(max_length=30)
#     scope = models.CharField(max_length=6, choices=SERVICE_SCOPE,
#                              default=SERVICE_TEST)
#     main_url = models.CharField(max_length=200)
#     login_callback_url = models.CharField(max_length=200)
#     unregister_url = models.CharField(max_length=200, null=True, blank=True)
#     secret_key = models.CharField(max_length=100)
#     admin_user = models.ForeignKey(User, on_delete=models.CASCADE,
#                                    related_name='managed_services')
#     cooltime = models.IntegerField()
#     icon = models.ImageField(null=True, blank=True)
# 
#     @property
#     def icon_url(self):
#         return self.icon.url if self.icon else '/static/img/test-service.png'
# 
#     def __str__(self):
#         return self.alias
# 
# class Statistic(models.Model):
#     """
#     denotes single statistic; raw json
#     - time: timestamp of statistic
#     - data: raw json statistic data
#     """
#     time = models.DateTimeField()
#     data = models.TextField()
# 
#     def pretty(self):
#         time_str = localtime(self.time).isoformat()
#         return f'{time_str} {self.data}'
# 
#     def __str__(self):
#         return f'{self.time}'


import json
from datetime import timedelta
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from django.utils import timezone
from apps.core.models import Service, Statistic

        if user.profile.birthday:
            year = user.profile.birthday.year
            if year in stat['birth_year']:
                stat['birth_year'][year] += 1
            else:
                stat['birth_year'][year] = 1

    def add_kaist_stat(self, service, kaist_info):
        kaist_stat = self.stats[service]['kaist']

        if 'ku_std_no' in kaist_info and \
                len(kaist_info['ku_std_no']) == 8:
            start_year = kaist_info['ku_std_no'][:4]
            if start_year in kaist_stat['start_year']:
                kaist_stat['start_year'][start_year] += 1
            else:
                kaist_stat['start_year'][start_year] = 1

        if 'ku_born_date' in kaist_info:
            birth_year = kaist_info['ku_born_date'][:4]
            if birth_year in kaist_stat['birth_year']:
                kaist_stat['birth_year'][birth_year] += 1
            else:
                kaist_stat['birth_year'][birth_year] = 1

        if 'ku_sex' in kaist_info:
            gender = 'male' if kaist_info['ku_sex'] == 'M' else 'female'
            kaist_stat['gender'][gender] += 1


# Path: apps/core/models.py
# class Service(models.Model):
#     """
#     denotes single sso client
#     - name:               unique name
#     - is_shown:           decides to show in SSO main page
#     - alias:              user friendly name
#     - scope:              scope; TEST / SPARCS / PUBLIC
#     - main_url:           main page url
#     - login_callback_url: login callback url
#     - unregister_url:     unregister page url
#     - secret_key:         secret key
#     - admin_user:         admin of this client
#     - cooltime:           cooltime (days) to re-register; < 60
#     - icon:               icon of this service
#     """
# 
#     name = models.CharField(max_length=40, primary_key=True)
#     is_shown = models.BooleanField(default=True)
#     alias = models.CharField(max_length=30)
#     scope = models.CharField(max_length=6, choices=SERVICE_SCOPE,
#                              default=SERVICE_TEST)
#     main_url = models.CharField(max_length=200)
#     login_callback_url = models.CharField(max_length=200)
#     unregister_url = models.CharField(max_length=200, null=True, blank=True)
#     secret_key = models.CharField(max_length=100)
#     admin_user = models.ForeignKey(User, on_delete=models.CASCADE,
#                                    related_name='managed_services')
#     cooltime = models.IntegerField()
#     icon = models.ImageField(null=True, blank=True)
# 
#     @property
#     def icon_url(self):
#         return self.icon.url if self.icon else '/static/img/test-service.png'
# 
#     def __str__(self):
#         return self.alias
# 
# class Statistic(models.Model):
#     """
#     denotes single statistic; raw json
#     - time: timestamp of statistic
#     - data: raw json statistic data
#     """
#     time = models.DateTimeField()
#     data = models.TextField()
# 
#     def pretty(self):
#         time_str = localtime(self.time).isoformat()
#         return f'{time_str} {self.data}'
# 
#     def __str__(self):
#         return f'{self.time}'


# Path: apps/core/management/commands/collect-stats.py
import json
from datetime import timedelta
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from django.utils import timezone
from apps.core.models import Service, Statistic


        if user.profile.birthday:
            year = user.profile.birthday.year
            if year in stat['birth_year']:
                stat['birth_year'][year] += 1
            else:
                stat['birth_year'][year] = 1

    def add_kaist_stat(self, service, kaist_info):
        kaist_stat = self.stats[service]['kaist']

        if 'ku_std_no' in kaist_info and \
                len(kaist_info['ku_std_no']) == 8:
            start_year = kaist_info['ku_std_no'][:4]
            if start_year in kaist_stat['start_year']:
                kaist_stat['start_year'][start_year] += 1
            else:
                kaist_stat['start_year'][start_year] = 1

        if 'ku_born_date' in kaist_info:
            birth_year = kaist_info['ku_born_date'][:4]
            if birth_year in kaist_stat['birth_year']:
                kaist_stat['birth_year'][birth_year] += 1
            else:
                kaist_stat['birth_year'][birth_year] = 1

        if 'ku_sex' in kaist_info:
            gender = 'male' if kaist_info['ku_sex'] == 'M' else 'female'
            kaist_stat['gender'][gender] += 1


        if 'ku_kaist_org_id' in kaist_info: