Fakultas Ilmu Komputer UI

Commit 505fa23d authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

[GREEN] Change accounts to have author, adjust logs accordingly

parent 0a57d7ee
Pipeline #41413 failed with stages
in 10 minutes and 15 seconds
# Generated by Django 3.0.1 on 2020-04-19 11:26
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0002_auto_20200419_0156'),
]
operations = [
migrations.AddField(
model_name='accounthistory',
name='author',
field=models.UUIDField(null=True),
),
]
# Generated by Django 3.0.1 on 2020-04-19 11:29
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('accounts', '0003_accounthistory_author'),
]
operations = [
migrations.AddField(
model_name='account',
name='author',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='account', to='accounts.Account'),
),
]
......@@ -15,6 +15,7 @@ class AccountHistory(HistoryModel):
is_admin = models.BooleanField(default=False)
is_verified = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
author = models.UUIDField(null=True)
objects = models.Manager()
......@@ -38,6 +39,13 @@ class Account(HistoryEnabledModel):
is_verified = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(
"self",
blank=True,
null=True,
on_delete=models.DO_NOTHING,
related_name="account",
)
history_class = AccountHistory
relation_fields = ["user"]
......
......@@ -52,3 +52,6 @@ class AccountRegisterSerializer(serializers.ModelSerializer):
serializer = AccountSerializer(instance)
return serializer.data
def save(self):
account = self.context.get('request').user.account
super(AccountRegisterSerializer, self).save(author=account)
......@@ -58,6 +58,9 @@ class AccountViewSet(viewsets.ModelViewSet):
user = User.objects.create_user(username=username, password=password)
Account.objects.create(user=user, **serializer.validated_data)
def perform_destroy(self, instance):
instance.delete(author=self.request.user.account)
@action(detail=False, methods=["get"], url_path="me")
def profile(self, request):
user = request.user
......
# Generated by Django 3.0.1 on 2020-04-18 18:56
# Generated by Django 3.0.1 on 2020-04-19 10:56
from django.db import migrations
......
from rest_framework import serializers
from apps.accounts.serializers import AccountSerializer
from apps.accounts.models import AccountHistory
from apps.accounts.models import AccountHistory, Account
from apps.cases.models import (
CaseSubjectHistory,
InvestigationCaseHistory,
......@@ -44,5 +44,9 @@ class LogSerializer(serializers.Serializer):
return MODEL_NAME_CASE_SUBJECT
def get_author(self, instance):
if hasattr(instance, "author"):
return AccountSerializer(instance.author).data
if isinstance(instance.author, Account):
author = instance.author
else:
author = Account.objects.filter(id=instance.author).first()
return AccountSerializer(author).data
Supports Markdown
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