Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from .models import Account
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import ugettext_lazy as _

from .forms import CustomUserChangeForm, CustomUserCreationForm

class CustomUserAdmin(UserAdmin):
# The forms to add and change user instances

# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference the removed 'username' field
fieldsets = (
(None, {'fields': ('email', 'password')}),
(_('Personal info'), {'fields': (
'first_name',
'last_name',
'email',
'handle',
'about_me',
'subscribers',
)}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password1', 'password2')}
),
)
form = CustomUserChangeForm
add_form = CustomUserCreationForm
list_display = ('email', 'first_name', 'last_name', 'is_staff')
list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
search_fields = ('email', 'first_name', 'last_name')
ordering = ('email',)
filter_horizontal = ('groups', 'user_permissions',)
readonly_fields = ("date_joined",)

admin.site.register(Account, CustomUserAdmin)

class CustomUserCreationForm(UserCreationForm):
"""
A form that creates a user, with no privileges, from the given email and
password.
"""

def __init__(self, *args, **kargs):
super(CustomUserCreationForm, self).__init__(*args, **kargs)
del self.fields['username']

class Meta:
model = Account
fields = ("email",)

class CustomUserChangeForm(UserChangeForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""

def __init__(self, *args, **kargs):
super(CustomUserChangeForm, self).__init__(*args, **kargs)
del self.fields['username']

class Meta:
model = Account
fields = ("email",)
37 changes: 37 additions & 0 deletions accounts/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import Account

class CustomUserCreationForm(UserCreationForm):
"""
A form that creates a user, with no privileges, from the given email and
password.
"""

def __init__(self, *args, **kargs):
super(CustomUserCreationForm, self).__init__(*args, **kargs)
#del self.fields['username']
self.fields['email'].widget.attrs.update({'class' : 'form-control'})
self.fields['first_name'].widget.attrs.update({'class' : 'form-control'})
self.fields['last_name'].widget.attrs.update({'class' : 'form-control'})
self.fields['handle'].widget.attrs.update({'class' : 'form-control'})
self.fields['about_me'].widget.attrs.update({'class' : 'form-control'})
self.fields['password1'].widget.attrs.update({'class' : 'form-control'})
self.fields['password2'].widget.attrs.update({'class' : 'form-control'})
class Meta:
model = Account
fields = ('email', 'first_name', 'last_name', 'handle', 'about_me', 'password1', 'password2')

class CustomUserChangeForm(UserChangeForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""

def __init__(self, *args, **kargs):
super(CustomUserChangeForm, self).__init__(*args, **kargs)
#del self.fields['username']
self.fields['email'].widget.attrs.update({'class' : 'form-control'})

class Meta:
model = Account
fields = ('email',)
44 changes: 44 additions & 0 deletions accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2017-04-28 03:32
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0008_alter_user_username_max_length'),
]

operations = [
migrations.CreateModel(
name='Account',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('email', models.EmailField(max_length=225, unique=True)),
('username', models.CharField(max_length=225, unique=True)),
('first_name', models.CharField(blank=True, max_length=50, null=True)),
('last_name', models.CharField(blank=True, max_length=50, null=True)),
('handle', models.CharField(blank=True, max_length=100)),
('about_me', models.TextField(blank=True)),
('date_joined', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('is_admin', models.BooleanField(default=False)),
('is_staff', models.BooleanField(default=False)),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('subscribers', models.ManyToManyField(related_name='_account_subscribers_+', to=settings.AUTH_USER_MODEL)),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'abstract': False,
},
),
]
20 changes: 20 additions & 0 deletions accounts/migrations/0002_auto_20170428_0359.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2017-04-28 03:59
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='account',
name='username',
field=models.CharField(blank=True, max_length=225),
),
]
20 changes: 20 additions & 0 deletions accounts/migrations/0003_auto_20170428_0401.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2017-04-28 04:01
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0002_auto_20170428_0359'),
]

operations = [
migrations.AlterField(
model_name='account',
name='username',
field=models.CharField(blank=True, max_length=225, null=True),
),
]
Empty file added accounts/migrations/__init__.py
Empty file.
80 changes: 80 additions & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from __future__ import unicode_literals

import os

from django.conf import settings
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.core.urlresolvers import reverse
from django.db import models
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _

class AccountManager(BaseUserManager):
""" Manager class that contains methods for
the account model
"""
def create_user(self, email, password=None, **kwargs):
""" create user account
"""
if not email:
raise ValueError("Email is required")

# create account
email = self.normalize_email(email)
account = self.model(email=email, username=email)
account.set_password(password)
account.save()

return account

def create_superuser(self, email, password, **kwargs):
""" creates a user that has administrative
access. (can access the admin panel)
"""
account = self.create_user(email, password, **kwargs)
account.is_admin = True
account.is_staff = True
account.is_superuser = True
account.save()

return account


class Account(AbstractBaseUser, PermissionsMixin):
""" Custom model for the account user. it is
an override from the `django.auth.models.User`
"""
email = models.EmailField(max_length=225, unique=True)
username = models.CharField(max_length=225, blank=True, null=True)
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=50, blank=True, null=True)
handle = models.CharField(max_length=100, blank=True)
about_me = models.TextField(blank=True)
subscribers = models.ManyToManyField('self')

date_joined = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(_('active'), default=True,
help_text=_('Designates whether this user should be treated as '
'active. Unselect this instead of deleting accounts.'))

objects = AccountManager()

USERNAME_FIELD = 'email'

def get_full_name(self):
""" returns the user's full name
"""
return "{first_name} {last_name}".format(
first_name=self.first_name,
last_name=self.last_name
)

def get_short_name(self):
return self.first_name

# def check_passwords(self, password1, password2):
# if
3 changes: 3 additions & 0 deletions accounts/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views

urlpatterns = [
url(r'^register$', views.RegistrationView.as_view(), name='registration'),
url(r'^login$', auth_views.login, { 'template_name':'accounts/login.html'}, name='login'),
]
21 changes: 21 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.shortcuts import render, redirect
from .forms import CustomUserCreationForm
from django.views.generic import TemplateView
from .models import Account

# Create your views here.
class RegistrationView(TemplateView):
template_name = 'accounts/register.html'
form_class = CustomUserCreationForm
initial = {'key': 'value'}
def get(self, request, *args, **kwargs):
form = self.form_class()
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = CustomUserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
print form.errors
return render(request, self.template_name, {'form': form})
14 changes: 12 additions & 2 deletions swiftmeet/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts'
]

MIDDLEWARE = [
Expand All @@ -54,7 +55,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates/')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down Expand Up @@ -117,8 +118,14 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_PATH = os.path.join(BASE_DIR, 'static')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
STATIC_PATH,
)


# Allow any settings to be defined in local_settings.py which should be
# ignored in your version control system allowing for settings to be
Expand All @@ -127,4 +134,7 @@
from .local_settings import *
except ImportError as e:
if "local_settings" not in str(e):
raise e
raise e

AUTH_USER_MODEL = 'accounts.Account'
LOGIN_REDIRECT_URL = '/events'
3 changes: 2 additions & 1 deletion swiftmeet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^account/', include('accounts.urls'))
]
Loading