-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
130 lines (109 loc) · 4.66 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
import os
import ipapub
from django.db import models
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
from django.utils.deconstruct import deconstructible
from uuid import uuid4
from .contenttyperestrictedfilefield import ContentTypeRestrictedFileField
@deconstructible
class PathAndRename(object):
def __init__(self, sub_path):
self.path = sub_path
def __call__(self, instance, filename):
ext = filename.split('.')[-1]
# set filename as random string
if instance.pk:
ext = filename.split('.')[-1]
filename = '{0}.{1}'.format(instance.pk, ext)
else:
# set filename with path
filename = os.path.join(os.path.dirname(instance.path), filename)
# return the whole path to the file
return os.path.join(self.path, filename)
icon_path = PathAndRename(ipapub.PACKAGE_DIR)
@deconstructible
class PathAndRename2(object):
def __init__(self, sub_path):
self.path = sub_path
def __call__(self, instance, filename):
ext = filename.split('.')[-1]
# set filename as random string
if instance.pk:
filename = '{0}.{1}'.format(instance.pk, ext)
else:
# set filename as random string
filename = '{0}.{1}'.format(uuid4().hex, ext)
# return the whole path to the file
return os.path.join(self.path, filename)
random_path = PathAndRename2(ipapub.UPLOAD_DIR)
class UpFile(models.Model):
path = models.CharField(max_length=200) # 上传路径
# file = models.FileField(upload_to=path_and_rename(ipapub.UPLOAD_DIR))
file = ContentTypeRestrictedFileField(content_types=['application/x-gtar'],
max_upload_size=104857600,
upload_to=random_path, blank=True, null=True) # 上传的文件
icons = ContentTypeRestrictedFileField(content_types=['image/png'],
max_upload_size=2621440,
upload_to=icon_path) # 小图标
iconb = ContentTypeRestrictedFileField(content_types=['image/png'],
max_upload_size=2621440,
upload_to=icon_path) # 大图标
plist = models.FileField(upload_to=ipapub.PACKAGE_DIR) # plist文件
pub = models.FileField(upload_to=ipapub.PACKAGE_DIR) # 发布的文件压缩包集合
signed = ContentTypeRestrictedFileField(content_types=['application/iphone', 'application/octet-stream'],
max_upload_size=104857600,
upload_to=icon_path, blank=True, null=True) # 签名好的文件
status = models.CharField(max_length=10, blank=True) # 状态,uploaded,。。。
user = models.CharField(max_length=10, blank=False) # 上传的svn用户名
label = models.CharField(max_length=200, blank=True) # 标签
up_date = models.DateTimeField('upload date', auto_now_add=True) # 上传的时间
from_ip = models.GenericIPAddressField(blank=True, null=True) # 上传的ip
def __unicode__(self):
return self.path
# These two auto-delete files from filesystem when they are unneeded:
@receiver(models.signals.post_delete, sender=UpFile)
def auto_delete_file_on_delete(sender, instance, **kwargs):
"""Deletes file from filesystem
when corresponding `UpFile` object is deleted.
"""
if instance.file:
if os.path.isfile(instance.file.path):
os.remove(instance.file.path)
if instance.signed:
if os.path.isfile(instance.signed.path):
os.remove(instance.signed.path)
# @receiver(models.signals.pre_save, sender=UpFile)
# def auto_delete_file_on_change(sender, instance, **kwargs):
# """Deletes file from filesystem
# when corresponding `UpFile` object is changed.
# """
# if not instance.pk:
# return False
#
# while True:
# try:
# old_file = UpFile.objects.get(pk=instance.pk).file
# except UpFile.DoesNotExist:
# break;
#
# new_file = instance.file
# if not old_file == new_file:
# if os.path.isfile(old_file.path):
# os.remove(old_file.path)
#
# break
#
# while True:
# try:
# old_file = UpFile.objects.get(pk=instance.pk).signed
# except UpFile.DoesNotExist:
# break;
#
# new_file = instance.signed
# if not old_file == new_file:
# if os.path.isfile(old_file.path):
# os.remove(old_file.path)
#
# break