# Path: letsmeet/users/models.py
# class UserProfile(TimeStampedModel):
#     user = models.OneToOneField('auth.User', unique=True, on_delete=models.CASCADE)
#     avatar = StdImageField(
#         upload_to='avatars',
#         variations={
#             'thumbnail': (400, 400, True),
#             'mini': (200, 200, True),
#             'tiny': (100, 100, True),
#             'micro': (40, 40, True),
#         },
#         help_text='Image should be square. Otherwise it will be cropped.'
#     )
#     notify_on_new_event = models.BooleanField(default=True)
#     notify_on_new_subscription = models.BooleanField(default=True)
#     notify_on_new_rsvp_for_organizer = models.BooleanField(default=True)
#     notify_on_new_rsvp_for_attending = models.BooleanField(default=True)
#     notify_on_new_comment = models.BooleanField(default=True)
#     pending_email_address = models.EmailField(default='')
#     personal_ical_uuid = models.UUIDField(default=uuid.uuid4, unique=True)
# 
#     def get_next_event(self):
#         return self.get_upcoming_events().order_by('begin').first()
# 
#     def get_upcoming_events(self):
#         return Event.objects.upcoming().filter(community__in=self.user.communities.all().values_list('id', flat=True))
# 
#     def get_next_yes_event(self):
#         return self.get_upcoming_yes_events().order_by('begin').first()
# 
#     def get_upcoming_yes_events(self):
#         return Event.objects.upcoming().filter(
#             pk__in=EventRSVP.objects.filter(user=self.user, coming=True).values_list('event__pk', flat=True))
# 
#     def get_communitysubscriptions(self):
#         return CommunitySubscription.objects.exclude(community__is_deleted=True).filter(user=self.user)
# 
#     @staticmethod
#     def get_absolute_url():
#         return reverse('profile')
# 
#     def __str__(self):
#         return 'Profile of {}'.format(self.user.username)


import hashlib
import rules
from django.conf import settings
from django.contrib import messages
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.urls import reverse, reverse_lazy
from django.http.response import Http404
from django.shortcuts import redirect
from django.utils.http import urlquote
from django.views.generic.detail import DetailView
from django.views.generic.edit import UpdateView
from django.views.generic.base import View
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from rules.contrib.views import PermissionRequiredMixin
from social_django.models import UserSocialAuth
from .models import UserProfile





class HomeView(DetailView):
    def get_object(self, queryset=None):
        if self.request.user.is_authenticated:
            up, created = UserProfile.objects.get_or_create(user=self.request.user)
            return up

        return None

    def get_template_names(self):
        if self.request.user.is_authenticated:
            return 'users/dashboard.html'

        return 'main/home.html'


class UserProfileView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
    template_name = "users/profile.html"

# Path: letsmeet/users/models.py
# class UserProfile(TimeStampedModel):
#     user = models.OneToOneField('auth.User', unique=True, on_delete=models.CASCADE)
#     avatar = StdImageField(
#         upload_to='avatars',
#         variations={
#             'thumbnail': (400, 400, True),
#             'mini': (200, 200, True),
#             'tiny': (100, 100, True),
#             'micro': (40, 40, True),
#         },
#         help_text='Image should be square. Otherwise it will be cropped.'
#     )
#     notify_on_new_event = models.BooleanField(default=True)
#     notify_on_new_subscription = models.BooleanField(default=True)
#     notify_on_new_rsvp_for_organizer = models.BooleanField(default=True)
#     notify_on_new_rsvp_for_attending = models.BooleanField(default=True)
#     notify_on_new_comment = models.BooleanField(default=True)
#     pending_email_address = models.EmailField(default='')
#     personal_ical_uuid = models.UUIDField(default=uuid.uuid4, unique=True)
# 
#     def get_next_event(self):
#         return self.get_upcoming_events().order_by('begin').first()
# 
#     def get_upcoming_events(self):
#         return Event.objects.upcoming().filter(community__in=self.user.communities.all().values_list('id', flat=True))
# 
#     def get_next_yes_event(self):
#         return self.get_upcoming_yes_events().order_by('begin').first()
# 
#     def get_upcoming_yes_events(self):
#         return Event.objects.upcoming().filter(
#             pk__in=EventRSVP.objects.filter(user=self.user, coming=True).values_list('event__pk', flat=True))
# 
#     def get_communitysubscriptions(self):
#         return CommunitySubscription.objects.exclude(community__is_deleted=True).filter(user=self.user)
# 
#     @staticmethod
#     def get_absolute_url():
#         return reverse('profile')
# 
#     def __str__(self):
#         return 'Profile of {}'.format(self.user.username)


# Path: letsmeet/users/views.py
import hashlib
import rules
from django.conf import settings
from django.contrib import messages
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.urls import reverse, reverse_lazy
from django.http.response import Http404
from django.shortcuts import redirect
from django.utils.http import urlquote
from django.views.generic.detail import DetailView
from django.views.generic.edit import UpdateView
from django.views.generic.base import View
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from rules.contrib.views import PermissionRequiredMixin
from social_django.models import UserSocialAuth
from .models import UserProfile






class HomeView(DetailView):
    def get_object(self, queryset=None):
        if self.request.user.is_authenticated:
            up, created = UserProfile.objects.get_or_create(user=self.request.user)
            return up

        return None

    def get_template_names(self):
        if self.request.user.is_authenticated:
            return 'users/dashboard.html'

        return 'main/home.html'


class UserProfileView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
    template_name = "users/profile.html"

    model = UserProfile