Skip to content

Commit 09d93b9

Browse files
committed
Migration to Python 3 and Django 3.1
1 parent 2402870 commit 09d93b9

File tree

11 files changed

+206
-177
lines changed

11 files changed

+206
-177
lines changed

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Copyright (c) 2012 Peter Kuma
2-
1+
Copyright (c) 2012-2020 Peter Kuma
2+
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

fileshack/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default_app_config = 'fileshack.apps.Fileshack'

fileshack/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from models import *
1+
from .models import *
22
from django.contrib import admin
33

44
class StoreAdmin(admin.ModelAdmin):

fileshack/apps.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.apps import AppConfig
2+
from django.db.models import signals
3+
4+
def create_default_store(sender, app_config, verbosity, **kwargs):
5+
# Only create the default sites in databases where Django created the table.
6+
if verbosity >= 2:
7+
print("Creating default Store object")
8+
from .models import Store
9+
Store().save()
10+
11+
class Fileshack(AppConfig):
12+
name = 'fileshack'
13+
verbose_name = 'Fileshack'
14+
15+
def ready(self):
16+
signals.post_migrate.connect(create_default_store, sender=self)

fileshack/management.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

fileshack/models.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
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
@@ -22,7 +22,7 @@
2222
from django.utils.translation import ugettext_lazy as _
2323
from django.core.files.storage import default_storage
2424
from django.conf import settings
25-
from django.core.urlresolvers import reverse
25+
from django.urls import reverse
2626

2727
import os
2828
from 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

6767
class 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

165169
class 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")

fileshack/templatetags/staticfiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from urlparse import urljoin
1+
from urllib.parse import urljoin
22
from django import template
33
from django.conf import settings
44

fileshack/urls.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
from django.conf.urls.defaults import patterns, include, url
1+
from django.conf.urls import include, url
2+
from fileshack.views import *
23

3-
urlpatterns = patterns('fileshack.views',
4-
url(r'^cron/$', 'cron', name='cron'),
5-
url(r'^unsubscribe/$', 'unsubscribe', name='unsubscribe'),
6-
url(r'^(?P<store_path>.*)logout/$', 'logout'),
7-
url(r'^(?P<store_path>.*)iframe/$', 'iframe'),
8-
url(r'^(?P<store_path>.*)upload/$', 'simple_upload'),
9-
url(r'^(?P<store_path>.*)upload/(?P<id>[^/]+)/$', 'upload'),
10-
url(r'^(?P<store_path>.*)delete/(?P<item_id>[0-9]+)/$', 'delete'),
11-
url(r'^(?P<store_path>.*)download/(?P<item_id>[0-9]+)/$', 'download', name='download'),
12-
url(r'^(?P<store_path>.*)update/$', 'update'),
13-
url(r'^(?P<store_path>.*)update/(?P<since>[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}:[0-9]{2}:[0-9]{2})/$', 'update'),
14-
url(r'^(?P<store_path>.*)unwatch/$', 'unwatch'),
15-
url(r'^(?P<store_path>.*)watch/$', 'watch'),
16-
url(r'^(?P<store_path>.*)/$', 'index', name='index'),
17-
url(r'^(?P<store_path>)$', 'index'),
18-
)
4+
app_name = 'fileshack'
5+
6+
urlpatterns = [
7+
url(r'^cron/$', cron, name='cron'),
8+
url(r'^unsubscribe/$', unsubscribe, name='unsubscribe'),
9+
url(r'^(?P<store_path>.*)logout/$', logout),
10+
url(r'^(?P<store_path>.*)iframe/$', iframe),
11+
url(r'^(?P<store_path>.*)upload/$', simple_upload),
12+
url(r'^(?P<store_path>.*)upload/(?P<id>[^/]+)/$', upload),
13+
url(r'^(?P<store_path>.*)delete/(?P<item_id>[0-9]+)/$', delete),
14+
url(r'^(?P<store_path>.*)download/(?P<item_id>[0-9]+)/$', download, name='download'),
15+
url(r'^(?P<store_path>.*)update/$', update),
16+
url(r'^(?P<store_path>.*)update/(?P<since>[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}:[0-9]{2}:[0-9]{2})/$', update),
17+
url(r'^(?P<store_path>.*)unwatch/$', unwatch),
18+
url(r'^(?P<store_path>.*)watch/$', watch),
19+
url(r'^(?P<store_path>.*)/$', index, name='index'),
20+
url(r'^(?P<store_path>)$', index),
21+
]

0 commit comments

Comments
 (0)