diff --git a/accounts/admin.py b/accounts/admin.py new file mode 100644 index 0000000..bb2b65b --- /dev/null +++ b/accounts/admin.py @@ -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",) \ No newline at end of file diff --git a/accounts/forms.py b/accounts/forms.py new file mode 100644 index 0000000..7bb0465 --- /dev/null +++ b/accounts/forms.py @@ -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',) \ No newline at end of file diff --git a/accounts/migrations/0001_initial.py b/accounts/migrations/0001_initial.py new file mode 100644 index 0000000..4466b23 --- /dev/null +++ b/accounts/migrations/0001_initial.py @@ -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, + }, + ), + ] diff --git a/accounts/migrations/0002_auto_20170428_0359.py b/accounts/migrations/0002_auto_20170428_0359.py new file mode 100644 index 0000000..2c0ccd6 --- /dev/null +++ b/accounts/migrations/0002_auto_20170428_0359.py @@ -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), + ), + ] diff --git a/accounts/migrations/0003_auto_20170428_0401.py b/accounts/migrations/0003_auto_20170428_0401.py new file mode 100644 index 0000000..78e01dc --- /dev/null +++ b/accounts/migrations/0003_auto_20170428_0401.py @@ -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), + ), + ] diff --git a/accounts/migrations/__init__.py b/accounts/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/accounts/models.py b/accounts/models.py new file mode 100644 index 0000000..4dcc70d --- /dev/null +++ b/accounts/models.py @@ -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 \ No newline at end of file diff --git a/accounts/tests.py b/accounts/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/accounts/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/accounts/urls.py b/accounts/urls.py new file mode 100644 index 0000000..0a8f095 --- /dev/null +++ b/accounts/urls.py @@ -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'), +] \ No newline at end of file diff --git a/accounts/views.py b/accounts/views.py new file mode 100644 index 0000000..9d5d025 --- /dev/null +++ b/accounts/views.py @@ -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}) diff --git a/swiftmeet/settings.py b/swiftmeet/settings.py index 9a2bcf3..eb791ca 100644 --- a/swiftmeet/settings.py +++ b/swiftmeet/settings.py @@ -37,6 +37,7 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'accounts' ] MIDDLEWARE = [ @@ -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': [ @@ -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 @@ -127,4 +134,7 @@ from .local_settings import * except ImportError as e: if "local_settings" not in str(e): - raise e \ No newline at end of file + raise e + +AUTH_USER_MODEL = 'accounts.Account' +LOGIN_REDIRECT_URL = '/events' \ No newline at end of file diff --git a/swiftmeet/urls.py b/swiftmeet/urls.py index d23646d..582bb82 100644 --- a/swiftmeet/urls.py +++ b/swiftmeet/urls.py @@ -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')) ] diff --git a/templates/accounts/login.html b/templates/accounts/login.html new file mode 100644 index 0000000..5dca62d --- /dev/null +++ b/templates/accounts/login.html @@ -0,0 +1,41 @@ +{% extends 'main/base.html' %} + +{% block content %} +
+

+
+
+

LOGIN TO ACCOUNT

+ {% if success %} +
+ {{ success }} +
+ {% endif %} + {% if form.errors %} + {% for field in form %} + {% for error in field.errors %} +
+ {{ error|escape }} +
+ {% endfor %} + {% endfor %} + {% for error in form.non_field_errors %} +
+ {{ error|escape }} +
+ {% endfor %} + {% endif %} +
+
+ {% csrf_token %} +

+

+
+ Create an account +
+
+
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/templates/accounts/register.html b/templates/accounts/register.html new file mode 100644 index 0000000..3382c2f --- /dev/null +++ b/templates/accounts/register.html @@ -0,0 +1,40 @@ +{% extends 'main/base.html' %} + +{% block content %} +
+

+
+
+

CREATE AN ACCOUNT

+ {% if success %} +
+ {{ success }} +
+ {% endif %} + {% if form.errors %} + {% for field in form %} + {% for error in field.errors %} +
+ {{ error|escape }} +
+ {% endfor %} + {% endfor %} + {% for error in form.non_field_errors %} +
+ {{ error|escape }} +
+ {% endfor %} + {% endif %} +
+
+ {% csrf_token %} + {{form.as_p}} +
+ Proceed to login +
+
+
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/templates/includes/footer.html b/templates/includes/footer.html new file mode 100644 index 0000000..90414a7 --- /dev/null +++ b/templates/includes/footer.html @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/templates/includes/navbar.html b/templates/includes/navbar.html new file mode 100644 index 0000000..e69de29 diff --git a/templates/main/base.html b/templates/main/base.html new file mode 100644 index 0000000..d642748 --- /dev/null +++ b/templates/main/base.html @@ -0,0 +1,25 @@ + + + + Swiftmeet + {% load static %} + + + + +{% include 'includes/navbar.html' %} + +
+ {% block content %} + {% endblock %} +
+ + + + + + \ No newline at end of file