Completed
Push — master ( 646ecf...07a97f )
by
unknown
48s
created

  A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 45
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parkleVersion.file_package_name() 0 3 1
A parkleVersion.file_url() 0 3 1
A parkleVersion.file_absolute_url() 0 3 1
A parkleVersion.size() 0 3 1
A parkleVersion.__str__() 0 2 1
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 __future__ import unicode_literals
22
from django.utils.encoding import python_2_unicode_compatible
23
24
import os
25
26
from django.db import models
27
from django.dispatch import receiver
28
from django.db.models.signals import pre_delete,pre_save
29
30
from versionfield import VersionField
31
32
from omaha.models import BaseModel, Application, Channel
33
from sparkle.managers import VersionManager
34
from omaha_server.s3utils import public_read_storage
35
36
37
def version_upload_to(obj, filename):
38
    return os.path.join('sparkle', obj.app.name, obj.channel.name, str(obj.version), filename)
39
40
41
@python_2_unicode_compatible
42
class SparkleVersion(BaseModel):
43
    is_enabled = models.BooleanField(default=True)
44
    is_critical = models.BooleanField(default=False)
45
    app = models.ForeignKey(Application)
46
    channel = models.ForeignKey(Channel, db_index=True)
47
    version = VersionField(help_text='Format: 65535.65535',
48
                           number_bits=(16, 16), db_index=True)
49
    short_version = VersionField(help_text='Format: 255.255.65535.65535',
50
                                 number_bits=(8, 8, 16, 16), blank=True, null=True)
51
    release_notes = models.TextField(blank=True, null=True)
52
    file = models.FileField(upload_to=version_upload_to, null=True,
53
                            storage=public_read_storage)
54
    file_size = models.PositiveIntegerField(null=True, blank=True)
55
    dsa_signature = models.CharField(verbose_name='DSA signature',
56
                                     max_length=140, null=True, blank=True)
57
58
    objects = VersionManager()
59
60
    class Meta:
61
        index_together = (
62
            ('app', 'channel'),
63
        )
64
        unique_together = (
65
            ('app', 'channel', 'version'),
66
        )
67
68
    def __str__(self):
69
        return "{app} {version}".format(app=self.app, version=self.version)
70
71
    @property
72
    def file_absolute_url(self):
73
        return self.file.url
74
75
    @property
76
    def file_package_name(self):
77
        return os.path.basename(self.file_absolute_url)
78
79
    @property
80
    def file_url(self):
81
        return '%s/' % os.path.dirname(self.file_absolute_url)
82
83
    @property
84
    def size(self):
85
        return self.file_size
86
87
88
@receiver(pre_save, sender=SparkleVersion)
89
def pre_sparkle_save(sender, instance, *args, **kwargs):
90
    if instance.pk:
91
        old = sender.objects.get(pk=instance.pk)
92
        if old.file == instance.file:
93
            return
94
        else:
95
            old.file.delete(save=False)
96
            old.file_size = 0
97
98
99
@receiver(pre_delete, sender=SparkleVersion)
100
def pre_version_delete(sender, instance, **kwargs):
101
    storage, name = instance.file.storage, instance.file.name
102
    if name:
103
        storage.delete(name)
104