Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fields = (
'name',
'format',
'default',
'content',
'raw_content'
)
def get_raw_content(self, obj):
if obj.content:
return obj.content.read().encode('base64')
else:
return None
# Record Serializer
class RecordSerializer(serializers.ModelSerializer):
styles = StyleSerializer(many=True,required=False)
workspace = serializers.CharField(max_length=255, write_only=True)
name = serializers.CharField(max_length=255, write_only=True)
class Meta:
model = Record
fields = (
'title',
'insert_date',
'any_text',
'modified',
'abstract',
'keywords',
'bounding_box',
'crs',
'publication_date',
'service_type',
'service_type_version',
def create(self,request):
styles_data = None
http_status = status.HTTP_200_OK
if "styles" in request.data:
styles_data = request.data.pop("styles")
#parse and valid record data
serializer = RecordSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
#parse and valid styles data
style_serializers = [StyleSerializer(data=style) for style in styles_data] if styles_data else []
if style_serializers:
for style_serializer in style_serializers:
style_serializer.is_valid(raise_exception=True)
#save record data.
identifier = "{}:{}".format(serializer.validated_data['workspace'],serializer.validated_data['name'])
try:
serializer.instance = Record.objects.get(identifier=identifier)
if not serializer.instance.auto_update:
#auto update disabled
for key in ["title","abstract","auto_update","modified","insert_date"]:
if key in serializer.validated_data: serializer.validated_data.pop(key)
except Record.DoesNotExist:
serializer.validated_data['identifier']=identifier
http_status = status.HTTP_201_CREATED