
Some specific Catalyst Cloud stuff are here and there, they will be cleaned up later after we migrated to Distil v2. Change-Id: I81df0765cfc876faaa58a43469d87825d30c167e
105 lines
3.7 KiB
Python
105 lines
3.7 KiB
Python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
from __future__ import division
|
|
|
|
import datetime
|
|
|
|
from django.utils import timezone
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from horizon import forms
|
|
from horizon import messages
|
|
|
|
|
|
class BaseBilling(object):
|
|
|
|
def __init__(self, request, project_id=None):
|
|
self.project_id = project_id or request.user.tenant_id
|
|
self.request = request
|
|
self.billing_list = []
|
|
|
|
@property
|
|
def today(self):
|
|
return timezone.now()
|
|
|
|
@staticmethod
|
|
def get_start(year, month, day):
|
|
start = datetime.datetime(year, month, day, 0, 0, 0)
|
|
return timezone.make_aware(start, timezone.utc)
|
|
|
|
@staticmethod
|
|
def get_end(year, month, day):
|
|
end = datetime.datetime(year, month, day, 23, 59, 59)
|
|
return timezone.make_aware(end, timezone.utc)
|
|
|
|
def get_date_range(self):
|
|
if not hasattr(self, "start") or not hasattr(self, "end"):
|
|
args_start = (self.today.year, self.today.month, 1)
|
|
args_end = (self.today.year, self.today.month, self.today.day)
|
|
form = self.get_form()
|
|
if form.is_valid():
|
|
start = form.cleaned_data['start']
|
|
end = form.cleaned_data['end']
|
|
args_start = (start.year,
|
|
start.month,
|
|
start.day)
|
|
args_end = (end.year,
|
|
end.month,
|
|
end.day)
|
|
elif form.is_bound:
|
|
messages.error(self.request,
|
|
_("Invalid date format: "
|
|
"Using today as default."))
|
|
self.start = self.get_start(*args_start)
|
|
self.end = self.get_end(*args_end)
|
|
return self.start, self.end
|
|
|
|
def init_form(self):
|
|
today = datetime.date.today()
|
|
self.start = datetime.date(day=1, month=today.month, year=today.year)
|
|
self.end = today
|
|
|
|
return self.start, self.end
|
|
|
|
def get_form(self):
|
|
if not hasattr(self, 'form'):
|
|
req = self.request
|
|
start = req.GET.get('start', req.session.get('billing_start'))
|
|
end = req.GET.get('end', req.session.get('billing_end'))
|
|
if start and end:
|
|
# bound form
|
|
self.form = forms.DateForm({'start': start, 'end': end})
|
|
else:
|
|
# non-bound form
|
|
init = self.init_form()
|
|
start = init[0].isoformat()
|
|
end = init[1].isoformat()
|
|
self.form = forms.DateForm(initial={'start': start,
|
|
'end': end})
|
|
req.session['billing_start'] = start
|
|
req.session['billing_end'] = end
|
|
return self.form
|
|
|
|
def get_billing_list(self, start, end):
|
|
return []
|
|
|
|
def csv_link(self):
|
|
form = self.get_form()
|
|
data = {}
|
|
if hasattr(form, "cleaned_data"):
|
|
data = form.cleaned_data
|
|
if not ('start' in data and 'end' in data):
|
|
data = {"start": self.today.date(), "end": self.today.date()}
|
|
return "?start=%s&end=%s&format=csv" % (data['start'],
|
|
data['end'])
|