# Path: emaula/school/forms.py
# class ProfessorForm(forms.ModelForm):
#     password = forms.CharField(widget=forms.PasswordInput())
# 
#     class Meta:
#         model = User
#         fields = ('username', 'first_name', 'last_name', 'email', 'password')
# 
# class ProfileForm(forms.ModelForm):
#     class Meta:
#         model = Professor
#         fields = ('picture', )
# 
# Path: emaula/school/models.py
# class Classroom(models.Model):
# 
#     """ Modelo com os conteúdos da aula, como textos, links, imagens, etc. """
# 
#     # TODO: Opção de deixar ou não pública a aula
#     subject = models.ManyToManyField(Subject,
#                                      help_text='Disciplina')
#     title = models.CharField(max_length=255,
#                              help_text='Tema ou título da aula',
#                              blank=True)
#     year = models.ManyToManyField(Year,
#                                   help_text='Selecione \
#                                   um ano ou série para esta aula')
#     summary = models.TextField(max_length=1000,
#                                help_text='Escreva um breve sumário da aula.',
#                                blank=True)
# 
#     author = models.ForeignKey('Professor',
#                                related_name='classroom',
#                                blank=True,
#                                on_delete=models.SET_NULL,
#                                null=True)
#     created_date = models.DateField(default=timezone.now,
#                                     help_text='Data de criação')
#     published_date = models.DateField(blank=True,
#                                       null=True,
#                                       help_text='Data de publicação')
#     views = models.IntegerField(default=0, help_text='Visitas')
# 
#     def __str__(self):
#         return '{0}'.format(self.title)
# 
#     def publish(self):
#         """ Método para tornar pública uma aula. """
#         self.published_date = timezone.now()
#         self.save()
# 
#     def get_absolute_url(self):
#         """ Acessa uma aula específica através da interface administrativa. """
#         return reverse('school:classroom_detail', args=[str(self.id)])
# 
#     def display_subject(self):
#         return ', '.join(
#             [subject.subject for subject in self.subject.all()])
# 
#     def display_year(self):
#         return ', '.join(
#             [year.year for year in self.year.all()[:3]])
# 
#     def display_author(self):
#         return ', '.join(
#             [author.author for author in self.author.all()[:3]])
# 
#     def display_text(self):
#         object_texts = dict()
#         for text in self.text.all():
#             object_texts['title'] = text.title
#             object_texts['text'] = text.text
#             object_texts['source'] = text.source
#         return object_texts
# 
#     def display_link(self):
#         return ', '.join(
#             [link.link for link in self.objects.link.all()])
# 
#     def display_image(self):
#         object_images = dict()
#         for image in self.image.all():
#             object_images['image_path'] = image.image_path
#             object_images['alt'] = image.alt
#         return object_images
# 
#     display_author.short_description = 'Author'
#     display_subject.short_description = 'Subject'
#     display_year.short_description = 'Year'
#     display_text.short_description = 'Text'
#     display_link.short_description = 'Link'
#     display_link.short_description = 'Image'
# 
# class Professor(models.Model):
# 
#     """ Modelo para o professor que criou a aula. """
# 
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     picture = models.ImageField(upload_to='media',
#                                 default='pic_folder/user/no-img.jpg')
#     first_name = models.CharField(max_length=255,
#                                   unique=False,
#                                   help_text='Primeiro nome')
#     last_name = models.CharField(max_length=255,
#                                  unique=False,
#                                  help_text='Último nome')
# 
#     def __str__(self):
#         if self.user.first_name != '':
#             return self.user.first_name.capitalize()
#         else:
#             return self.user.username.capitalize()
# 
#     def get_absolute_url(self):
#         """ Acessa uma professora específica através da interface administrativa. """
#         return reverse('school:professor_detail', args=[str(self.id)])


from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.views import generic
from django.contrib.auth.models import Permission, User
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from .forms import ProfessorForm, ProfileForm
from .models import Classroom, Professor
                      'all_professors_all_classes': all_professors_all_classes,
                      'all_professors_all_published_classes':
                      all_professors_all_published_classes})


def register(request):
    registered = False
    if request.method == 'POST':
        user_form = ProfessorForm(data=request.POST)
        profile_form = ProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.is_staff = True
            user.is_superuser = True
            g, created = Group.objects.get_or_create(name='school_staff')
            g.user_set.add(user)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user

            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']
            profile.save()

            registered = True
        else:
            print(user_form.errors, profile_form.errors)
    else:

