Skip to content

Instantly share code, notes, and snippets.

View aballah-chamakh's full-sized avatar

abdallah-chamakh aballah-chamakh

View GitHub Profile
from rest_framework import serializers
from .models import Profile
from Door.models import Door
from Door.serializers import DoorSerializer
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(style={'input_type':'password'},write_only=True,required=True)
password2 = serializers.CharField(style={'input_type':'password'},write_only=True,required=True)
profile_slug = serializers.SlugField(source='profile.slug',read_only=True)
profile_image = serializers.ImageField(source='profile.image',read_only=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created :
profile_obj = Profile.objects.create(user=instance)
@receiver(pre_save, sender=Profile)
def set_profile_slug(sender, instance, **kwargs):
if not instance.slug :
instance.slug = unique_slug_generator(instance)
from django.contrib import admin
admin.site.register(Profile)
from django.db import models
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
image = models.ImageField(default='defaultProfile.png')
slug = models.SlugField(blank=True,null=True)
@property
def owner(self):
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .forms import UserAdminCreationForm, UserAdminChangeForm
from .models import User
class UserAdmin(BaseUserAdmin):
form = UserAdminChangeForm
add_form = UserAdminCreationForm
from django import forms
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from .models import User
class RegisterForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)
from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
class UserManager(BaseUserManager):
def create_user(self, email, username, password=None):
if not email:
raise ValueError('Users must have an email address')
<html>
<head>
<title>Porfile</title>
</head>
<body>
<h2>Hello {{user.username}}</h2>
<p><a href="{% url 'logout' %}">logout</a></p>
</body>
</html>
from django.shortcuts import render,redirect
from django.contrib.auth import logout
def logout_view(request):
logout(request)
return redirect('/')
<html>
<head>
<title>Login</title>
</head>
<body>
<form method='POST' > {% csrf_token %}
{{form.as_p}}
<input type='submit' name='login' />
</form>
</body>