# Path: accounts/models.py
# class AnalyticsUser(AbstractBaseUser):
#     USERNAME_FIELD = 'email'
#     email = models.EmailField(unique=True)
#     is_active = models.BooleanField(default=True)
# 
#     objects = EmailUserManager()
# 
#     def get_full_name(self):
#         return self.email
# 
#     def get_short_name(self):
#         return self.email
# 
# Path: accounts/views.py
# class RegisterView(FormView):
#     template_name = 'accounts/register.html'
#     form_class = EmailUserCreationForm
#     success_url = reverse_lazy('accounts:login')
# 
#     def form_valid(self, form):
#         form.save()
#         messages.success(self.request, 'Registration successful, you can now sign in.')
#         return super(RegisterView, self).form_valid(form)
# 
# 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 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/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))


from django.core.urlresolvers import reverse
from django.test.client import RequestFactory
from django.test import TestCase
from django.contrib.messages.storage.fallback import FallbackStorage
from accounts.models import AnalyticsUser
from accounts.views import RegisterView, LoginView, AddWebsiteView
from core.models import Website



class AccountsTestCase(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.user = AnalyticsUser.objects.create_user(email='test@test.com', password='password')

    def test_user_registration(self):
        request = self.factory.post(reverse('accounts:register'), {
            'email': 'testemail@gmail.com',
            'password1': 'passwd',
            'password2': 'passwd'
        })
        # RequestFactory doesn't support middlewares, need fallback storage for messages
        setattr(request, 'session', 'session')
        messages = FallbackStorage(request)
        setattr(request, '_messages', messages)

# Path: accounts/models.py
# class AnalyticsUser(AbstractBaseUser):
#     USERNAME_FIELD = 'email'
#     email = models.EmailField(unique=True)
#     is_active = models.BooleanField(default=True)
# 
#     objects = EmailUserManager()
# 
#     def get_full_name(self):
#         return self.email
# 
#     def get_short_name(self):
#         return self.email
# 
# Path: accounts/views.py
# class RegisterView(FormView):
#     template_name = 'accounts/register.html'
#     form_class = EmailUserCreationForm
#     success_url = reverse_lazy('accounts:login')
# 
#     def form_valid(self, form):
#         form.save()
#         messages.success(self.request, 'Registration successful, you can now sign in.')
#         return super(RegisterView, self).form_valid(form)
# 
# 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 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/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/test.py
from django.core.urlresolvers import reverse
from django.test.client import RequestFactory
from django.test import TestCase
from django.contrib.messages.storage.fallback import FallbackStorage
from accounts.models import AnalyticsUser
from accounts.views import RegisterView, LoginView, AddWebsiteView
from core.models import Website




class AccountsTestCase(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.user = AnalyticsUser.objects.create_user(email='test@test.com', password='password')

    def test_user_registration(self):
        request = self.factory.post(reverse('accounts:register'), {
            'email': 'testemail@gmail.com',
            'password1': 'passwd',
            'password2': 'passwd'
        })
        # RequestFactory doesn't support middlewares, need fallback storage for messages
        setattr(request, 'session', 'session')
        messages = FallbackStorage(request)
        setattr(request, '_messages', messages)

        response = RegisterView.as_view()(request)