#
# Copyright (C) 2007 by Johan De Taeye
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
# file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.8.0/contrib/django/freppledb/execute/views.py $
# revision : $LastChangedRevision: 1184 $ $LastChangedBy: jdetaeye $
# date : $LastChangedDate: 2010-02-20 22:54:11 +0100 (Sat, 20 Feb 2010) $
import os, os.path
from django.conf import settings
from django.core import management,serializers
from django.contrib.admin.views.decorators import staff_member_required
from django.http import Http404,HttpResponseRedirect
from django.views.decorators.cache import never_cache
from django.shortcuts import render_to_response,get_object_or_404
from django.views.generic.simple import direct_to_template
from django.utils.translation import ugettext_lazy
from execute.models import log
from common.report import *
@staff_member_required
def main(request):
'''
This view implements the overview screen with all execution
actions.
'''
try: constraint = int(request.session['constraint'])
except: constraint = 15
return direct_to_template(request, template='execute/execute.html',
extra_context={
'title': _('Execute'),
'reset_crumbs': True,
'capacityconstrained': constraint & 4,
'materialconstrained': constraint & 2,
'leadtimeconstrained': constraint & 1,
'fenceconstrained': constraint & 8,
} )
@staff_member_required
@never_cache
def erase(request):
'''
Erase the contents of the database.
'''
# Allow only post
if request.method != 'POST':
request.user.message_set.create(message='Only POST method allowed')
return HttpResponseRedirect('/execute/execute.html')
# Erase the database contents
try:
management.call_command('frepple_flush', user=request.user.username, nonfatal=True)
request.user.message_set.create(message='Erased the database')
except Exception, e:
request.user.message_set.create(message='Failure during database erasing:%s' % e)
# Redirect the page such that reposting the doc is prevented and refreshing the page doesn't give errors
return HttpResponseRedirect('/execute/execute.html')
@staff_member_required
@never_cache
def create(request):
'''
Create a sample model in the database.
'''
# Allow only post
if request.method != 'POST':
request.user.message_set.create(message='Only POST method allowed')
return HttpResponseRedirect('/execute/execute.html')
# Validate the input form data
try:
clusters = int(request.POST['clusters'])
demands = int(request.POST['demands'])
fcstqty = int(request.POST['fcst'])
levels = int(request.POST['levels'])
resources = int(request.POST['rsrc_number'])
resource_size = int(request.POST['rsrc_size'])
procure_lt = int(request.POST['procure_lt'])
components_per = int(request.POST['components_per'])
components = int(request.POST['components'])
deliver_lt = int(request.POST['deliver_lt'])
if clusters>100000 or clusters<=0 \
or fcstqty<0 or demands>=10000 or demands<0 \
or levels<0 or levels>=50 \
or resources>=1000 or resources<0 \
or resource_size>100 or resource_size<0 \
or deliver_lt<=0 or procure_lt<=0 \
or components<0 or components>=100000 \
or components_per<0:
raise ValueError("Invalid parameters")
except KeyError:
raise Http404
except ValueError, e:
request.user.message_set.create(message='Invalid input field')
else:
# Execute
try:
management.call_command('frepple_flush', user=request.user.username, nonfatal=True)
management.call_command('frepple_createmodel',
verbosity=0, cluster=clusters, demand=demands,
forecast_per_item=fcstqty, level=levels, resource=resources,
resource_size=resource_size, components=components,
components_per=components_per, deliver_lt=deliver_lt,
procure_lt=procure_lt, user=request.user.username,
nonfatal=True
)
request.user.message_set.create(message='Created sample model in the database')
except Exception, e:
request.user.message_set.create(message='Failure during sample model creation: %s' % e)
# Show the main screen again
# Redirect the page such that reposting the doc is prevented and refreshing the page doesn't give errors
return HttpResponseRedirect('/execute/')
@staff_member_required
@never_cache
def runfrepple(request):
'''
FrePPLe execution button.
'''
# Decode form input
constraint = 0
for value in request.POST.getlist('constraint'):
try: constraint += int(value)
except: pass
plantype = 1
try: plantype = request.POST.get('plantype')
except: pass
# Update the session object
request.session['plantype'] = plantype
request.session['constraint'] = constraint
# Run frepple
try:
management.call_command(
'frepple_run',
user=request.user.username,
constraint=constraint,
nonfatal=True,
plantype=plantype
)
request.user.message_set.create(message='Successfully ran frepple')
except Exception, e:
request.user.message_set.create(message='Failure when running frePPLe: %s' % e)
# Redirect the page such that reposting the doc is prevented and refreshing the page doesn't give errors
return HttpResponseRedirect('/execute/execute.html')
@staff_member_required
def fixture(request):
"""
Load a dataset stored in a django fixture file.
"""
# Validate the request
if request.method != 'POST':
request.user.message_set.create(message='Only POST method allowed')
# Redirect the page such that reposting the doc is prevented and refreshing the page doesn't give errors
return HttpResponseRedirect('/execute/execute.html')
# Decode the input data from the form
try:
fixture = request.POST['datafile']
if fixture == '-': raise
except:
request.user.message_set.create(message='Missing dataset name')
return HttpResponseRedirect('/execute/execute.html')
# Load the fixture
# The fixture loading code is unfornately such that no exceptions are
# or any error status returned when it fails...
try:
log(category='LOAD', theuser=request.user.username,
message='Start loading dataset "%s"' % fixture).save()
management.call_command('loaddata', fixture, verbosity=0)
request.user.message_set.create(message='Loaded dataset')
log(category='LOAD', theuser=request.user.username,
message='Finished loading dataset "%s"' % fixture).save()
except Exception, e:
request.user.message_set.create(message='Error while loading dataset: %s' % e)
log(category='LOAD', theuser=request.user.username,
message='Failed loading dataset "%s": %s' % (fixture,e)).save()
return HttpResponseRedirect('/execute/execute.html')
class LogReport(ListReport):
'''
A list report to review the history of actions.
'''
template = 'execute/log.html'
title = _('Command log')
reset_crumbs = True
basequeryset = log.objects.all()
default_sort = '1d'
model = log
frozenColumns = 0
editable = False
rows = (
('lastmodified', {
'title':_('last modified'),
'filter': FilterDate(),
}),
('category', {
'filter': FilterText(),
'title': _('category'),
}),
('theuser', {
'filter': FilterText(),
'title': _('user'),
}),
('message', {
'filter': FilterText(size=30),
'title':_('message'),
}),
)
|