mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-11 05:17:55 +03:00
done accounting usage of services
This commit is contained in:
parent
310d3ca618
commit
9e2a1cc9f9
@ -35,6 +35,7 @@ from __future__ import unicode_literals
|
||||
from django.utils.translation import ugettext_lazy as _, ugettext
|
||||
from uds.models import Account
|
||||
from uds.core.util import permissions
|
||||
import datetime
|
||||
|
||||
from uds.REST.model import ModelHandler
|
||||
|
||||
@ -54,7 +55,7 @@ class Accounts(ModelHandler):
|
||||
model = Account
|
||||
detail = {'usage': AccountsUsage }
|
||||
|
||||
custom_methods = [('clear', True)]
|
||||
custom_methods = [('clear', True), ('timemark', True)]
|
||||
|
||||
save_fields = ['name', 'comments', 'tags']
|
||||
|
||||
@ -62,7 +63,7 @@ class Accounts(ModelHandler):
|
||||
table_fields = [
|
||||
{'name': {'title': _('Name'), 'visible': True}},
|
||||
{'comments': {'title': _('Comments')}},
|
||||
{'time_mark': {'title': _('Time mark')}},
|
||||
{'time_mark': {'title': _('Time mark'), 'type': 'callback'}},
|
||||
{'tags': {'title': _('tags'), 'visible': False}},
|
||||
]
|
||||
|
||||
@ -79,6 +80,11 @@ class Accounts(ModelHandler):
|
||||
def getGui(self, type_):
|
||||
return self.addDefaultFields([], ['name', 'comments', 'tags'])
|
||||
|
||||
def timemark(self, item):
|
||||
item.time_mark = datetime.datetime.now()
|
||||
item.save()
|
||||
return
|
||||
|
||||
def clear(self, item):
|
||||
self.ensureAccess(item, permissions.PERMISSION_MANAGEMENT)
|
||||
return item.usages.filter(user_service=None).delete()
|
||||
|
@ -74,6 +74,8 @@ class AccountsUsage(DetailHandler): # pylint: disable=too-many-public-methods
|
||||
'start': item.start,
|
||||
'end': item.end,
|
||||
'running': item.user_service is not None,
|
||||
'elapsed': item.elapsed,
|
||||
'elapsed_timemark': item.elapsed_timemark,
|
||||
'permission': perm
|
||||
}
|
||||
|
||||
@ -99,6 +101,8 @@ class AccountsUsage(DetailHandler): # pylint: disable=too-many-public-methods
|
||||
{'running': {'title': _('Running')}},
|
||||
{'start': {'title': _('Starts'), 'type': 'datetime'}},
|
||||
{'end': {'title': _('Ends'), 'type': 'datetime'}},
|
||||
{'elapsed': {'title': _('Elapsed')}},
|
||||
{'elapsed_timemark': {'title': _('Elapsed timemark')}},
|
||||
]
|
||||
|
||||
def getRowStyle(self, parent):
|
||||
|
@ -33,6 +33,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.utils import formats
|
||||
from django.utils.translation import ugettext
|
||||
import django.template.defaultfilters as filters
|
||||
|
||||
import datetime
|
||||
@ -40,7 +41,7 @@ import datetime
|
||||
import sys
|
||||
import os
|
||||
|
||||
__updated__ = '2015-05-04'
|
||||
__updated__ = '2017-01-31'
|
||||
|
||||
|
||||
class DictAsObj(object):
|
||||
@ -77,3 +78,22 @@ def timestampAsStr(stamp, format_='SHORT_DATETIME_FORMAT'):
|
||||
'''
|
||||
format_ = formats.get_format(format_)
|
||||
return filters.date(datetime.datetime.fromtimestamp(stamp), format_)
|
||||
|
||||
def secondsToString(seconds):
|
||||
seconds = int(seconds)
|
||||
minutes = seconds / 60
|
||||
seconds %= 60
|
||||
hours = minutes / 60
|
||||
minutes %= 60
|
||||
days = hours / 24
|
||||
hours %= 24
|
||||
res = []
|
||||
if days > 0:
|
||||
res.append(ugettext('{} days').format(days))
|
||||
if hours > 0:
|
||||
res.append(ugettext('{} hours').format(hours))
|
||||
if minutes > 0:
|
||||
res.append(ugettext('{} minutes').format(minutes))
|
||||
res.append(ugettext('{} seconds').format(seconds))
|
||||
|
||||
return ', '.join(res)
|
||||
|
@ -31,7 +31,7 @@
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
__updated__ = '2016-09-21'
|
||||
__updated__ = '2017-01-31'
|
||||
|
||||
from django.db import models
|
||||
|
||||
@ -39,6 +39,7 @@ from uds.models.UUIDModel import UUIDModel
|
||||
from uds.models.Account import Account
|
||||
from uds.models.UserService import UserService
|
||||
from uds.models.Util import NEVER
|
||||
from uds.core.util.tools import secondsToString
|
||||
|
||||
import logging
|
||||
|
||||
@ -68,5 +69,34 @@ class AccountUsage(UUIDModel):
|
||||
db_table = 'uds_acc_usage'
|
||||
app_label = 'uds'
|
||||
|
||||
@property
|
||||
def elapsed_seconds(self):
|
||||
if self.end == NEVER or self.start == NEVER:
|
||||
return 0
|
||||
return (self.end - self.start).total_seconds()
|
||||
|
||||
@property
|
||||
def elapsed_seconds_timemark(self):
|
||||
if self.end == NEVER or self.start == NEVER:
|
||||
return 0
|
||||
|
||||
start = self.start
|
||||
end = self.end
|
||||
if start < self.account.time_mark:
|
||||
start = self.account.time_mark
|
||||
if end < start:
|
||||
return 0
|
||||
|
||||
return (end - start).total_seconds()
|
||||
|
||||
@property
|
||||
def elapsed(self):
|
||||
return secondsToString(self.elapsed_seconds)
|
||||
|
||||
@property
|
||||
def elapsed_timemark(self):
|
||||
return secondsToString(self.elapsed_seconds_timemark)
|
||||
|
||||
|
||||
def __unicode__(self):
|
||||
return 'AccountUsage id {}, pool {}, name {}, start {}, end {}'.format(self.id, self.pool_name, self.user_name, self.start, self.end)
|
||||
|
@ -444,7 +444,13 @@ api.sPoolGroups = new BasicModelRest("gallery/servicespoolgroups")
|
||||
api.system = new BasicModelRest("system")
|
||||
api.reports = new BasicModelRest("reports") # Not fully used, but basic usage is common
|
||||
api.calendars = new BasicModelRest("calendars")
|
||||
|
||||
api.accounts = new BasicModelRest("accounts")
|
||||
api.accounts.timemark = (id, success_fnc, fail_fnc) ->
|
||||
@get
|
||||
id: id + '/timemark'
|
||||
success: success_fnc
|
||||
fail: fail_fnc
|
||||
|
||||
# In fact, reports do not have any type
|
||||
api.reports.types = (success_fnc, fail_fnc) ->
|
||||
|
@ -3,6 +3,15 @@ gui.accounts = new GuiElement(api.accounts, "accounts")
|
||||
gui.accounts.link = (event) ->
|
||||
"use strict"
|
||||
|
||||
dateRenderer = gui.tools.renderDate(api.tools.djangoFormat(get_format("SHORT_DATETIME_FORMAT")))
|
||||
# Callback for custom fields
|
||||
renderer = (fld, data, type, record) ->
|
||||
# Display "custom" fields of rules table
|
||||
if fld == "time_mark"
|
||||
if data == 78793200
|
||||
return gettext('No Time Mark')
|
||||
return dateRenderer(data)
|
||||
return fld
|
||||
|
||||
|
||||
useTable = undefined
|
||||
@ -27,6 +36,7 @@ gui.accounts.link = (event) ->
|
||||
gui.accounts.table
|
||||
icon: 'accounts'
|
||||
container: "accounts-placeholder"
|
||||
callback: renderer
|
||||
rowSelect: "single"
|
||||
|
||||
onRefresh: (tbl) ->
|
||||
@ -67,6 +77,30 @@ gui.accounts.link = (event) ->
|
||||
buttons: [
|
||||
"new"
|
||||
"edit"
|
||||
{
|
||||
text: gui.tools.iconAndText( 'fa-calendar', gettext('Set time mark') )
|
||||
css: "disabled"
|
||||
disabled: true
|
||||
click: (vals, value, btn, tbl, refreshFnc) ->
|
||||
val = vals[0]
|
||||
gui.forms.confirmModal gettext("Time Mark"), gettext("Set timemark to current datetime?"),
|
||||
onYes: ->
|
||||
gui.accounts.rest.timemark vals[0].id + "/timemark", ->
|
||||
refreshFnc()
|
||||
return
|
||||
|
||||
return
|
||||
|
||||
return
|
||||
|
||||
select: (vals, value, btn, tbl, refreshFnc) ->
|
||||
unless vals.length == 1
|
||||
$(btn).addClass("disabled").prop('disabled', true)
|
||||
return
|
||||
|
||||
$(btn).removeClass("disabled").prop('disabled', false)
|
||||
|
||||
}
|
||||
"delete"
|
||||
"xls"
|
||||
"permissions"
|
||||
|
Loading…
Reference in New Issue
Block a user