# 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


def invite_user(modeladmin, request, queryset):
    for user in queryset:
        if user.has_usable_password():
            messages.error(
                request,
                _("User %(username)s already has a password!") % {'username': user.username}
            )
            return
        elif user.email == '':
            messages.error(
                request,
                _("User %(username)s has no email!") % {'username': user.username }
            )
            return

    for user in 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



def invite_user(modeladmin, request, queryset):
    for user in queryset:
        if user.has_usable_password():
            messages.error(
                request,
                _("User %(username)s already has a password!") % {'username': user.username}
            )
            return
        elif user.email == '':
            messages.error(
                request,
                _("User %(username)s has no email!") % {'username': user.username }
            )
            return

    for user in queryset:

        reset_form = CaptchaPasswordResetForm({'email': user.email})