Fields:
* id
* url
* created_time
* last_updated
* eauth_id
* first_name
* middle_name
* last_name
* title
* email
* desk_phone
* desk_phone_extension
* mobile_phone
* fsis_cell_phone
* status_id
* home_phone
* status_name
* status_display_text
* last_login_time
* access_level_id
* access_level_name
* access_level_display_text
"""
# The phone group requires this not_required_char to work.
not_required = {'allow_null': True, 'required': False}
not_required_char = not_required.copy()
not_required_char.update({'allow_blank': True})
phone_regex = r"^\(?[2-9]\d\d\)?-\d{3}-\d{4}$"
desk_phone = serializers.RegexField(regex=phone_regex, **not_required_char)
mobile_phone = serializers.RegexField(
regex=phone_regex, **not_required_char)
home_phone = serializers.RegexField(regex=phone_regex, **not_required_char)
fsis_cell_phone = serializers.RegexField(
regex=phone_regex, **not_required_char)
status_id = serializers.IntegerField(required=False)
status_name = StatusLookupField()
status_display_text = serializers.CharField(
source='status.display_text', read_only=True)
access_level_id = serializers.IntegerField(required=False)
access_level_name = AccessLevelField(required=True)
access_level_display_text = serializers.CharField(
source='access_level.display_text', read_only=True, required=False)
sec_user = serializers.PrimaryKeyRelatedField(read_only=True)
def validate(self, data):
phone_fields = (
'desk_phone',
'mobile_phone',
'home_phone',
'fsis_cell_phone',
)
if self.instance:
new_data = model_to_dict(self.instance)
else:
new_data = {}
new_data.update(data)
if all((new_data.get(f, '') == '' or new_data.get(f, '') is None)
for f in phone_fields):
raise serializers.ValidationError(
'At least one phone is required.')
if not (new_data.get('status') or new_data.get('status_id')):
raise serializers.ValidationError(
'status_id or status_name is required.')
return data
def create(self, validated_data):
self._link_sec_user(validated_data)
return super(UserSerializer, self).create(validated_data)
def _link_sec_user(self, validated_data):
"""Gets or creates a SecUser with the provided eauth_id and sets it on
the FRIO user's sec_user field."""
eauth_id = validated_data.get('eauth_id')
if eauth_id:
module = USDAModule.objects.get(name='FRIO')
sec_user, created = SecUser.objects.get_or_create(
module=module, user_name=eauth_id)
validated_data['sec_user'] = sec_user
def update(self, instance, validated_data):
self._update_sec_user(instance, validated_data)
return super(UserSerializer, self).update(instance, validated_data)
def _update_sec_user(self, instance, validated_data):
"""Updates the security user's user_name to match the eauth_id."""
eauth_id = validated_data.get('eauth_id')
if eauth_id and instance.sec_user.user_name != eauth_id:
instance.sec_user.user_name = eauth_id
instance.sec_user.save()
class Meta:
model = models.User
fields = ('id', 'url', 'created_time', 'last_updated',
'eauth_id', 'first_name', 'middle_name', 'last_name',
'title', 'email', 'desk_phone', 'desk_phone_extension',
'mobile_phone', 'home_phone', 'fsis_cell_phone', 'status_id',
'status_name', 'status_display_text',
'last_login_time', 'access_level_id', 'access_level_name',
'access_level_display_text',
'sec_user'
)</code>
As you can see, I am trying to update 'last_login_time' and the natural_key on that model is eauth_id. Looking at the code it seems that update
is just not implemented in your code. Am I missing something?