Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding readonly support for stores and files #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fileshack/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

import signals
2 changes: 1 addition & 1 deletion fileshack/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class StoreAdmin(admin.ModelAdmin):
list_display = ("__unicode__",)

class ItemAdmin(admin.ModelAdmin):
pass
readonly_fields = ('created', 'size_total', 'size')

admin.site.register(Store, StoreAdmin)
admin.site.register(Item, ItemAdmin)
Expand Down
9 changes: 9 additions & 0 deletions fileshack/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Store(Model):
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)
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)
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)
readonly = BooleanField(_("read only"), help_text=_("Set all files to read only. Uploading or deleting files is disabled."), default=False)

def __unicode__(self):
url = self.get_absolute_url()
Expand Down Expand Up @@ -72,6 +73,11 @@ class Item(Model):
modified = DateTimeField(_("modified"), auto_now=True)
size_total = IntegerField(_("size total"), default=0)
size = IntegerField(_("size"), default=0)
readonly = BooleanField(_("read only"), default=False)

@property
def is_readonly(self):
return self.store.readonly or self.readonly

def delete(self):
dir = os.path.dirname(os.path.join(settings.MEDIA_ROOT, self.fileobject.name))
Expand Down Expand Up @@ -132,13 +138,16 @@ def simple(self):
"modified": self.modified,
"created": self.created,
"uploaded": self.uploaded,
"readonly": self.is_readonly,
}

def __unicode__(self):
return self.name()

class Meta:
ordering = [('created')]
verbose_name = _("file")
verbose_name_plural = ("files")

class User(Model):
email = EmailField(_("e-mail"), max_length=254, unique=True)
Expand Down
18 changes: 18 additions & 0 deletions fileshack/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-

from django.db.models.signals import pre_save
from models import Item


def set_file_size(sender, instance, **kwargs): # @UnusedVariable
"""
Automatic set file sizes after uploading.
"""
if instance.fileobject._file is None:
# skip front-end uploads
return
# set sizes
instance.size = instance.fileobject._file.size
instance.size_total = instance.fileobject._file.size

pre_save.connect(set_file_size, Item)
4 changes: 3 additions & 1 deletion fileshack/static/fileshack/js/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ var Item = new Class({
status: 'READY', // 'READY', 'UPLOADING', 'STALE'.
created: new Date(),
uploaded: new Date(),
modified: new Date()
modified: new Date(),
readonly: false,
},

initialize: function(attributes) {
Expand All @@ -98,6 +99,7 @@ var Item = new Class({
this.modified = new Date().parse(json.modified);
this.uploaded = new Date().parse(json.uploaded);
this.created = new Date().parse(json.created);
this.readonly = json.readonly;

this.fireEvent('change');
},
Expand Down
3 changes: 3 additions & 0 deletions fileshack/static/fileshack/js/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ var ItemView = new Class({
this.progressbar.value = 1;
updateProgress(this.progressbar);
}
if (this.model.readonly)
this.deletebtn.hide();

var percentage = 0;
if (this.model.size > 0 && this.model.size_total > 0)
percentage = Math.round((this.model.size * 100)/this.model.size_total);
Expand Down
2 changes: 2 additions & 0 deletions fileshack/templates/fileshack/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
{% block content %}
<div id="subcontainer">
<iframe src="iframe/" id="iframe" style="display: none"></iframe>
{% if not store.readonly %}
<form id="dropbox" action="/upload/" method="post" enctype="multipart/form-data">
<span id="dropbox-text">{% trans "Drop your item in here or click" %}</span>
<span id="dropbox-text-nodragndrop" style="display: none">{% trans "Click here to upload a file" %}</span>
Expand All @@ -68,6 +69,7 @@
</div>
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}" />
</form>
{% endif %}
<div id="list">
<div id="bootstrap" style="display: none">
{{ bootstrap|escape }}
Expand Down
19 changes: 14 additions & 5 deletions fileshack/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def login_wrapper(request, *args, **kwargs):
def logout(request, store):
try:
request.session["fileshack_stores"].remove(store.id)
# FIX: we should call session.save() because django session system doesn't track
# the changes made inside of the list
request.session.save()
except (KeyError, ValueError):
pass
request.session.save()
Expand All @@ -117,7 +120,10 @@ def index(request, store):
if not request.session.has_key("fileshack_stores"):
request.session["fileshack_stores"] = [store.id]
else:
request.session["fileshack_stores"].append(store.id)
request.session["fileshack_stores"].append(store.id)
# FIX: we should call session.save() because django session system doesn't track
# the changes made inside of the list
request.session.save()
return HttpResponseRedirect(store.get_absolute_url())
else:
t = loader.get_template("fileshack/accesscode.html")
Expand Down Expand Up @@ -154,7 +160,7 @@ def iframe(request, store):
c = RequestContext(request)
return HttpResponse(t.render(c))

if not request.FILES.has_key("file"):
if not request.FILES.has_key("file") or store.readonly is True:
return HttpResponseForbidden()
f = request.FILES["file"]

Expand Down Expand Up @@ -192,7 +198,8 @@ def iframe(request, store):
@require_store
@require_login
def upload(request, store, id):
if request.method != "POST" or not request.FILES.has_key("file"):
if request.method != "POST" or not request.FILES.has_key("file") or \
store.readonly is True:
data = {
"status": "failed",
"error_label": "Upload failed",
Expand Down Expand Up @@ -318,7 +325,8 @@ def upload(request, store, id):
@require_store
@require_login
def simple_upload(request, store):
if request.method != "POST" or not request.FILES.has_key("file"):
if request.method != "POST" or not request.FILES.has_key("file") or \
store.readonly is True:
return HttpResponseRedirect(store.get_absolute_url())

#if store.item_limit and f.size > store.item_limit*1024*1024:
Expand All @@ -340,8 +348,9 @@ def delete(request, store, item_id):
return HttpResponseForbidden()

item = get_object_or_404(Item, pk=item_id, store=store)
if item.is_readonly:
return HttpResponseForbidden()
item.delete()

return HttpResponse("Item has been deleted")

@never_cache
Expand Down