# Path: core/mixins.py
# class LoginRequiredMixin(object):
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
# 
# class AngularAppMixin(LoginRequiredMixin):
#     def get_website(self):
#         try:
#             website = Website.objects.get(pk=self.kwargs['website_id'])
#         except Website.DoesNotExist:
#             raise Http404('No such website.')
#         if website.user != self.request.user:
#             raise PermissionDenied('You don\'t have permission to view this website.')
#         return website
# 
#     def website_to_json(self, website):
#         return mark_safe(json.dumps(model_to_dict(website, exclude=('timezone', ))))
# 
# Path: core/models.py
# class Website(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     name = models.CharField(max_length=255)
#     url = models.URLField()
#     timezone = TimeZoneField()
# 
#     def __unicode__(self):
#         return self.name
# 
#     def view_count_today(self):
#         today_midnight = timezone.now().astimezone(self.timezone).replace(hour=0, minute=0, second=0)
#         return len(PageView.objects.filter(session__website=self,
#                                            view_timestamp__gt=today_midnight))
# 
# Path: accounts/forms.py
# class EmailUserCreationForm(UserCreationForm):
#     class Meta:
#         model = get_user_model()
#         fields = (model.USERNAME_FIELD, )
# 
#     def __init__(self, *args, **kwargs):
#         super(EmailUserCreationForm, self).__init__(*args, **kwargs)
#         if 'username' not in self.Meta.fields:
#             self.fields.pop('username', None)


import django_select2
from django.http.response import HttpResponseRedirect
from django.contrib import messages
from django.contrib.auth.forms import PasswordChangeForm
from django.templatetags.static import static
from django.views.generic.base import TemplateView
from django.contrib.auth import logout
from django.contrib.auth.views import AuthenticationForm, login
from django.core.urlresolvers import reverse_lazy, reverse
from django.forms.models import modelform_factory
from django.views.generic import FormView, ListView, RedirectView
from core.mixins import LoginRequiredMixin, AngularAppMixin
from core.models import Website
from accounts.forms import EmailUserCreationForm




class LoginView(FormView):
    template_name = 'accounts/login.html'
    form_class = AuthenticationForm

    def get_success_url(self):
        if 'next' in self.request.GET:
            return self.request.GET['next']
        return reverse_lazy('accounts:website_list')

    def form_valid(self, form):
        login(self.request, form.get_user())
        return super(LoginView, self).form_valid(form)


class LogoutView(RedirectView):
    url = reverse_lazy('home')

    def dispatch(self, *args, **kwargs):
        logout(self.request)
        return super(LogoutView, self).dispatch(*args, **kwargs)


class RegisterView(FormView):
    template_name = 'accounts/register.html'

# Path: core/mixins.py
# class LoginRequiredMixin(object):
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
# 
# class AngularAppMixin(LoginRequiredMixin):
#     def get_website(self):
#         try:
#             website = Website.objects.get(pk=self.kwargs['website_id'])
#         except Website.DoesNotExist:
#             raise Http404('No such website.')
#         if website.user != self.request.user:
#             raise PermissionDenied('You don\'t have permission to view this website.')
#         return website
# 
#     def website_to_json(self, website):
#         return mark_safe(json.dumps(model_to_dict(website, exclude=('timezone', ))))
# 
# Path: core/models.py
# class Website(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     name = models.CharField(max_length=255)
#     url = models.URLField()
#     timezone = TimeZoneField()
# 
#     def __unicode__(self):
#         return self.name
# 
#     def view_count_today(self):
#         today_midnight = timezone.now().astimezone(self.timezone).replace(hour=0, minute=0, second=0)
#         return len(PageView.objects.filter(session__website=self,
#                                            view_timestamp__gt=today_midnight))
# 
# Path: accounts/forms.py
# class EmailUserCreationForm(UserCreationForm):
#     class Meta:
#         model = get_user_model()
#         fields = (model.USERNAME_FIELD, )
# 
#     def __init__(self, *args, **kwargs):
#         super(EmailUserCreationForm, self).__init__(*args, **kwargs)
#         if 'username' not in self.Meta.fields:
#             self.fields.pop('username', None)


# Path: accounts/views.py
import django_select2
from django.http.response import HttpResponseRedirect
from django.contrib import messages
from django.contrib.auth.forms import PasswordChangeForm
from django.templatetags.static import static
from django.views.generic.base import TemplateView
from django.contrib.auth import logout
from django.contrib.auth.views import AuthenticationForm, login
from django.core.urlresolvers import reverse_lazy, reverse
from django.forms.models import modelform_factory
from django.views.generic import FormView, ListView, RedirectView
from core.mixins import LoginRequiredMixin, AngularAppMixin
from core.models import Website
from accounts.forms import EmailUserCreationForm





class LoginView(FormView):
    template_name = 'accounts/login.html'
    form_class = AuthenticationForm

    def get_success_url(self):
        if 'next' in self.request.GET:
            return self.request.GET['next']
        return reverse_lazy('accounts:website_list')

    def form_valid(self, form):
        login(self.request, form.get_user())
        return super(LoginView, self).form_valid(form)


class LogoutView(RedirectView):
    url = reverse_lazy('home')

    def dispatch(self, *args, **kwargs):
        logout(self.request)
        return super(LogoutView, self).dispatch(*args, **kwargs)


class RegisterView(FormView):
    template_name = 'accounts/register.html'

    form_class = EmailUserCreationForm