SparkleVersionSerializer   A
last analyzed

Size/Duplication

Total Lines 19
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 5 5 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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