SparkleVersion.__str__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
rs 10
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 __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
    minimum_system_version = VersionField(help_text='Format: 255.255.255',
52
                                          number_bits=(8, 8, 8), blank=True, null=True)
53
    release_notes = models.TextField(blank=True, null=True)
54
    file = models.FileField(upload_to=version_upload_to, null=True,
55
                            storage=public_read_storage)
56
    file_size = models.PositiveIntegerField(null=True, blank=True)
57
    dsa_signature = models.CharField(verbose_name='DSA signature',
58
                                     max_length=140, null=True, blank=True)
59
60
    objects = VersionManager()
61
62
    class Meta:
63
        index_together = (
64
            ('app', 'channel'),
65
        )
66
        unique_together = (
67
            ('app', 'channel', 'version'),
68
        )
69
70
    def __str__(self):
71
        return "{app} {version}".format(app=self.app, version=self.version)
72
73
    @property
74
    def file_absolute_url(self):
75
        return self.file.url
76
77
    @property
78
    def file_package_name(self):
79
        return os.path.basename(self.file_absolute_url)
80
81
    @property
82
    def file_url(self):
83
        return '%s/' % os.path.dirname(self.file_absolute_url)
84
85
    @property
86
    def size(self):
87
        return self.file_size
88
89
90
@receiver(pre_save, sender=SparkleVersion)
91
def pre_sparkle_save(sender, instance, *args, **kwargs):
92
    if instance.pk:
93
        old = sender.objects.get(pk=instance.pk)
94
        if old.file == instance.file:
95
            return
96
        else:
97
            old.file.delete(save=False)
98
            old.file_size = 0
99
100
101
@receiver(pre_delete, sender=SparkleVersion)
102
def pre_version_delete(sender, instance, **kwargs):
103
    storage, name = instance.file.storage, instance.file.name
104
    if name:
105
        storage.delete(name)
106