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