# Path: accounts/forms.py
# class LoginForm(forms.Form):
#     username = forms.CharField(
#         required=True,
#         label=False,
#         error_messages={"required": "Please type username"},
#         widget=forms.TextInput(
#             attrs={
#                 "placeholder": "Username / Project name",
#                 "class": "form-control",
#             }
#         ),
#     )
#     password = forms.CharField(
#         required=True,
#         label=False,
#         error_messages={"required": "Please type your password"},
#         widget=forms.PasswordInput(
#             attrs={
#                 "placeholder": "Password",
#                 "class": "form-control",
#             }
#         )
#     )
# 
# class PasswordChangeForm(forms.Form):
#     old_password = forms.CharField(
#         required=True,
#         label="Raw Password",
#         error_messages={"required": "Please type your raw password"},
#         widget=forms.PasswordInput(
#             attrs={
#                 "placeholder": "Raw password",
#             }
#         )
#     )
#     new_password_1 = forms.CharField(
#         required=True,
#         label="New Password",
#         error_messages={"required": "Please type your new password"},
#         widget=forms.PasswordInput(
#             attrs={
#                 "placeholder": "New password",
#             }
#         )
#     )
#     new_password_2 = forms.CharField(
#         required=True,
#         label="Confirm Password",
#         error_messages={"required": "Please confirm new password"},
#         widget=forms.PasswordInput(
#             attrs={
#                 "placeholder": "Confirm new password",
#             }
#         )
#     )
# 
# class UserRegisterForm(forms.ModelForm):
#     password = forms.CharField(
#         required=True,
#         label=False,
#         widget=forms.PasswordInput(
#             attrs={
#                 "class": "form-control",
#                 "placeholder": "Password",
#             }
#         )
#     )
#     password_2 = forms.CharField(
#         required=True,
#         label=False,
#         widget=forms.PasswordInput(
#             attrs={
#                 "class": 'form-control',
#                 "placeholder": "Confirm password",
#             }
#         )
#     )
# 
#     class Meta:
#         model = User
#         fields = ("username", "first_name", "last_name", "email")
#         widgets = {
#             "username": forms.TextInput(attrs={"class": "form-control", "placeholder": "Username / Project name"}),
#             "first_name": forms.TextInput(attrs={"class": "form-control", "placeholder": "First name"}),
#             "last_name": forms.TextInput(attrs={"class": "form-control", "placeholder": "Last name"}),
#             "email": forms.EmailInput(attrs={"class": "form-control", "placeholder": "E-mail"}),
#         }
#         labels = {
#             "username": False,
#             "first_name": False,
#             "last_name": False,
#             "email": False,
#         }
#         help_texts = {
#             "username": False,
#             "first_name": False,
#             "last_name": False,
#         }
# 
#     def clean_password2(self):
#         cd = self.cleaned_data
#         if cd["password"] != cd["password_2"]:
#             raise forms.ValidationError('Password doesn\'t match.')
#         return cd["password_2"]
# 
# Path: ui/tools.py
# def success(message, jump_url='.', msg_title="success", status=1, wait_second=1):
#     json_data = dict()
#     json_data['msg_title'] = msg_title
#     json_data['info'] = message
#     json_data['url'] = jump_url
#     json_data['status'] = status
#     json_data['wait_second'] = wait_second
#     return JsonResponse(json_data)
# 
# def error(message, jump_url='.', msg_title="error", status=0, wait_second=3):
#     json_data = dict()
#     json_data['msg_title'] = msg_title
#     json_data['info'] = str(message)
#     json_data['url'] = jump_url
#     json_data['status'] = status
#     json_data['wait_second'] = wait_second
#     return JsonResponse(json_data)


from django.shortcuts import render
from .forms import LoginForm, PasswordChangeForm, UserRegisterForm
from django.contrib.auth import authenticate, login, update_session_auth_hash
from ui.tools import success, error
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group


def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = authenticate(username=cd['username'], password=cd['password'])

            if user is not None:
                if user.is_active:
                    login(request, user)
                    if "next" in request.POST.keys():
                        red_url = request.POST["next"]
                        if red_url == "":
                            red_url = "/ui"
                    else:
                        red_url = "/ui"

