Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User deletion #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions user_backend/users/migrations/0004_deleteduser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 2.0.5 on 2019-11-14 22:31

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import users.managers


class Migration(migrations.Migration):

dependencies = [
('users', '0003_auto_20180602_0452'),
]

operations = [
migrations.CreateModel(
name='DeletedUser',
fields=[
('user_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
('date_deleted', models.DateTimeField(auto_now_add=True, verbose_name='date_deleted')),
],
options={
'abstract': False,
},
bases=('users.user',),
managers=[
('objects', users.managers.UserManager()),
],
),
]
24 changes: 24 additions & 0 deletions user_backend/users/migrations/0005_auto_20191114_2237.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.0.5 on 2019-11-14 22:37

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('matching', '0011_report'),
('oauth2_provider', '0006_auto_20171214_2232'),
('chat', '0003_auto_20190517_0119'),
('admin', '0002_logentry_remove_auto_add'),
('users', '0004_deleteduser'),
]

operations = [
migrations.RemoveField(
model_name='deleteduser',
name='user_ptr',
),
migrations.DeleteModel(
name='DeletedUser',
),
]
32 changes: 32 additions & 0 deletions user_backend/users/migrations/0006_deleteduser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 2.0.5 on 2019-11-14 22:37

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0005_auto_20191114_2237'),
]

operations = [
migrations.CreateModel(
name='DeletedUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date_deleted', models.DateTimeField(auto_now_add=True, verbose_name='date_deleted')),
('email', models.EmailField(default='', max_length=255, verbose_name='email')),
('first_name', models.CharField(blank=True, max_length=40, verbose_name='first_name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last_name')),
('major', models.CharField(blank=True, max_length=150, verbose_name='major')),
('minor', models.CharField(blank=True, max_length=150, verbose_name='minor')),
('year', models.IntegerField(blank=True, default=0, null=True, verbose_name='year')),
('self_bio', models.TextField(blank=True, verbose_name='self_bio')),
('is_on_chat', models.BooleanField(default=False, verbose_name='is_on_chat')),
('device_id', models.CharField(blank=True, default='', max_length=150, verbose_name='device_id')),
('is_active', models.BooleanField(default=True, verbose_name='is_active')),
('is_admin', models.BooleanField(default=False, verbose_name='is_admin')),
('is_staff', models.BooleanField(default=True, verbose_name='staff status')),
],
),
]
18 changes: 18 additions & 0 deletions user_backend/users/migrations/0007_auto_20191114_2252.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.5 on 2019-11-14 22:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0006_deleteduser'),
]

operations = [
migrations.AlterField(
model_name='deleteduser',
name='email',
field=models.EmailField(max_length=255, verbose_name='email'),
),
]
17 changes: 13 additions & 4 deletions user_backend/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

# Create your models here.

class User(AbstractBaseUser, PermissionsMixin):
class BaseUser(models.Model):
# visible user data
email = models.EmailField(_('email'), max_length=255, unique=True)
email = models.EmailField(_('email'), max_length=255)
first_name = models.CharField(_('first_name'), max_length=40, blank=True)
last_name = models.CharField(_('last_name'), max_length=150, blank=True)
major = models.CharField(_('major'), max_length=150, blank=True)
Expand All @@ -27,12 +27,18 @@ class User(AbstractBaseUser, PermissionsMixin):
default="")

# Metadata fields, automatically has primary key ID
date_created = models.DateTimeField(_('date_created'), auto_now_add=True)
date_updated = models.DateTimeField(_('date_updated'), auto_now=True)
is_active = models.BooleanField(_('is_active'), default=True)
is_admin = models.BooleanField(_('is_admin'), default=False)
is_staff = models.BooleanField(_('staff status'),default=True)

class Meta:
abstract = True

class User(BaseUser, AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email'), max_length=255, unique=True)
date_created = models.DateTimeField(_('date_created'), auto_now_add=True)
date_updated = models.DateTimeField(_('date_updated'), auto_now=True)

# Add email as the username field for auth purposes
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
Expand Down Expand Up @@ -66,3 +72,6 @@ def email_user(self, subject, message, from_email=None, **kwargs):
"""
send_mail(subject, message, from_email, [self.email], **kwargs)


class DeletedUser(BaseUser):
date_deleted = models.DateTimeField(_('date_deleted'), auto_now_add=True)
8 changes: 7 additions & 1 deletion user_backend/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.db.models.signals import post_save
from django.dispatch import receiver

from users.models import User
from users.models import User, DeletedUser
from users.serializers import UserSerializer
from users.S3Client import S3Client

Expand Down Expand Up @@ -102,6 +102,12 @@ def delete(self, request, format=None):
"""
email = get_email(request)
user = get_user_by_email(email)
serializer = UserSerializer(user)
data = serializer.data
for key in ["id", "date_created", "date_updated"]:
del data[key]
deleted_user = DeletedUser(**data)
deleted_user.save()
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

Expand Down