# 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

    def form_valid(self, form):
        form.save()
        messages.success(self.request, 'Password successfully changed.')
        return super(AccountSettingsView, self).form_valid(form)


class WebsiteListView(LoginRequiredMixin, ListView):
    template_name = 'accounts/websites.html'
    context_object_name = 'websites'

    def get_queryset(self):
        return self.request.user.website_set.order_by('pk')


class AddWebsiteView(LoginRequiredMixin, FormView):
    template_name = 'accounts/add_website.html'
    form_class = modelform_factory(Website, exclude=('user', ), widgets={'timezone': django_select2.Select2Widget()})
    success_url = reverse_lazy('accounts:website_list')

    def redirect_to_website_created(self, pk):
        return HttpResponseRedirect(reverse_lazy('accounts:website_created') + '?id={}'.format(pk))

    def form_valid(self, form):
        website = form.save(commit=False)
        website.user = self.request.user
        website.save()
        return self.redirect_to_website_created(website.pk)



# 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


    def form_valid(self, form):
        form.save()
        messages.success(self.request, 'Password successfully changed.')
        return super(AccountSettingsView, self).form_valid(form)


class WebsiteListView(LoginRequiredMixin, ListView):
    template_name = 'accounts/websites.html'
    context_object_name = 'websites'

    def get_queryset(self):
        return self.request.user.website_set.order_by('pk')


class AddWebsiteView(LoginRequiredMixin, FormView):
    template_name = 'accounts/add_website.html'
    form_class = modelform_factory(Website, exclude=('user', ), widgets={'timezone': django_select2.Select2Widget()})
    success_url = reverse_lazy('accounts:website_list')

    def redirect_to_website_created(self, pk):
        return HttpResponseRedirect(reverse_lazy('accounts:website_created') + '?id={}'.format(pk))

    def form_valid(self, form):
        website = form.save(commit=False)
        website.user = self.request.user
        website.save()
        return self.redirect_to_website_created(website.pk)



class EditWebsiteView(AngularAppMixin, FormView):