# Path: course/management/commands/refreshachievements.py
# class Command(BaseCommand):
#     def handle(self, *args, **options):
# def refresh_achievements(course_classes):
# 
# Path: course/forms/forms.py
# class UserCreationForm(UserCreationForm):
#     """
#     A UserCreationForm with optional password inputs.
#     """
# 
#     def __init__(self, *args, **kwargs):
#         super(UserCreationForm, self).__init__(*args, **kwargs)
#         self.fields['password1'].required = False
#         self.fields['password2'].required = False
#         # If one field gets autocompleted but not the other, our 'neither
#         # password or both password' validation will be triggered.
#         self.fields['password1'].widget.attrs['autocomplete'] = 'off'
#         self.fields['password2'].widget.attrs['autocomplete'] = 'off'
# 
#     def clean_password2(self):
#         password1 = self.cleaned_data.get("password1")
#         password2 = self.cleaned_data.get("password2")
#         if password1 and password2 and password1 != password2:
#             raise forms.ValidationError(
#                 self.error_messages['password_mismatch'],
#                 code='password_mismatch',
#             )
# 
#         if bool(password1) ^ bool(password2):
#             raise forms.ValidationError("Fill out both fields")
# 
#         return password2
# 
# class CaptchaPasswordResetForm(PasswordResetForm):
# 	captcha = (
# 		ReCaptchaField() 
# 		if settings.RECAPTCHA_PUBLIC_KEY != '' and settings.RECAPTCHA_PRIVATE_KEY != ''
# 		else None
# 	)
# 
# 	def __init__(self, *args, **kwargs):
# 		super().__init__(*args, **kwargs)
# 		self.fields['email'].widget.attrs.update({'autofocus': 'autofocus'})
# 
# 	def get_users(self, email):
# 		# removed check verifying if password is unusable
# 		user_model = get_user_model()
# 		active_users = user_model._default_manager.filter(**{
#             '%s__iexact' % user_model.get_email_field_name(): email,
#             'is_active': True,
#         })
# 
# 		return active_users


from django.contrib import admin
from django.apps import apps
from course.management.commands import refreshachievements
from .models import *
from .forms.forms import UserCreationForm, CaptchaPasswordResetForm
from django.forms import BaseInlineFormSet, ModelForm
from django.forms.widgets import TextInput
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ungettext
from django.utils.formats import date_format
import markdown2
            else:
                copy_number += 1
        new_course_class.save()
        
        # Duplicate assignment tasks from original course class
        existing_assignment_tasks = AssignmentTask.objects.filter(course_class=course_class).order_by('id')
        for existing_assignment_task in existing_assignment_tasks:
            new_assignment_task = existing_assignment_task
            new_assignment_task.id = None
            new_assignment_task.course_class = new_course_class
            new_assignment_task.save()

        # Duplicate badges tasks from original course class
        class_badges = ClassBadge.objects.filter(course_class=course_class).order_by('id')
        for class_badge in class_badges:
            criteria_list = ClassBadgeCriteria.objects.filter(class_badge=class_badge).order_by('id') # this line must come before we change the id in class_badge

            class_badge.id = None
            class_badge.course_class = new_course_class
            class_badge.save()

            for criteria in criteria_list:
                criteria.id = None
                criteria.class_badge = class_badge
                criteria.save()

            
duplicate_course_class.short_description = _("Duplicate course class")

def refresh_achievements(modeladmin, request, queryset):

# Path: course/management/commands/refreshachievements.py
# class Command(BaseCommand):
#     def handle(self, *args, **options):
# def refresh_achievements(course_classes):
# 
# Path: course/forms/forms.py
# class UserCreationForm(UserCreationForm):
#     """
#     A UserCreationForm with optional password inputs.
#     """
# 
#     def __init__(self, *args, **kwargs):
#         super(UserCreationForm, self).__init__(*args, **kwargs)
#         self.fields['password1'].required = False
#         self.fields['password2'].required = False
#         # If one field gets autocompleted but not the other, our 'neither
#         # password or both password' validation will be triggered.
#         self.fields['password1'].widget.attrs['autocomplete'] = 'off'
#         self.fields['password2'].widget.attrs['autocomplete'] = 'off'
# 
#     def clean_password2(self):
#         password1 = self.cleaned_data.get("password1")
#         password2 = self.cleaned_data.get("password2")
#         if password1 and password2 and password1 != password2:
#             raise forms.ValidationError(
#                 self.error_messages['password_mismatch'],
#                 code='password_mismatch',
#             )
# 
#         if bool(password1) ^ bool(password2):
#             raise forms.ValidationError("Fill out both fields")
# 
#         return password2
# 
# class CaptchaPasswordResetForm(PasswordResetForm):
# 	captcha = (
# 		ReCaptchaField() 
# 		if settings.RECAPTCHA_PUBLIC_KEY != '' and settings.RECAPTCHA_PRIVATE_KEY != ''
# 		else None
# 	)
# 
# 	def __init__(self, *args, **kwargs):
# 		super().__init__(*args, **kwargs)
# 		self.fields['email'].widget.attrs.update({'autofocus': 'autofocus'})
# 
# 	def get_users(self, email):
# 		# removed check verifying if password is unusable
# 		user_model = get_user_model()
# 		active_users = user_model._default_manager.filter(**{
#             '%s__iexact' % user_model.get_email_field_name(): email,
#             'is_active': True,
#         })
# 
# 		return active_users


# Path: course/admin.py
from django.contrib import admin
from django.apps import apps
from course.management.commands import refreshachievements
from .models import *
from .forms.forms import UserCreationForm, CaptchaPasswordResetForm
from django.forms import BaseInlineFormSet, ModelForm
from django.forms.widgets import TextInput
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ungettext
from django.utils.formats import date_format
import markdown2

            else:
                copy_number += 1
        new_course_class.save()
        
        # Duplicate assignment tasks from original course class
        existing_assignment_tasks = AssignmentTask.objects.filter(course_class=course_class).order_by('id')
        for existing_assignment_task in existing_assignment_tasks:
            new_assignment_task = existing_assignment_task
            new_assignment_task.id = None
            new_assignment_task.course_class = new_course_class
            new_assignment_task.save()

        # Duplicate badges tasks from original course class
        class_badges = ClassBadge.objects.filter(course_class=course_class).order_by('id')
        for class_badge in class_badges:
            criteria_list = ClassBadgeCriteria.objects.filter(class_badge=class_badge).order_by('id') # this line must come before we change the id in class_badge

            class_badge.id = None
            class_badge.course_class = new_course_class
            class_badge.save()

            for criteria in criteria_list:
                criteria.id = None
                criteria.class_badge = class_badge
                criteria.save()

            
duplicate_course_class.short_description = _("Duplicate course class")

def refresh_achievements(modeladmin, request, queryset):

    refreshachievements.refresh_achievements(queryset)