# Path: user_resource/models.py
# class ContactNumber(models.Model):
#     user = models.ForeignKey(User, related_name='contacts')
#     number = models.CharField(max_length=16)
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return self.number
# 
# class InstituteAddress(models.Model):
#     user = models.OneToOneField(User, related_name='insti_address')
#     room = models.CharField(max_length=8, null=True, blank=True)
#     hostel = models.CharField(max_length=8, choices=HOSTELS, null=True, blank=True)
#     _history_ = HistoricalRecords()
# 
#     def clean(self):
#         if self.room:
#             if self.hostel in HOSTELS_WITH_WINGS:
#                 if not ROOM_VALIDATION_REGEX.match(self.room):
#                     raise ValidationError(_('Room number must have wing name like A-123'))
# 
#     def __str__(self):
#         if self.hostel:
#             if self.room:
#                 return "%s-%s" % (self.hostel, self.room)
#             return self.hostel
#         return ''
# 
# class Program(models.Model):
#     user = models.OneToOneField(User, related_name='program')
#     department = models.CharField(max_length=16, choices=SORTED_DISCIPLINES, null=True, blank=True)
#     join_year = models.PositiveSmallIntegerField(null=True, blank=True, validators=[validate_join_year])
#     graduation_year = models.PositiveSmallIntegerField(null=True, blank=True, validators=[validate_graduation_year])
#     degree = models.CharField(max_length=16, choices=DEGREES, null=True, blank=True)
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return "%s, %s" % (self.get_degree_display(), self.get_department_display())
# 
# class SecondaryEmail(models.Model):
#     user = models.ForeignKey(User, related_name='secondary_emails')
#     email = models.EmailField()
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return self.email
# 
# Path: user_resource/oauth.py
# DEFAULT_FIELDS = ['id', ]
# 
# USER_FIELDS = [
#     'username',
#     'first_name',
#     'last_name',
#     'type',
#     'is_alumni',
#     'sex',
#     'profile_picture',
#     'email',
#     'program',
#     'secondary_emails',
#     'contacts',
#     'insti_address',
#     'mobile',
#     'roll_number',
# ]


import copy
from django.contrib.auth.models import User
from rest_framework import serializers
from .models import ContactNumber, InstituteAddress, Program, SecondaryEmail
from .oauth import DEFAULT_FIELDS, USER_FIELDS


class InstituteAddressSerializer(serializers.ModelSerializer):
    hostel_name = serializers.SerializerMethodField()

    def get_hostel_name(self, obj):
        return obj.get_hostel_display()

    class Meta:
        model = InstituteAddress
        exclude = ['user']


class UserSerializer(serializers.ModelSerializer):
    program = ProgramSerializer()
    secondary_emails = SecondaryEmailSerializer(many=True)
    contacts = ContactNumberSerializer(many=True)
    insti_address = InstituteAddressSerializer()
    mobile = serializers.CharField(source='userprofile.mobile')
    roll_number = serializers.CharField(source='userprofile.roll_number')
    profile_picture = serializers.ImageField(source='userprofile.profile_picture')
    sex = serializers.CharField(source='userprofile.sex')
    type = serializers.CharField(source='userprofile.type')
    is_alumni = serializers.BooleanField(source='userprofile.is_alumni')

    def __init__(self, *args, **kwargs):
        super(UserSerializer, self).__init__(*args, **kwargs)
        fields = self.context.get('fields')
        if not isinstance(fields, list) and not isinstance(fields, set):
            fields = []

# Path: user_resource/models.py
# class ContactNumber(models.Model):
#     user = models.ForeignKey(User, related_name='contacts')
#     number = models.CharField(max_length=16)
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return self.number
# 
# class InstituteAddress(models.Model):
#     user = models.OneToOneField(User, related_name='insti_address')
#     room = models.CharField(max_length=8, null=True, blank=True)
#     hostel = models.CharField(max_length=8, choices=HOSTELS, null=True, blank=True)
#     _history_ = HistoricalRecords()
# 
#     def clean(self):
#         if self.room:
#             if self.hostel in HOSTELS_WITH_WINGS:
#                 if not ROOM_VALIDATION_REGEX.match(self.room):
#                     raise ValidationError(_('Room number must have wing name like A-123'))
# 
#     def __str__(self):
#         if self.hostel:
#             if self.room:
#                 return "%s-%s" % (self.hostel, self.room)
#             return self.hostel
#         return ''
# 
# class Program(models.Model):
#     user = models.OneToOneField(User, related_name='program')
#     department = models.CharField(max_length=16, choices=SORTED_DISCIPLINES, null=True, blank=True)
#     join_year = models.PositiveSmallIntegerField(null=True, blank=True, validators=[validate_join_year])
#     graduation_year = models.PositiveSmallIntegerField(null=True, blank=True, validators=[validate_graduation_year])
#     degree = models.CharField(max_length=16, choices=DEGREES, null=True, blank=True)
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return "%s, %s" % (self.get_degree_display(), self.get_department_display())
# 
# class SecondaryEmail(models.Model):
#     user = models.ForeignKey(User, related_name='secondary_emails')
#     email = models.EmailField()
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return self.email
# 
# Path: user_resource/oauth.py
# DEFAULT_FIELDS = ['id', ]
# 
# USER_FIELDS = [
#     'username',
#     'first_name',
#     'last_name',
#     'type',
#     'is_alumni',
#     'sex',
#     'profile_picture',
#     'email',
#     'program',
#     'secondary_emails',
#     'contacts',
#     'insti_address',
#     'mobile',
#     'roll_number',
# ]


# Path: user_resource/serializers.py
import copy
from django.contrib.auth.models import User
from rest_framework import serializers
from .models import ContactNumber, InstituteAddress, Program, SecondaryEmail
from .oauth import DEFAULT_FIELDS, USER_FIELDS



class InstituteAddressSerializer(serializers.ModelSerializer):
    hostel_name = serializers.SerializerMethodField()

    def get_hostel_name(self, obj):
        return obj.get_hostel_display()

    class Meta:
        model = InstituteAddress
        exclude = ['user']


class UserSerializer(serializers.ModelSerializer):
    program = ProgramSerializer()
    secondary_emails = SecondaryEmailSerializer(many=True)
    contacts = ContactNumberSerializer(many=True)
    insti_address = InstituteAddressSerializer()
    mobile = serializers.CharField(source='userprofile.mobile')
    roll_number = serializers.CharField(source='userprofile.roll_number')
    profile_picture = serializers.ImageField(source='userprofile.profile_picture')
    sex = serializers.CharField(source='userprofile.sex')
    type = serializers.CharField(source='userprofile.type')
    is_alumni = serializers.BooleanField(source='userprofile.is_alumni')

    def __init__(self, *args, **kwargs):
        super(UserSerializer, self).__init__(*args, **kwargs)
        fields = self.context.get('fields')
        if not isinstance(fields, list) and not isinstance(fields, set):
            fields = []

        fields.extend(DEFAULT_FIELDS)