-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deleted messages app, Implemented users
- Loading branch information
Showing
18 changed files
with
174 additions
and
84 deletions.
There are no files selected for viewing
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.conf.urls import patterns, include | ||
from django.utils.module_loading import import_by_path | ||
|
||
|
||
def get_api_urlpatterns(apps): | ||
urls = [] | ||
|
||
for app in apps: | ||
dotted_path = 'chatrooms.{}.urls.api_urlpatterns'.format(app) | ||
urls.append((r'', include(import_by_path(dotted_path)))) | ||
|
||
return patterns('', *urls) | ||
|
||
|
||
urlpatterns = get_api_urlpatterns([ | ||
'users', | ||
# 'rooms', | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
from django.conf.urls import patterns, include, url | ||
from django.views.generic import TemplateView | ||
# from django.views.generic import TemplateView | ||
|
||
from django.contrib import admin | ||
admin.autodiscover() | ||
|
||
urlpatterns = patterns( | ||
'', | ||
url(r'^admin/', include(admin.site.urls)), | ||
url(r'^api-v1/', include('chatrooms.router')) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,30 @@ | ||
from django.contrib import admin | ||
from django.contrib.auth.models import Group | ||
from django.contrib.auth.admin import UserAdmin | ||
|
||
from .models import User | ||
from .forms import UserChangeForm, UserCreationForm | ||
|
||
|
||
admin.site.register(User) | ||
class MyUserAdmin(UserAdmin): | ||
form = UserChangeForm | ||
add_form = UserCreationForm | ||
list_display = ('username', 'first_name', 'last_name', 'email', 'is_admin') | ||
list_filter = ('is_admin',) | ||
fieldsets = ( | ||
(None, {'fields': ('username', 'password')}), | ||
('Personal info', {'fields': ('email', 'first_name', 'last_name')}), | ||
('Permissions', {'fields': ('is_admin',)}), | ||
) | ||
add_fieldsets = ( | ||
(None, { | ||
'classes': ('wide',), | ||
'fields': ('username', 'email', 'first_name', | ||
'last_name', 'password1', 'password2')}), | ||
) | ||
search_fields = ('username', 'first_name', 'last_name', 'email',) | ||
ordering = ('username', 'email', 'first_name', 'last_name') | ||
filter_horizontal = () | ||
|
||
admin.site.register(User, MyUserAdmin) | ||
admin.site.unregister(Group) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from django import forms | ||
from django.contrib.auth.forms import ReadOnlyPasswordHashField | ||
|
||
from .models import User | ||
|
||
|
||
class UserCreationForm(forms.ModelForm): | ||
password1 = forms.CharField(label='Password', widget=forms.PasswordInput) | ||
password2 = forms.CharField( | ||
label='Password confirmation', | ||
widget=forms.PasswordInput) | ||
|
||
class Meta: | ||
model = User | ||
fields = ('email', 'first_name', 'last_name') | ||
|
||
def clean_password2(self): | ||
password1 = self.cleaned_data.get("password1") | ||
password2 = self.cleaned_data.get("password2") | ||
if password1 and password2 and password1 != password2: | ||
raise forms.ValidationError("Passwords don't match") | ||
return password2 | ||
|
||
def save(self, commit=True): | ||
user = super(UserCreationForm, self).save(commit=False) | ||
user.set_password(self.cleaned_data["password1"]) | ||
if commit: | ||
user.save() | ||
return user | ||
|
||
|
||
class UserChangeForm(forms.ModelForm): | ||
password = ReadOnlyPasswordHashField() | ||
|
||
class Meta: | ||
model = User | ||
fields = ('email', 'password', 'first_name', | ||
'last_name', 'is_active', 'is_admin') | ||
|
||
def clean_password(self): | ||
return self.initial["password"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,32 @@ | ||
from django.utils import timezone | ||
from django.contrib.auth.models import BaseUserManager | ||
|
||
|
||
class UserManager(BaseUserManager): | ||
def create_user(self, username, first_name, last_name, | ||
email, password=None): | ||
if not email: | ||
raise ValueError('Users must have an email address') | ||
|
||
user = self.model( | ||
email=self.normalize_email(email), | ||
username=username, | ||
first_name=first_name, | ||
last_name=last_name, | ||
) | ||
|
||
def _create_user(self, username, email, password, | ||
is_staff, is_superuser, **extra_fields): | ||
now = timezone.now() | ||
if not username: | ||
raise ValueError('The given username must be set') | ||
email = self.normalize_email(email) | ||
user = self.model(username=username, email=email, | ||
is_staff=is_staff, is_active=True, | ||
is_superuser=is_superuser, last_login=now, | ||
date_joined=now, **extra_fields) | ||
user.set_password(password) | ||
user.save(using=self._db) | ||
return user | ||
|
||
def create_user(self, username, email=None, password=None, **extra_fields): | ||
return self._create_user(username, email, password, False, False, | ||
**extra_fields) | ||
|
||
def create_superuser(self, username, email, password, **extra_fields): | ||
return self._create_user(username, email, password, True, True, | ||
**extra_fields) | ||
def create_superuser(self, username, first_name, last_name, | ||
email, password): | ||
user = self.create_user( | ||
username=username, | ||
email=email, | ||
password=password, | ||
first_name=first_name, | ||
last_name=last_name, | ||
) | ||
user.is_admin = True | ||
user.save(using=self._db) | ||
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from .models import User | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class UserSerializer(serializers.HyperlinkedModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = ('url', 'username', 'email', 'first_name', | ||
'last_name', 'gravatar_url') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
from django.conf.urls import patterns, url | ||
from rest_framework import routers | ||
|
||
from .views import UserViewSet | ||
|
||
urlpatterns = patterns( | ||
'', | ||
) | ||
|
||
router = routers.DefaultRouter() | ||
router.register(r'users', UserViewSet) | ||
|
||
api_urlpatterns = router.urls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from .models import User | ||
|
||
from rest_framework import viewsets | ||
|
||
from .serializers import UserSerializer | ||
|
||
|
||
class UserViewSet(viewsets.ModelViewSet): | ||
queryset = User.objects.all() | ||
serializer_class = UserSerializer | ||
filter_fields = ('username', 'email', 'first_name', 'last_name') | ||
search_fields = ('username', 'email', 'first_name', 'last_name') | ||
ordering = ('username',) | ||
ordering_fields = ('username', 'email', 'first_name', 'last_name') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters