Commit fa7d3632 authored by freetux's avatar freetux

web app for tanbama

parent 8f4672af
from django.contrib import admin
from django.conf import settings
from django.db import models
from __future__ import unicode_literals
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'accounts'
"""Integrate with admin module."""
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.utils.translation import ugettext_lazy as _
from users.models import User
@admin.register(User)
class UserAdmin(DjangoUserAdmin):
"""Define admin model for custom User model with no email field."""
fieldsets = (
(None, {'fields': ('email', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name')}),
(_('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'),
}),
)
list_display = ('email', 'first_name', 'last_name', 'is_staff')
search_fields = ('email', 'first_name', 'last_name')
ordering = ('email',)
\ No newline at end of file
from __future__ import unicode_literals
from django.db import models
from django.core.mail import send_mail
from django.contrib import auth
from django.contrib.auth.models import (
AbstractBaseUser,
BaseUserManager,
Group,
Permission,
_user_get_all_permissions,
_user_has_perm,
_user_has_module_perms,
)
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
# class UserManager(BaseUserManager):
# def create_user(self, email, password=None, **kw):
# """
# Creates a user with a given email address and password
# """
# if not email:
# raise ValueError('The given email address must be set')
# now = timezone.now()
# u = self.model(
# email=UserManager.normalize_email(email),
# last_login=now,
# **kw
# )
# u.set_password(password)
# u.save(using=self._db)
# return u
# def create_superuser(self, email, password, **kw):
# """
# Creates a superuser with a given email address and password
# """
# u = self.create_user(email, password, **kw)
# u.is_staff = True
# u.is_active = True
# u.is_superuser = True
# u.save(using=self._db)
# return u
# """
# Model for users across the site
# """
# class User(AbstractBaseUser):
# first_name = models.CharField(_('نام'), max_length=30, blank=True)
# last_name = models.CharField(_('نام خانوادگی'), max_length=30, blank=True)
# email = models.EmailField(_('ایمیل'), max_length=255, unique=True)
# photo = models.ImageField(upload_to='images/users/', null=True, blank=True)
# is_active = models.BooleanField(
# _('active'),
# default=True,
# help_text=_('Disable this instead of deleting a user')
# )
# is_staff = models.BooleanField(
# _('staff status'),
# default=False,
# help_text=_('Allow the user access to the admin site')
# )
# is_superuser = models.BooleanField(
# _('superuser status'),
# default=False,
# help_text=_('User has all permissions')
# )
# date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
# groups = models.ManyToManyField(
# Group,
# verbose_name=_('groups'),
# blank=True,
# help_text=_('Groups a user belongs to and inherits permissions from')
# )
# user_permissions = models.ManyToManyField(
# Permission,
# verbose_name=_('user permissions'),
# blank=True,
# help_text=_('Permissions for this user')
# )
# objects = UserManager()
# USERNAME_FIELD = 'email'
# REQUIRED_FIELDS = []
# class Meta:
# verbose_name = _('user')
# verbose_name_plural = _('users')
# def get_full_name(self):
# """
# Return the first name and last name
# """
# full_name = '%s %s' % (self.first_name, self.last_name)
# return full_name.strip()
# def get_short_name(self):
# """
# Since the user is identified by their email address return that
# """
# if self.first_name :
# return self.first_name
# else:
# return self.email
# def get_group_permissions(self, obj=None):
# """
# Returns a list of permissions that this user has through their groups
# """
# permissions = set()
# for backend in auth.get_backends():
# if hasattr(backend, 'get_group_permissions'):
# if obj is not None:
# permissions.update(backend.get_group_permissions(
# self, obj
# ))
# else:
# permissions.update(backend.get_group_permissions(self))
# return permissions
# def get_all_permissions(self, obj=None):
# return _user_get_all_permissions(self, obj)
# def has_perm(self, perm, obj=None):
# """
# Returns True if the user has the given permission
# """
# if self.is_active and self.is_superuser:
# return True
# return _user_has_perm(self, perm, obj)
# def has_perms(self, perm_list, obj=None):
# """
# Returns True if the user has each of the permissions in the give list
# """
# for perm in perm_list:
# if not self.has_perm(perm, obj):
# return False
# return True
# def has_module_perms(self, app_label):
# """
# Returns True if the user has any permissions in the give app label
# """
# if self.is_active and self.is_superuser:
# return True
# return _user_has_module_perms(self, app_label)
# def email_user(self, subject, message, from_email=None):
# """
# Sends an email to the user
# """
# send_mail(subject, message, from_email, [self.email])
# def __unicode__(self):
# return self.get_full_name
\ No newline at end of file
......@@ -3,9 +3,8 @@ from __future__ import unicode_literals
from django.contrib.auth import authenticate
# from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from .models import User
from users.models import User
from .validators import validate_username
......@@ -34,8 +33,6 @@ class UserSerializer(serializers.ModelSerializer):
'id',
'last_login',
'email',
'first_name',
'last_name',
'is_active',
'date_joined',
'password'
......@@ -43,8 +40,6 @@ class UserSerializer(serializers.ModelSerializer):
read_only_fields = ('last_login', 'is_active', 'date_joined')
extra_kwargs = {
'password': {'required': True, 'write_only': True},
'first_name': {'required': True},
'last_name': {'required': True}
}
......@@ -58,4 +53,3 @@ class UserSerializer(serializers.ModelSerializer):
validated_data.pop('password'),
**validated_data
)
import json
from django.contrib.auth.models import User
from users.models import User
from django.core.urlresolvers import reverse
from rest_framework.authtoken.models import Token
......
from django.conf.urls import url
from users.views import UserRegistrationAPIView
from .views import UserRegistrationAPIView
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
urlpatterns = [
......
from django.core.exceptions import ValidationError
from .models import User
from users.models import User
def validate_username(username):
if User.objects.filter(**{'{}__iexact'.format(User.USERNAME_FIELD): username}).exists():
......
......@@ -3,7 +3,7 @@ from rest_framework import status, permissions
from rest_framework.generics import CreateAPIView, GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from users.serializers import UserSerializer, UserLoginSerializer
from .serializers import UserSerializer, UserLoginSerializer
import jwt
from rest_framework_jwt.utils import jwt_payload_handler
from tanbama import settings
......
from django.contrib.auth.models import User
from users.models import User
from rest_framework import serializers
from body.models import Weight, Drug, Rx
......@@ -8,7 +8,7 @@ class WeightUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("id", "username", "email", "date_joined")
fields = ("id", "email", "date_joined")
......
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class SitesConfig(AppConfig):
name = 'sites'
from django import forms
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from users.models import User
class UserCreationForm(forms.ModelForm):
email = forms.EmailField(label='ایمیل', max_length=255)
password1 = forms.CharField(label='گذرواژه', widget=forms.PasswordInput)
password2 = forms.CharField(label='گذرواژه مجدد', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('email',)
def clean_email(self):
email = self.cleaned_data.get('email')
qs = User.objects.filter(email=email)
if qs.exists():
raise forms.ValidationError("email is taken")
return email
def clean_password2(self):
# Check that the two password entries match
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
class UserAdminCreationForm(forms.ModelForm):
"""A form for creating new users. Includes all the required
fields, plus a repeated password."""
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('email',)
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")