SparkleVersionSerializer.create()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
dl 5
loc 5
rs 9.4285
c 0
b 0
f 0
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 omaha.models import Application, Channel
24
25
from sparkle.models import SparkleVersion
26
27
28
__all__ = ['SparkleVersionSerializer']
29
30
31
32 View Code Duplication
class SparkleVersionSerializer(serializers.HyperlinkedModelSerializer):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
33
    is_enabled = serializers.BooleanField(default=True, required=False)
34
    app = serializers.PrimaryKeyRelatedField(queryset=Application.objects.all())
35
    channel = serializers.PrimaryKeyRelatedField(queryset=Channel.objects.all())
36
    version = serializers.CharField()
37
    short_version = serializers.CharField(required=False)
38
39
    class Meta:
40
        model = SparkleVersion
41
        fields = ('id', 'is_enabled', 'is_critical', 'app', 'channel', 'version', 'short_version',
42
                  'release_notes', 'file', 'file_size', 'dsa_signature',
43
                  'created', 'modified')
44
        read_only_fields = ('created', 'modified')
45
46
    def create(self, validated_data):
47
        if not validated_data.get('file_size'):
48
            file = validated_data['file']
49
            validated_data['file_size'] = file.size
50
        return super(SparkleVersionSerializer, self).create(validated_data)
51