# Path: account/forms.py
# class SignUpForm(forms.ModelForm):
#     class Meta:
#         model = User
#         fields = ('first_name', 'last_name', 'username', 'email', 'password',)
#         User._meta.get_field('email')._unique = True
#         User._meta.get_field('email').blank = False
#         User._meta.get_field('email').null = False
#         User._meta.get_field('first_name').blank = False
#         User._meta.get_field('first_name').null = False
#         User._meta.get_field('last_name').blank = False
#         User._meta.get_field('last_name').null = False
# 
#     def clean_username(self):
#         username = self.cleaned_data['username']
#         if not re.match(r'^[A-Za-z0-9]+$', username):
#             raise forms.ValidationError('Username does not allow special characters.')
#         return username.lower()
# 
#     def clean_email(self):
#         email = self.cleaned_data['email']
#         return email.lower()
# 
# class LoginForm(forms.Form):
#     identity = forms.CharField(min_length=3, required=True)
#     password = forms.CharField(min_length=3, required=True)
#     keep_connected = forms.BooleanField(initial=False, required=False)
# 
# class SettingsForm(forms.Form):
#     first_name = forms.CharField(min_length=3, required=True)
#     last_name = forms.CharField(min_length=3, required=True)
#     password = forms.CharField(min_length=3, required=False)
#     confirm_password = forms.CharField(min_length=3, required=False)
# 
#     def clean(self):
#         cleaned_data = super(SettingsForm, self).clean()
#         password = cleaned_data.get("password")
#         confirm_password = cleaned_data.get("confirm_password")
#         if password != confirm_password:
#             raise forms.ValidationError('Password confirmation does not equal with password.',)
#         return password
# 
# Path: account/models.py
# class Account(models.Model):
#     user = models.ForeignKey(to='auth.User', related_name='accounts')
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     @property
#     def user_avatar(self):
#         fs = FileSystemStorage()
#         if fs.exists(self.user.username + '.jpg'):
#             return '/static/avatar/' + self.user.username + '.jpg'
#         else:
#             return '/static/img/profile/default.jpg'
# 
#     def is_following(self, follow):
#         return self.following.filter(follow=follow).exists()
# 
#     def unfollow(self, follow):
#         relationship = self.following.filter(follow=follow).first()
#         relationship.delete()
# 
#     @staticmethod
#     def get_by_user(user):
#         return Account.objects.filter(user=user).first()
# 
#     @staticmethod
#     def get_by_username(username):
#         return Account.objects.filter(user__username=username).first()
# 
# class Relationship(models.Model):
#     owner = models.ForeignKey(to='account.Account', related_name='following')
#     follow = models.ForeignKey(to='account.Account', related_name='followers')
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         unique_together = ('owner', 'follow',)
# 
# Path: notification/models.py
# class Notification(models.Model):
#     owner = models.ForeignKey(to='account.Account', related_name='notifications')
#     sender = models.ForeignKey(to='account.Account', related_name='sent_notifications')
#     post = models.ForeignKey(to='link.Post', null=True)
#     type = models.CharField(max_length=50)
#     viewed = models.BooleanField(default=False)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     @staticmethod
#     def check_as_viewed(owner):
#         Notification.objects.filter(owner=owner).update(viewed=True)
# 
#     @staticmethod
#     def add(owner, sender, type, post=None):
#         notify = Notification(owner=owner, sender=sender, type=type.value, post=post)
#         notify.save()
# 
# class NotificationType(Enum):
#     ADD = 'ADD'
#     REACT = 'LIKE'
#     COMMENT = 'COMMENT'
#     FOLLOW = 'FOLLOW'


import os
from django.shortcuts import redirect, render, get_object_or_404
from django.urls import reverse
from django.contrib.auth import login, logout
from django.contrib.auth.models import User
from django.core.files.storage import FileSystemStorage
from django.contrib.auth.decorators import login_required
from account.forms import SignUpForm, LoginForm, SettingsForm
from account.models import Account, Relationship
from notification.models import Notification, NotificationType




class AuthView:
    @staticmethod
    def signup(request):
        form = None
        if request.method == 'POST':
            form = SignUpForm(request.POST)
            if form.is_valid():
                form_user = form.save(commit=False)
                user = User.objects.create_user(username=form_user.username, email=form_user.email,
                                                password=form_user.password,)
                user.first_name = form_user.first_name
                user.last_name = form_user.last_name
                user.save()
                account = Account(user=user)
                account.save()
        return render(request, 'index.html', {'formSignUp': form})

    @staticmethod
    def login(request):
        form = None
        if request.method == 'POST':

