# Path: horarios/models.py
# class Session(models.Model):
#     url  = models.CharField(primary_key=True, max_length=10, default=make_uuid)
#     session = models.CharField(max_length=500000)
# 
# class Subject(models.Model):
#     name = models.CharField(max_length=200)
#     code = models.IntegerField(primary_key=True)
#     credits = models.IntegerField()
#     stype = models.CharField(max_length=10)
#     @staticmethod
#     def autocomplete(search_term="", profession="", subject_type=[]):
#         print "Search"
#         print search_term
#         print "profession"
#         print profession == ""
#         print "stype"
#         print subject_type
#         if search_term == "":
#             if profession == "":
#                 return Subject.objects.all()
#             else:
#                 query = Subject.objects.filter(
#                     groups__in=Group.objects.filter(professions__code=profession))
#                 if len(subject_type) != 0:
#                     query = query.filter(stype__in=subject_type)
#                 return query.distinct()
#         else:
#             search_term = sanitize_search_term(search_term)
#             query = Subject.objects.extra(
#                     where=["name @@ to_tsquery('sp', %s)"], params=[search_term])
#             if profession != "":
#                 query = query.filter(groups__in=Group.objects.filter(professions__code=profession))
#             if len(subject_type) != 0:
#                 query = query.filter(stype__in=subject_type)
#             return query.distinct()
# 
# class Group(models.Model):
#     schedule = models.CommaSeparatedIntegerField(max_length=100)
#     code = models.IntegerField()
#     teacher = models.ForeignKey(Teacher, related_name='groups')
#     subject = models.ForeignKey(Subject, related_name='groups')
#     professions = models.ManyToManyField(Profession)
# 
# class Profession(models.Model):
#     name = models.CharField(max_length=100)
#     code = models.CharField(primary_key=True, max_length=20)


from horarios.models import Session, Subject, Group, Profession
from rest_framework import serializers
import json

class ProfessionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profession

class SessionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Session

class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
    def to_representation(self, group):
        return {
            "id": group.id,
            "teacher": group.teacher.name,
            "schedule": json.loads(group.schedule),
            "code": group.code,
            "subject": group.subject.code,
            "professions": [p.code for p in group.professions.all()]
        }

class SubjectSerializer(serializers.ModelSerializer):
    groups = GroupSerializer(many=True, read_only=True)
    class Meta:
        model =  Subject


class SubjectSimpleSerializer(serializers.ModelSerializer):
    class Meta:

# Path: horarios/models.py
# class Session(models.Model):
#     url  = models.CharField(primary_key=True, max_length=10, default=make_uuid)
#     session = models.CharField(max_length=500000)
# 
# class Subject(models.Model):
#     name = models.CharField(max_length=200)
#     code = models.IntegerField(primary_key=True)
#     credits = models.IntegerField()
#     stype = models.CharField(max_length=10)
#     @staticmethod
#     def autocomplete(search_term="", profession="", subject_type=[]):
#         print "Search"
#         print search_term
#         print "profession"
#         print profession == ""
#         print "stype"
#         print subject_type
#         if search_term == "":
#             if profession == "":
#                 return Subject.objects.all()
#             else:
#                 query = Subject.objects.filter(
#                     groups__in=Group.objects.filter(professions__code=profession))
#                 if len(subject_type) != 0:
#                     query = query.filter(stype__in=subject_type)
#                 return query.distinct()
#         else:
#             search_term = sanitize_search_term(search_term)
#             query = Subject.objects.extra(
#                     where=["name @@ to_tsquery('sp', %s)"], params=[search_term])
#             if profession != "":
#                 query = query.filter(groups__in=Group.objects.filter(professions__code=profession))
#             if len(subject_type) != 0:
#                 query = query.filter(stype__in=subject_type)
#             return query.distinct()
# 
# class Group(models.Model):
#     schedule = models.CommaSeparatedIntegerField(max_length=100)
#     code = models.IntegerField()
#     teacher = models.ForeignKey(Teacher, related_name='groups')
#     subject = models.ForeignKey(Subject, related_name='groups')
#     professions = models.ManyToManyField(Profession)
# 
# class Profession(models.Model):
#     name = models.CharField(max_length=100)
#     code = models.CharField(primary_key=True, max_length=20)


# Path: horarios/serializers.py
from horarios.models import Session, Subject, Group, Profession
from rest_framework import serializers
import json


class ProfessionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profession

class SessionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Session

class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
    def to_representation(self, group):
        return {
            "id": group.id,
            "teacher": group.teacher.name,
            "schedule": json.loads(group.schedule),
            "code": group.code,
            "subject": group.subject.code,
            "professions": [p.code for p in group.professions.all()]
        }

class SubjectSerializer(serializers.ModelSerializer):
    groups = GroupSerializer(many=True, read_only=True)
    class Meta:
        model =  Subject


class SubjectSimpleSerializer(serializers.ModelSerializer):
    class Meta:

        model =  Subject