# Path: accounts/forms.py
# class LoginForm(forms.Form):
#     username = forms.CharField(
#         required=True,
#         label=False,
#         error_messages={"required": "Please type username"},
#         widget=forms.TextInput(
#             attrs={
#                 "placeholder": "Username / Project name",
#                 "class": "form-control",
#             }
#         ),
#     )
#     password = forms.CharField(
#         required=True,
#         label=False,
#         error_messages={"required": "Please type your password"},
#         widget=forms.PasswordInput(
#             attrs={
#                 "placeholder": "Password",
#                 "class": "form-control",
#             }
#         )
#     )
# 
# class PasswordChangeForm(forms.Form):
#     old_password = forms.CharField(
#         required=True,
#         label="Raw Password",
#         error_messages={"required": "Please type your raw password"},
#         widget=forms.PasswordInput(
#             attrs={
#                 "placeholder": "Raw password",
#             }
#         )
#     )
#     new_password_1 = forms.CharField(
#         required=True,
#         label="New Password",
#         error_messages={"required": "Please type your new password"},
#         widget=forms.PasswordInput(
#             attrs={
#                 "placeholder": "New password",
#             }
#         )
#     )
#     new_password_2 = forms.CharField(
#         required=True,
#         label="Confirm Password",
#         error_messages={"required": "Please confirm new password"},
#         widget=forms.PasswordInput(
#             attrs={
#                 "placeholder": "Confirm new password",
#             }
#         )
#     )
# 
# class UserRegisterForm(forms.ModelForm):
#     password = forms.CharField(
#         required=True,
#         label=False,
#         widget=forms.PasswordInput(
#             attrs={
#                 "class": "form-control",
#                 "placeholder": "Password",
#             }
#         )
#     )
#     password_2 = forms.CharField(
#         required=True,
#         label=False,
#         widget=forms.PasswordInput(
#             attrs={
#                 "class": 'form-control',
#                 "placeholder": "Confirm password",
#             }
#         )
#     )
# 
#     class Meta:
#         model = User
#         fields = ("username", "first_name", "last_name", "email")
#         widgets = {
#             "username": forms.TextInput(attrs={"class": "form-control", "placeholder": "Username / Project name"}),
#             "first_name": forms.TextInput(attrs={"class": "form-control", "placeholder": "First name"}),
#             "last_name": forms.TextInput(attrs={"class": "form-control", "placeholder": "Last name"}),
#             "email": forms.EmailInput(attrs={"class": "form-control", "placeholder": "E-mail"}),
#         }
#         labels = {
#             "username": False,
#             "first_name": False,
#             "last_name": False,
#             "email": False,
#         }
#         help_texts = {
#             "username": False,
#             "first_name": False,
#             "last_name": False,
#         }
# 
#     def clean_password2(self):
#         cd = self.cleaned_data
#         if cd["password"] != cd["password_2"]:
#             raise forms.ValidationError('Password doesn\'t match.')
#         return cd["password_2"]
# 
# Path: ui/tools.py
# def success(message, jump_url='.', msg_title="success", status=1, wait_second=1):
#     json_data = dict()
#     json_data['msg_title'] = msg_title
#     json_data['info'] = message
#     json_data['url'] = jump_url
#     json_data['status'] = status
#     json_data['wait_second'] = wait_second
#     return JsonResponse(json_data)
# 
# def error(message, jump_url='.', msg_title="error", status=0, wait_second=3):
#     json_data = dict()
#     json_data['msg_title'] = msg_title
#     json_data['info'] = str(message)
#     json_data['url'] = jump_url
#     json_data['status'] = status
#     json_data['wait_second'] = wait_second
#     return JsonResponse(json_data)


# Path: accounts/views.py
from django.shortcuts import render
from .forms import LoginForm, PasswordChangeForm, UserRegisterForm
from django.contrib.auth import authenticate, login, update_session_auth_hash
from ui.tools import success, error
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group



def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = authenticate(username=cd['username'], password=cd['password'])

            if user is not None:
                if user.is_active:
                    login(request, user)
                    if "next" in request.POST.keys():
                        red_url = request.POST["next"]
                        if red_url == "":
                            red_url = "/ui"
                    else:
                        red_url = "/ui"

                    return success('Authenticated successfully', red_url)