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

Add the email / username of the last user to edit #643

Merged
merged 1 commit into from
Dec 6, 2017
Merged
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
20 changes: 18 additions & 2 deletions app/data/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,31 @@
from django.conf import settings


class DriverRecordSerializer(serializers.RecordSerializer):
class BaseDriverRecordSerializer(serializers.RecordSerializer):
def validate_occurred_from(self, value):
""" Require that record occurred_from be in the past. """
if value > datetime.datetime.now(pytz.timezone(settings.TIME_ZONE)):
raise ValidationError('Occurred date must not be in the future.')
return value


class DetailsReadOnlyRecordSerializer(DriverRecordSerializer):
class DriverRecordSerializer(BaseDriverRecordSerializer):
modified_by = SerializerMethodField(method_name='get_latest_change_email')

def get_latest_change_email(self, record):
"""Returns the email of the user who has most recently modified this Record"""
latest_audit_entry = (RecordAuditLogEntry.objects
.filter(record=record)
.order_by('-date')
.first())
if latest_audit_entry:
if latest_audit_entry.user is not None:
return latest_audit_entry.user.email
return latest_audit_entry.username
return None


class DetailsReadOnlyRecordSerializer(BaseDriverRecordSerializer):
"""Serialize records with only read-only fields included"""
data = serializer_fields.MethodTransformJsonField('filter_details_only')

Expand Down