# Path: kobra/db_fields.py
# class IdField(models.UUIDField):
#     def __init__(self, *args, **kwargs):
#         kwargs['primary_key'] = kwargs.get('primary_key', True)
#         kwargs['default'] = kwargs.get('default', uuid.uuid4)
#         kwargs['editable'] = kwargs.get('editable', False)
#         kwargs['verbose_name'] = kwargs.get('verbose_name', _('ID'))
#         super(IdField, self).__init__(*args, **kwargs)
# 
# class MoneyField(models.DecimalField):
#     def __init__(self, *args, **kwargs):
#         kwargs['max_digits'] = kwargs.get('max_digits', 12)
#         kwargs['decimal_places'] = 2
#         super(MoneyField, self).__init__(*args, **kwargs)
# 
# class NameField(StripValueMixin, models.CharField):
#     def __init__(self, *args, **kwargs):
#         kwargs['max_length'] = kwargs.get('max_length', 64)
#         kwargs['verbose_name'] = kwargs.get('verbose_name', _('name'))
#         super(NameField, self).__init__(*args, **kwargs)


import logging
import sesam
from django.conf import settings
from django.contrib.auth.models import (
    AbstractBaseUser,
    BaseUserManager,
    PermissionsMixin
)
from django.db import models
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from .db_fields import IdField, MoneyField, NameField

logger = logging.getLogger(__name__)

sesam_student_service_client = sesam.StudentServiceClient(
    settings.SESAM_USERNAME, settings.SESAM_PASSWORD)


def update_or_create_from_sesam(student=None, **kwargs):
    sesam_student = sesam_student_service_client.get_student(
        iso_id=kwargs.pop('iso_id', None),
        liu_id=kwargs.pop('liu_id', None),
        mifare_id=kwargs.pop('mifare_id', None),
        nor_edu_person_lin=kwargs.pop('id', None),
        nor_edu_person_nin=kwargs.pop('national_id', None)
    )

    if kwargs:
        raise TypeError("Can't search Sesam for the specified parameter(s)")

    if not student:
        student_kwargs = dict(id=sesam_student.nor_edu_person_lin)
        try:
            student = Student.objects.get(**student_kwargs)
        except Student.DoesNotExist:
            # Just instantiate, don't save.
            student = Student(**student_kwargs)

    student.email = sesam_student.email
    student.liu_id = sesam_student.liu_id
    student.liu_lin = sesam_student.liu_lin

# Path: kobra/db_fields.py
# class IdField(models.UUIDField):
#     def __init__(self, *args, **kwargs):
#         kwargs['primary_key'] = kwargs.get('primary_key', True)
#         kwargs['default'] = kwargs.get('default', uuid.uuid4)
#         kwargs['editable'] = kwargs.get('editable', False)
#         kwargs['verbose_name'] = kwargs.get('verbose_name', _('ID'))
#         super(IdField, self).__init__(*args, **kwargs)
# 
# class MoneyField(models.DecimalField):
#     def __init__(self, *args, **kwargs):
#         kwargs['max_digits'] = kwargs.get('max_digits', 12)
#         kwargs['decimal_places'] = 2
#         super(MoneyField, self).__init__(*args, **kwargs)
# 
# class NameField(StripValueMixin, models.CharField):
#     def __init__(self, *args, **kwargs):
#         kwargs['max_length'] = kwargs.get('max_length', 64)
#         kwargs['verbose_name'] = kwargs.get('verbose_name', _('name'))
#         super(NameField, self).__init__(*args, **kwargs)


# Path: kobra/models.py
import logging
import sesam
from django.conf import settings
from django.contrib.auth.models import (
    AbstractBaseUser,
    BaseUserManager,
    PermissionsMixin
)
from django.db import models
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from .db_fields import IdField, MoneyField, NameField


logger = logging.getLogger(__name__)

sesam_student_service_client = sesam.StudentServiceClient(
    settings.SESAM_USERNAME, settings.SESAM_PASSWORD)


def update_or_create_from_sesam(student=None, **kwargs):
    sesam_student = sesam_student_service_client.get_student(
        iso_id=kwargs.pop('iso_id', None),
        liu_id=kwargs.pop('liu_id', None),
        mifare_id=kwargs.pop('mifare_id', None),
        nor_edu_person_lin=kwargs.pop('id', None),
        nor_edu_person_nin=kwargs.pop('national_id', None)
    )

    if kwargs:
        raise TypeError("Can't search Sesam for the specified parameter(s)")

    if not student:
        student_kwargs = dict(id=sesam_student.nor_edu_person_lin)
        try:
            student = Student.objects.get(**student_kwargs)
        except Student.DoesNotExist:
            # Just instantiate, don't save.
            student = Student(**student_kwargs)

    student.email = sesam_student.email
    student.liu_id = sesam_student.liu_id
    student.liu_lin = sesam_student.liu_lin

    student.full_name = sesam_student.full_name