# Path: horarios/models.py
# 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 django.db.models import Count
from django.db import DatabaseError
from horarios.models import Subject, Group, Profession
from django.core.management.base import BaseCommand
import re



class Command(BaseCommand):

    def handle(self, *args, **options):
        dupes = Subject.objects.values("name").annotate(count=Count("code")).order_by().filter(count__gt=1)
        pattern = re.compile(r' \| ')
        for item in dupes:

# Path: horarios/models.py
# 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/management/commands/remove_dupe_names.py
from django.db.models import Count
from django.db import DatabaseError
from horarios.models import Subject, Group, Profession
from django.core.management.base import BaseCommand
import re




class Command(BaseCommand):

    def handle(self, *args, **options):
        dupes = Subject.objects.values("name").annotate(count=Count("code")).order_by().filter(count__gt=1)
        pattern = re.compile(r' \| ')
        for item in dupes:

            subjects = Subject.objects.filter(name__iexact=item["name"])