1- # Copyright (c) 2012 Peter Kuma
1+ # Copyright (c) 2012-2020 Peter Kuma
22#
33# Permission is hereby granted, free of charge, to any person obtaining a copy
44# of this software and associated documentation files (the "Software"), to deal
55# in the Software without restriction, including without limitation the rights
66# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
77# copies of the Software, and to permit persons to whom the Software is
88# furnished to do so, subject to the following conditions:
9- #
9+ #
1010# The above copyright notice and this permission notice shall be included in
1111# all copies or substantial portions of the Software.
12- #
12+ #
1313# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1414# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1515# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2222from django .utils .translation import ugettext_lazy as _
2323from django .core .files .storage import default_storage
2424from django .conf import settings
25- from django .core . urlresolvers import reverse
25+ from django .urls import reverse
2626
2727import os
2828from random import choice
@@ -41,18 +41,18 @@ class Store(Model):
4141 protect_files = BooleanField (_ ("protect files" ), help_text = _ ("Protect files by a random string, so that they cannot be downloaded by guessing their name." ), default = True )
4242 allow_watch = BooleanField (_ ("allow watch" ), help_text = _ ('Allow users to subscribe to receive e-mail updates. Requires cron (see <a href="http://fileshack.sourceforge.net/doc/#store-watching">documentation</a>).' ), default = False )
4343 watch_delay = PositiveIntegerField (_ ("watch delay" ), help_text = _ ("Minimum delay between two notifications in minutes. Only applies when <strong>Allow watch</strong> is enabled." ), default = 360 )
44-
44+
4545 def __unicode__ (self ):
4646 url = self .get_absolute_url ()
4747 if url .startswith ("/" ):
4848 url = url [1 :]
4949 return "default" if url == "" else url
50-
50+
5151 def get_absolute_url (self ):
5252 url = reverse ("fileshack:index" , kwargs = dict (store_path = self .path ))
5353 if url .endswith ("//" ): url = url [:- 1 ] # Ugly hack.
5454 return url
55-
55+
5656 def total (self ):
5757 if self .items .count () == 0 : return 0
5858 return self .items .all ().aggregate (Sum ("size" ))["size__sum" ]
@@ -65,14 +65,18 @@ def item_upload_to(instance, filename):
6565 return os .path .join (instance .store .media , key , filename )
6666
6767class Item (Model ):
68- store = ForeignKey (Store , verbose_name = _ ("store" ), related_name = "items" )
68+ store = ForeignKey (Store ,
69+ verbose_name = _ ("store" ),
70+ related_name = "items" ,
71+ on_delete = CASCADE
72+ )
6973 fileobject = FileField (_ ("file" ), upload_to = item_upload_to )
7074 created = DateTimeField (_ ("created" ), auto_now_add = True )
7175 uploaded = DateTimeField (_ ("uploaded" ), auto_now_add = True )
7276 modified = DateTimeField (_ ("modified" ), auto_now = True )
7377 size_total = IntegerField (_ ("size total" ), default = 0 )
7478 size = IntegerField (_ ("size" ), default = 0 )
75-
79+
7680 def delete (self ):
7781 dir = os .path .dirname (os .path .join (settings .MEDIA_ROOT , self .fileobject .name ))
7882 try : self .fileobject .delete ()
@@ -81,7 +85,7 @@ def delete(self):
8185 except OSError : pass
8286 return Model .delete (self )
8387
84- def status (self ):
88+ def status (self ):
8589 if self .size < self .size_total and dt .now (self .modified .tzinfo ) - self .modified > timedelta (seconds = 10 ):
8690 return 'STALE'
8791 elif self .size < self .size_total :
@@ -108,7 +112,7 @@ def name(self):
108112 return os .path .basename (self .fileobject .name )
109113 except (OSError ,ValueError ):
110114 return None
111-
115+
112116 def size_human (self ):
113117 size = self .size ()
114118 for u in ("B" , "KB" , "MB" , "GB" , "TB" , "PB" , "EB" , "ZB" , "YB" ):
@@ -120,7 +124,7 @@ def size_human(self):
120124 return u"%.0f %s" % (size , unit )
121125 else :
122126 return u"%.1f %s" % (size , unit )
123-
127+
124128 def simple (self ):
125129 return {
126130 "id" : self .id ,
@@ -133,7 +137,7 @@ def simple(self):
133137 "created" : self .created ,
134138 "uploaded" : self .uploaded ,
135139 }
136-
140+
137141 def __unicode__ (self ):
138142 return self .name ()
139143
@@ -163,19 +167,27 @@ class Meta:
163167
164168
165169class Watcher (Model ):
166- user = ForeignKey (User , verbose_name = _ ("user" ), related_name = "watchers" )
167- store = ForeignKey (Store , verbose_name = _ ("store" ), related_name = "watchers" )
170+ user = ForeignKey (User ,
171+ verbose_name = _ ("user" ),
172+ related_name = "watchers" ,
173+ on_delete = CASCADE
174+ )
175+ store = ForeignKey (Store ,
176+ verbose_name = _ ("store" ),
177+ related_name = "watchers" ,
178+ on_delete = CASCADE
179+ )
168180 created = DateTimeField (_ ("created" ), auto_now_add = True )
169181 modified = DateTimeField (_ ("modified" ), auto_now = True )
170-
182+
171183 def simple (self ):
172184 return {
173185 "id" : self .id ,
174186 "email" : self .user .email ,
175187 "last_notification" : self .user .last_notification ,
176188 "created" : self .created ,
177189 }
178-
190+
179191 class Meta :
180192 verbose_name = _ ("watcher" )
181193 verbose_name_plural = _ ("watchers" )
0 commit comments