# Path: emaula/school/forms.py
# class ProfessorForm(forms.ModelForm):
#     password = forms.CharField(widget=forms.PasswordInput())
# 
#     class Meta:
#         model = User
#         fields = ('username', 'first_name', 'last_name', 'email', 'password')
# 
# class ProfileForm(forms.ModelForm):
#     class Meta:
#         model = Professor
#         fields = ('picture', )
# 
# Path: emaula/school/models.py
# class Classroom(models.Model):
# 
#     """ Modelo com os conteúdos da aula, como textos, links, imagens, etc. """
# 
#     # TODO: Opção de deixar ou não pública a aula
#     subject = models.ManyToManyField(Subject,
#                                      help_text='Disciplina')
#     title = models.CharField(max_length=255,
#                              help_text='Tema ou título da aula',
#                              blank=True)
#     year = models.ManyToManyField(Year,
#                                   help_text='Selecione \
#                                   um ano ou série para esta aula')
#     summary = models.TextField(max_length=1000,
#                                help_text='Escreva um breve sumário da aula.',
#                                blank=True)
# 
#     author = models.ForeignKey('Professor',
#                                related_name='classroom',
#                                blank=True,
#                                on_delete=models.SET_NULL,
#                                null=True)
#     created_date = models.DateField(default=timezone.now,
#                                     help_text='Data de criação')
#     published_date = models.DateField(blank=True,
#                                       null=True,
#                                       help_text='Data de publicação')
#     views = models.IntegerField(default=0, help_text='Visitas')
# 
#     def __str__(self):
#         return '{0}'.format(self.title)
# 
#     def publish(self):
#         """ Método para tornar pública uma aula. """
#         self.published_date = timezone.now()
#         self.save()
# 
#     def get_absolute_url(self):
#         """ Acessa uma aula específica através da interface administrativa. """
#         return reverse('school:classroom_detail', args=[str(self.id)])
# 
#     def display_subject(self):
#         return ', '.join(
#             [subject.subject for subject in self.subject.all()])
# 
#     def display_year(self):
#         return ', '.join(
#             [year.year for year in self.year.all()[:3]])
# 
#     def display_author(self):
#         return ', '.join(
#             [author.author for author in self.author.all()[:3]])
# 
#     def display_text(self):
#         object_texts = dict()
#         for text in self.text.all():
#             object_texts['title'] = text.title
#             object_texts['text'] = text.text
#             object_texts['source'] = text.source
#         return object_texts
# 
#     def display_link(self):
#         return ', '.join(
#             [link.link for link in self.objects.link.all()])
# 
#     def display_image(self):
#         object_images = dict()
#         for image in self.image.all():
#             object_images['image_path'] = image.image_path
#             object_images['alt'] = image.alt
#         return object_images
# 
#     display_author.short_description = 'Author'
#     display_subject.short_description = 'Subject'
#     display_year.short_description = 'Year'
#     display_text.short_description = 'Text'
#     display_link.short_description = 'Link'
#     display_link.short_description = 'Image'
# 
# class Professor(models.Model):
# 
#     """ Modelo para o professor que criou a aula. """
# 
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     picture = models.ImageField(upload_to='media',
#                                 default='pic_folder/user/no-img.jpg')
#     first_name = models.CharField(max_length=255,
#                                   unique=False,
#                                   help_text='Primeiro nome')
#     last_name = models.CharField(max_length=255,
#                                  unique=False,
#                                  help_text='Último nome')
# 
#     def __str__(self):
#         if self.user.first_name != '':
#             return self.user.first_name.capitalize()
#         else:
#             return self.user.username.capitalize()
# 
#     def get_absolute_url(self):
#         """ Acessa uma professora específica através da interface administrativa. """
#         return reverse('school:professor_detail', args=[str(self.id)])


# Path: emaula/school/views.py
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.views import generic
from django.contrib.auth.models import Permission, User
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from .forms import ProfessorForm, ProfileForm
from .models import Classroom, Professor

                      'all_professors_all_classes': all_professors_all_classes,
                      'all_professors_all_published_classes':
                      all_professors_all_published_classes})


def register(request):
    registered = False
    if request.method == 'POST':
        user_form = ProfessorForm(data=request.POST)
        profile_form = ProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.is_staff = True
            user.is_superuser = True
            g, created = Group.objects.get_or_create(name='school_staff')
            g.user_set.add(user)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user

            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']
            profile.save()

            registered = True
        else:
            print(user_form.errors, profile_form.errors)
    else:

        user_form = ProfessorForm()