Skip to content

Commit

Permalink
Merge pull request #128 from crypotex/feature/120
Browse files Browse the repository at this point in the history
Feature/120 into develop.
  • Loading branch information
tsudmi committed Dec 3, 2015
2 parents a91753a + 8947e9b commit 6592af2
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 3 deletions.
2 changes: 2 additions & 0 deletions taas/user/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class UserAdmin(auth_admin.UserAdmin):
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'budget')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
(_('Pins'), {'fields': ('pin', 'button_id')})
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('first_name', 'last_name', 'email', 'phone_number', 'password1', 'password2'),
}),
)
readonly_fields = ('pin', 'last_login', 'date_joined')
list_display = ('email', 'first_name', 'last_name', 'is_active')
list_filter = ('is_staff', 'is_active')
search_fields = ('first_name', 'last_name')
Expand Down
8 changes: 7 additions & 1 deletion taas/user/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ class UserConfig(django.apps.AppConfig):
def ready(self):
User = self.get_model('User')

db_signals.pre_save.connect(
handlers.check_user_activation,
sender=User,
dispatch_uid='taas.user.handlers.check_user_activation',
)

db_signals.post_save.connect(
handlers.send_emails_to_users,
sender=User,
dispatch_uid='taas.user.handlers.send_emails_to_users',
dispatch_uid='taas.user.handlers.send_emails_to_users'
)

auth_signals.user_logged_in.connect(
Expand Down
11 changes: 10 additions & 1 deletion taas/user/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

logger = logging.getLogger(__name__)


def send_emails_to_users(sender, instance=None, created=False, **kwargs):
if created:
instance.email_user_on_registration()
Expand All @@ -20,6 +19,16 @@ def send_emails_to_users(sender, instance=None, created=False, **kwargs):
elif (old_active, new_active) == (True, False):
instance.email_user_on_deactivation()

def check_user_activation(sender, instance=None, created=False, **kwargs):

old_active = instance.tracker.previous('is_active')
new_active = instance.is_active

if (old_active, new_active) == (False, True):
instance.create_pin()
elif (old_active, new_active) == (True, False):
instance.pin = None


def log_user_login(sender, user=None, **kwargs):
logger.info('User with email %s has been logged in.' % user.email)
Expand Down
24 changes: 24 additions & 0 deletions taas/user/migrations/0006_add_pins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('user', '0005_alter_budget_field'),
]

operations = [
migrations.AddField(
model_name='user',
name='button_id',
field=models.CharField(blank=True, max_length=64, null=True, verbose_name='Button ID'),
),
migrations.AddField(
model_name='user',
name='pin',
field=models.CharField(blank=True, max_length=4, null=True, verbose_name='pin'),
),
]
15 changes: 14 additions & 1 deletion taas/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from random import sample

from model_utils import FieldTracker

Expand Down Expand Up @@ -35,7 +36,6 @@ def create_user(self, email=None, password=None, **extra_fields):
def create_superuser(self, email, password, **extra_fields):
return self._create_user(email, password, True, True, **extra_fields)


class User(AbstractBaseUser, PermissionsMixin):
first_name = models.CharField(_('first name'), max_length=30)
last_name = models.CharField(_('last name'), max_length=30)
Expand All @@ -49,6 +49,8 @@ class User(AbstractBaseUser, PermissionsMixin):
budget = models.DecimalField(_('budget (€)'), decimal_places=2, max_digits=10,
validators=[MinValueValidator(0.0)], default=0.0)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
pin = models.CharField(_('pin'), max_length=4, blank=True, null=True, )
button_id = models.CharField(_('Button ID'), max_length=64, blank=True, null=True, )

objects = CustomUserManager()
tracker = FieldTracker()
Expand Down Expand Up @@ -79,6 +81,17 @@ def display_budget_money(self):
"""
return '%d€' % self.budget


def create_pin(self):
"""
Generates a pin for user
Only 10000 pins are generated. If you plan on having more users, change this.
"""
all_pins = set('{0:04}'.format(num) for num in range(0, 10000))
used_pins = set(User.objects.values_list('pin', flat=True))
all_pins.difference(used_pins)
self.pin = sample(all_pins, 1)[0]

def email_user_on_registration(self):
"""
Sends an email to this user, when he is created.
Expand Down
6 changes: 6 additions & 0 deletions taas/user/static/css/user.css
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,9 @@ input.field-error, input[type="text"].field-error {
cursor: initial;
}

#pin {
font-size: 14px;
letter-spacing: 1px;
text-transform: uppercase;
margin-left: 0;
}
3 changes: 3 additions & 0 deletions taas/user/templates/user_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ <h1>{% trans 'Update account' %}</h1>
{% endfor %}

<div class="fieldWrapper">

<p id="pin">{% trans "Your gate pin: " %}{{ pin }}</p>

{{ form.first_name.label_tag }}
{% render_field form.first_name|add_error_class:"field-error" %}
{% for err in form.first_name.errors %}
Expand Down
28 changes: 28 additions & 0 deletions taas/user/tests/test_user_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ def test_logged_in_user_can_update_his_first_name(self):
user = User.objects.get(email=self.user.email)
self.assertEqual(user.first_name, 'Test')

def test_active_user_can_see_pin(self):
user = UserFactory()
user.is_active = True
user.save()
self.client.login(username=user.email, password='isherenow', follow=True)
response = self.client.get(self.update_url)
self.assertNotEqual(response.context_data['pin'], None)

def test_active_user_has_pin(self):
user = UserFactory()
user.is_active = True
user.save()
self.assertNotEqual(user.pin, None)

def test_inactive_user_has_None_pin(self):
user = UserFactory()
self.assertEqual(user.pin, None)

def test_deactivated_user_has_None_pin(self):
user = UserFactory()
user.is_active = True
user.save()
self.assertNotEqual(user.pin, None)
user.is_active = False
user.save()
self.assertEqual(user.pin, None)


def test_logged_in_user_can_update_his_last_name(self):
self.client.login(username=self.user.email, password='isherenow', follow=True)

Expand Down
5 changes: 5 additions & 0 deletions taas/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class UserUpdateView(mixins.LoggedInMixin, UpdateView):
def get_object(self, queryset=None):
return self.request.user

def get_context_data(self, **kwargs):
kwargs['pin'] = self.request.user.pin

return super(UserUpdateView, self).get_context_data(**kwargs)

def form_valid(self, form):
self.object = form.save()
update_session_auth_hash(self.request, self.object)
Expand Down

0 comments on commit 6592af2

Please sign in to comment.