2014-05-08 16:59:35 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from .models import Account
|
|
|
|
|
|
|
|
|
|
|
|
class AccountSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Account
|
|
|
|
fields = (
|
2015-04-30 09:51:55 +00:00
|
|
|
'url', 'id', 'username', 'type', 'language', 'short_name', 'full_name', 'date_joined',
|
2014-10-27 14:31:04 +00:00
|
|
|
'is_active'
|
2014-05-08 16:59:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class AccountSerializerMixin(object):
|
2014-10-16 15:11:52 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(AccountSerializerMixin, self).__init__(*args, **kwargs)
|
2015-05-28 10:55:48 +00:00
|
|
|
self.account = self.get_account()
|
2015-05-18 15:21:42 +00:00
|
|
|
|
|
|
|
def get_account(self):
|
2014-10-16 15:11:52 +00:00
|
|
|
request = self.context.get('request')
|
|
|
|
if request:
|
2015-05-18 15:21:42 +00:00
|
|
|
return request.user
|
2014-10-16 15:11:52 +00:00
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
def create(self, validated_data):
|
2015-05-18 15:21:42 +00:00
|
|
|
validated_data['account'] = self.get_account()
|
2015-04-23 14:34:04 +00:00
|
|
|
return super(AccountSerializerMixin, self).create(validated_data)
|