Commit 3aca0bf8 authored by luxcem's avatar luxcem
Browse files

Fresh start for rp

parents
# OSX files
.DS_Store
._*
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*.pyc
.cache
*.sqlite3
.sass-cache
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Local and production settings
project/settings/env.py
# Logs
*.log
node_modules/
# Testing
htmlcov
.coverage
.tox
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class CoreConfig(AppConfig):
name = 'core'
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
class EmailOrUsernameModelBackend(ModelBackend):
"""
Authentification backend to allow email
overide AUTHENTICATION_BACKENDS with
fabauth.auth_backends.EmailOrUsernameModelBackend
"""
def authenticate(self, username=None, password=None, **kwargs):
if "@" in username:
kwargs = {"email": username}
else:
kwargs = {"username": username}
try:
user = User.objects.get(**kwargs)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
from django.db import models
# Create your models here.
from django.test import TestCase
# Create your tests here.
from django.test import TestCase
from django.test import Client
from django_factory_boy import auth as auth_factories
AUTHENTICATION_BACKENDS = [
# Default
"django.contrib.auth.backends.ModelBackend",
# Email or Username for login
"core.auth_backends.EmailOrUsernameModelBackend"
]
class ApiTest(TestCase):
def setUp(self):
self.client = Client()
self.user = auth_factories.UserFactory(password="dummypassword")
def test_authenticate(self):
with self.settings(AUTHENTICATION_BACKENDS=AUTHENTICATION_BACKENDS):
# username, wrong password
login_status = self.client.login(
username=self.user.username,
password="notthepassword"
)
assert not login_status
# username, correct password
login_status = self.client.login(
username=self.user.username,
password="dummypassword"
)
assert login_status
# email, wrong password
login_status = self.client.login(
username=self.user.email,
password="notthepassowrd"
)
assert not login_status
# email, correct password
login_status = self.client.login(
username=self.user.email,
password="dummypassword"
)
assert login_status
# username, wrong user
login_status = self.client.login(
username="{}-2".format(self.user.username),
password="dummypassword"
)
assert not login_status
# email, wrong user
login_status = self.client.login(
username="{}-2".format(self.user.email),
password="dummypassword"
)
assert not login_status
from django.shortcuts import render
# Create your views here.
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User, Permission
from django.utils.translation import ugettext_lazy as _
from userprofile.models import Profile
@admin.register(Permission)
class PermissionAdmin(admin.ModelAdmin):
list_display = ("name", "codename", "content_type")
list_filter = ("content_type",)
class UserProfileInline(admin.StackedInline):
model = Profile
list_display = ("user", "full_name")
extra = 1
max_num = 1
class UserProfileAdmin(UserAdmin):
inlines = [UserProfileInline]
fieldsets = (
(None, {"fields": ("username", "password")}),
(_("Personal info"), {"fields": ("first_name", "last_name", "email")}),
(_("Permissions"), {
"fields": ("is_active", "is_staff", "is_superuser")}),
)
admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)
from userprofile.models import Profile
from rest_framework import serializers
class ProfileSerializer(serializers.ModelSerializer):
"""A simple profile serializers"""
avatar = serializers.SerializerMethodField()
class Meta:
model = Profile
fields = "__all__"
def get_avatar(self, instance):
if not instance.picture:
return None
return instance.picture.url
from django.apps import AppConfig
class UserprofileConfig(AppConfig):
name = 'userprofile'
import factory
from django_factory_boy import auth as auth_factories
from .models import Profile
class ProfileFactory(factory.django.DjangoModelFactory):
class Meta:
model = Profile
user = factory.SubFactory(auth_factories.UserFactory)
description = factory.Faker("text")
picture = factory.django.ImageField()
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2017-04-03 15:05
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('description', models.CharField(blank=True, max_length=200, null=True)),
('picture', models.ImageField(blank=True, null=True, upload_to='uploads/users/%Y/%m/%d')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='profile', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'User',
},
),
]
from imagekit.models import ImageSpecField
from pilkit.processors import ResizeToFill
from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext as _
class Profile(models.Model):
"""
Extends `django.contrib.auth.models.User` with additional fields
"""
#: OneToOneField to ``django.contrib.auth.models.User``
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
related_name="profile"
)
#: User description
description = models.CharField(max_length=200, null=True, blank=True)
#: User avatar
picture = models.ImageField(
upload_to="uploads/users/%Y/%m/%d",
null=True,
blank=True
)
#: 50x50 thumbnail of user picture
picture_t = ImageSpecField(
source="picture",
processors=[ResizeToFill(50, 50)],
format="jpeg",
)
class Meta:
verbose_name = _("User")
app_label = "userprofile"
def __str__(self):
"""Returns user full name"""
return self.username
def delete(self, *args, **kwargs):
"""Deletes ``django.contrib.auth.models.User`` and
this profile"""
self.user.delete()
return super().delete(*args, **kwargs)
@property
def slug(self):
"""
Returns slug version of user.username
"""
return slugify(self.user.username)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment