Commit 132cf07d by Kálmán Viktor

dashboard: add toplist

parent e5d326ea
from django.http import Http404 from django.http import Http404
import json import json
import requests import requests
import time
import django.conf import django.conf
from datetime import datetime
from sizefield.utils import filesizeformat
from common.models import method_cache
settings = django.conf.settings.STORE_SETTINGS settings = django.conf.settings.STORE_SETTINGS
...@@ -93,8 +97,6 @@ def toplist(neptun): ...@@ -93,8 +97,6 @@ def toplist(neptun):
r = post_request(url, payload) r = post_request(url, payload)
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
tupplelist = json.loads(r.content) tupplelist = json.loads(r.content)
for item in tupplelist:
item['MTIME'] = time.ctime(item['MTIME'])
return tupplelist return tupplelist
else: else:
raise Http404 raise Http404
...@@ -197,3 +199,26 @@ def updateauthorizationinfo(neptun, password, key_list): ...@@ -197,3 +199,26 @@ def updateauthorizationinfo(neptun, password, key_list):
return True return True
else: else:
return False return False
def process_list(content):
for d in content:
d['human_readable_date'] = datetime.utcfromtimestamp(float(
d['MTIME']))
delta = (datetime.utcnow() - d['human_readable_date']).total_seconds()
d['is_new'] = delta < 5 and delta > 0
d['human_readable_size'] = (
"directory" if d['TYPE'] == "D" else
filesizeformat(float(d['SIZE'])))
d['path'] = d['DIR']
if len(d['path']) == 1 and d['path'][0] == ".":
d['path'] = "/"
else:
d['path'] = "/" + d['path'] + "/"
d['path'] += d['NAME']
if d['TYPE'] == "D":
d['path'] += "/"
return sorted(content, key=lambda k: k['TYPE'])
...@@ -23,11 +23,9 @@ ...@@ -23,11 +23,9 @@
</div> </div>
{% endif %} {% endif %}
{% comment %}
<div class="col-lg-4 col-sm-6"> <div class="col-lg-4 col-sm-6">
{% include "dashboard/index-files.html" %} {% include "dashboard/store/index-files.html" %}
</div> </div>
{% endcomment %}
{% if perms.vm.create_template %} {% if perms.vm.create_template %}
<div class="col-lg-4 col-sm-6"> <div class="col-lg-4 col-sm-6">
......
{% load i18n %}
<div class="panel panel-default">
<div class="panel-heading">
<span class="btn btn-default btn-xs infobtn pull-right"
title="itten van valami">
<i class="icon-info-sign"></i>
</span>
<h3 class="no-margin"><i class="icon-briefcase"></i> Files
</h3>
</div>
<div class="list-group" id="vm-list-view">
{% for t in toplist %}
{% if t.TYPE == "F" %}
<div class="list-group-item">
<i class="icon-file"></i> {{ t.NAME }}
<a href="{% url "dashboard.views.store-download" %}?path={{ t.path }}"
class="pull-right btn btn-xs" style="color: black;">
<i class="icon-cloud-download" title="{% trans "Download" %}"></i>
</a>
</div>
{% else %}
<a href="{% url "dashboard.views.store-list" %}?directory={{ t.path }}" class="list-group-item">
<i class="icon-folder-open"></i> {{ t.NAME }}
</a>
{% endif %}
{% endfor %}
<div class="list-group-item list-group-footer text-right">
<p>
<form class="pull-left" method="POST" action="{% url "dashboard.views.store-refresh-toplist" %}">
{% csrf_token %}
<button class="btn btn-success btn-xs" type="submit" title="{% trans "Refresh" %}"/>
<i class="icon-refresh"></i>
</button>
</form>
<a href="{% url "dashboard.views.store-list" %}" class="btn btn-primary btn-xs">
<i class="icon-chevron-sign-right"></i> {% trans "show my files" %}
</a>
<a href="{% url "dashboard.views.store-upload" %}" class="btn btn-success btn-xs">
<i class="icon-upload-alt"></i> {% trans "upload" %}
</a>
</p>
</div>
</div>
</div>
...@@ -38,7 +38,7 @@ from .views import ( ...@@ -38,7 +38,7 @@ from .views import (
ProfileView, toggle_use_gravatar, UnsubscribeFormView, ProfileView, toggle_use_gravatar, UnsubscribeFormView,
UserKeyDelete, UserKeyDetail, UserKeyCreate, UserKeyDelete, UserKeyDetail, UserKeyCreate,
StoreList, store_download, store_upload, store_get_upload_url, StoreRemove, StoreList, store_download, store_upload, store_get_upload_url, StoreRemove,
store_new_directory, store_new_directory, store_refresh_toplist
) )
urlpatterns = patterns( urlpatterns = patterns(
...@@ -184,4 +184,6 @@ urlpatterns = patterns( ...@@ -184,4 +184,6 @@ urlpatterns = patterns(
name="dashboard.views.store-remove"), name="dashboard.views.store-remove"),
url(r"^store/new_directory/$", store_new_directory, url(r"^store/new_directory/$", store_new_directory,
name="dashboard.views.store-new-directory"), name="dashboard.views.store-new-directory"),
url(r"^store/refresh_toplist$", store_refresh_toplist,
name="dashboard.views.store-refresh-toplist"),
) )
...@@ -35,6 +35,7 @@ from django.contrib.messages.views import SuccessMessageMixin ...@@ -35,6 +35,7 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.core.exceptions import ( from django.core.exceptions import (
PermissionDenied, SuspiciousOperation, PermissionDenied, SuspiciousOperation,
) )
from django.core.cache import get_cache
from django.core import signing from django.core import signing
from django.core.urlresolvers import reverse, reverse_lazy from django.core.urlresolvers import reverse, reverse_lazy
from django.db.models import Count from django.db.models import Count
...@@ -204,6 +205,15 @@ class IndexView(LoginRequiredMixin, TemplateView): ...@@ -204,6 +205,15 @@ class IndexView(LoginRequiredMixin, TemplateView):
context['templates'] = InstanceTemplate.get_objects_with_level( context['templates'] = InstanceTemplate.get_objects_with_level(
'operator', user).all()[:5] 'operator', user).all()[:5]
# toplist
cache = get_cache("default")
toplist = cache.get("toplist-test")
if not toplist:
toplist = store_api.process_list(store_api.toplist("test"))
cache.set("toplist-test", toplist, 300)
context['toplist'] = toplist
return context return context
...@@ -2948,7 +2958,8 @@ class StoreList(LoginRequiredMixin, TemplateView): ...@@ -2948,7 +2958,8 @@ class StoreList(LoginRequiredMixin, TemplateView):
directory = self.request.GET.get("directory", "/") directory = self.request.GET.get("directory", "/")
directory = "/" if not len(directory) else directory directory = "/" if not len(directory) else directory
context['root'] = self.clean_directory_list(directory) content = store_api.listfolder("test", directory)
context['root'] = store_api.process_list(content)
context['up_url'] = self.create_up_directory(directory) context['up_url'] = self.create_up_directory(directory)
context['current'] = directory context['current'] = directory
context['next_url'] = "%s%s?directory=%s" % ( context['next_url'] = "%s%s?directory=%s" % (
...@@ -2970,34 +2981,6 @@ class StoreList(LoginRequiredMixin, TemplateView): ...@@ -2970,34 +2981,6 @@ class StoreList(LoginRequiredMixin, TemplateView):
cut = -2 if directory.endswith("/") else -1 cut = -2 if directory.endswith("/") else -1
return "/".join(directory.split("/")[:cut]) + "/" return "/".join(directory.split("/")[:cut]) + "/"
def clean_directory_list(self, directory):
from datetime import datetime
from sizefield.utils import filesizeformat
content = store_api.listfolder("test", directory)
for d in content:
d['human_readable_date'] = datetime.utcfromtimestamp(float(
d['MTIME']))
delta = (datetime.utcnow() - d['human_readable_date']
).total_seconds()
d['is_new'] = delta < 5 and delta > 0
d['human_readable_size'] = (
"directory" if d['TYPE'] == "D" else
filesizeformat(float(d['SIZE'])))
d['path'] = d['DIR']
if len(d['path']) == 1 and d['path'][0] == ".":
d['path'] = "/"
else:
d['path'] = "/" + d['path'] + "/"
d['path'] += d['NAME']
if d['TYPE'] == "D":
d['path'] += "/"
return sorted(content, key=lambda k: k['TYPE'])
@require_GET @require_GET
@login_required @login_required
...@@ -3010,7 +2993,7 @@ def store_download(request): ...@@ -3010,7 +2993,7 @@ def store_download(request):
@require_GET @require_GET
@login_required @login_required
def store_upload(request): def store_upload(request):
directory = request.GET.get("directory") directory = request.GET.get("directory", "/")
action = store_api.requestupload("test", directory) action = store_api.requestupload("test", directory)
next_url = "%s%s?directory=%s" % ( next_url = "%s%s?directory=%s" % (
settings.DJANGO_URL[:-1], reverse("dashboard.views.store-list"), settings.DJANGO_URL[:-1], reverse("dashboard.views.store-list"),
...@@ -3074,3 +3057,12 @@ def store_new_directory(request): ...@@ -3074,3 +3057,12 @@ def store_new_directory(request):
store_api.requestnewfolder("test", path + name) store_api.requestnewfolder("test", path + name)
return redirect("%s?directory=%s" % ( return redirect("%s?directory=%s" % (
reverse("dashboard.views.store-list"), path)) reverse("dashboard.views.store-list"), path))
@require_POST
@login_required
def store_refresh_toplist(request):
cache = get_cache("default")
toplist = store_api.process_list(store_api.toplist("test"))
cache.set("toplist-test", toplist, 300)
return redirect(reverse("dashboard.index"))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment