Completed
Push — master ( 319370...171ce4 )
by
unknown
01:23
created

SymbolsSerializer.create()   B

Complexity

Conditions 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
dl 0
loc 14
rs 8.5454
1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2014 Crystalnix Limited
7
8
Licensed under the Apache License, Version 2.0 (the "License"); you may not
9
use this file except in compliance with the License. You may obtain a copy of
10
the License at
11
12
    http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
License for the specific language governing permissions and limitations under
18
the License.
19
"""
20
21
from rest_framework import serializers
22
23
from crash.models import Symbols, Crash
24
from crash.utils import parse_debug_meta_info
25
26
27
__all__ = ['SymbolsSerializer']
28
29
30
class SymbolsSerializer(serializers.HyperlinkedModelSerializer):
31
    file = serializers.FileField(required=True)
32
    debug_id = serializers.CharField(default='', required=False, allow_blank=True)
33
    debug_file = serializers.CharField(default='', required=False, allow_blank=True)
34
35
    class Meta:
36
        model = Symbols
37
        fields = ('id', 'file', 'file_size', 'debug_id', 'debug_file',
38
                  'created', 'modified')
39
        read_only_fields = ('created', 'modified')
40
        validators = [
41
            serializers.UniqueTogetherValidator(
42
                queryset=Symbols.objects.all(),
43
                fields=('debug_id', 'debug_file')
44
            )
45
        ]
46
47
    def create(self, validated_data):
48
        if not validated_data.get('debug_id') or \
49
                not validated_data.get('debug_file'):
50
            file = validated_data['file']
51
            try:
52
                head = file.readline().rstrip()
53
                meta = parse_debug_meta_info(head, exception=serializers.ValidationError)
54
                validated_data.update(meta)
55
            except:
56
                raise serializers.ValidationError(u"The file contains invalid data.")
57
        if not validated_data.get('file_size'):
58
            file = validated_data['file']
59
            validated_data['file_size'] = file.size
60
        return super(SymbolsSerializer, self).create(validated_data)
61
62
63
class CrashSerializer(serializers.HyperlinkedModelSerializer):
64
    meta = serializers.DictField()
65
    stacktrace_json = serializers.DictField()
66
67
    class Meta:
68
        model = Crash
69
        fields = ('id', 'upload_file_minidump', 'archive', 'appid', 'userid',
70
                  'meta', 'signature', 'stacktrace_json', 'created', 'modified',)
71