# Path: account/forms.py
# class SignUpForm(forms.ModelForm):
#     class Meta:
#         model = User
#         fields = ('first_name', 'last_name', 'username', 'email', 'password',)
#         User._meta.get_field('email')._unique = True
#         User._meta.get_field('email').blank = False
#         User._meta.get_field('email').null = False
#         User._meta.get_field('first_name').blank = False
#         User._meta.get_field('first_name').null = False
#         User._meta.get_field('last_name').blank = False
#         User._meta.get_field('last_name').null = False
# 
#     def clean_username(self):
#         username = self.cleaned_data['username']
#         if not re.match(r'^[A-Za-z0-9]+$', username):
#             raise forms.ValidationError('Username does not allow special characters.')
#         return username.lower()
# 
#     def clean_email(self):
#         email = self.cleaned_data['email']
#         return email.lower()
# 
# class LoginForm(forms.Form):
#     identity = forms.CharField(min_length=3, required=True)
#     password = forms.CharField(min_length=3, required=True)
#     keep_connected = forms.BooleanField(initial=False, required=False)
# 
# class SettingsForm(forms.Form):
#     first_name = forms.CharField(min_length=3, required=True)
#     last_name = forms.CharField(min_length=3, required=True)
#     password = forms.CharField(min_length=3, required=False)
#     confirm_password = forms.CharField(min_length=3, required=False)
# 
#     def clean(self):
#         cleaned_data = super(SettingsForm, self).clean()
#         password = cleaned_data.get("password")
#         confirm_password = cleaned_data.get("confirm_password")
#         if password != confirm_password:
#             raise forms.ValidationError('Password confirmation does not equal with password.',)
#         return password
# 
# Path: account/models.py
# class Account(models.Model):
#     user = models.ForeignKey(to='auth.User', related_name='accounts')
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     @property
#     def user_avatar(self):
#         fs = FileSystemStorage()
#         if fs.exists(self.user.username + '.jpg'):
#             return '/static/avatar/' + self.user.username + '.jpg'
#         else:
#             return '/static/img/profile/default.jpg'
# 
#     def is_following(self, follow):
#         return self.following.filter(follow=follow).exists()
# 
#     def unfollow(self, follow):
#         relationship = self.following.filter(follow=follow).first()
#         relationship.delete()
# 
#     @staticmethod
#     def get_by_user(user):
#         return Account.objects.filter(user=user).first()
# 
#     @staticmethod
#     def get_by_username(username):
#         return Account.objects.filter(user__username=username).first()
# 
# class Relationship(models.Model):
#     owner = models.ForeignKey(to='account.Account', related_name='following')
#     follow = models.ForeignKey(to='account.Account', related_name='followers')
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         unique_together = ('owner', 'follow',)
# 
# Path: notification/models.py
# class Notification(models.Model):
#     owner = models.ForeignKey(to='account.Account', related_name='notifications')
#     sender = models.ForeignKey(to='account.Account', related_name='sent_notifications')
#     post = models.ForeignKey(to='link.Post', null=True)
#     type = models.CharField(max_length=50)
#     viewed = models.BooleanField(default=False)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     @staticmethod
#     def check_as_viewed(owner):
#         Notification.objects.filter(owner=owner).update(viewed=True)
# 
#     @staticmethod
#     def add(owner, sender, type, post=None):
#         notify = Notification(owner=owner, sender=sender, type=type.value, post=post)
#         notify.save()
# 
# class NotificationType(Enum):
#     ADD = 'ADD'
#     REACT = 'LIKE'
#     COMMENT = 'COMMENT'
#     FOLLOW = 'FOLLOW'


# Path: account/views.py
import os
from django.shortcuts import redirect, render, get_object_or_404
from django.urls import reverse
from django.contrib.auth import login, logout
from django.contrib.auth.models import User
from django.core.files.storage import FileSystemStorage
from django.contrib.auth.decorators import login_required
from account.forms import SignUpForm, LoginForm, SettingsForm
from account.models import Account, Relationship
from notification.models import Notification, NotificationType





class AuthView:
    @staticmethod
    def signup(request):
        form = None
        if request.method == 'POST':
            form = SignUpForm(request.POST)
            if form.is_valid():
                form_user = form.save(commit=False)
                user = User.objects.create_user(username=form_user.username, email=form_user.email,
                                                password=form_user.password,)
                user.first_name = form_user.first_name
                user.last_name = form_user.last_name
                user.save()
                account = Account(user=user)
                account.save()
        return render(request, 'index.html', {'formSignUp': form})

    @staticmethod
    def login(request):
        form = None
        if request.method == 'POST':

            form = LoginForm(request.POST)