view av_html.py @ 548:451c77e7a829

Forgot to change the year! And it is April already! (Logical change 1.161)
author optonline.net!jeffpc
date Fri, 09 Apr 2004 02:51:28 +0000
parents 0abba56409b4
children
line wrap: on
line source

#/*
# * AV Admin - Helps to manage an AV department
# *
# * Copyright (C) 2003, 2004 Josef "Jeff" Sipek
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
# *
# * This program 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 General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# *
# * @(#) %M% %I% %E% %U%
# */

from htmltmpl import TemplateManager

import av_conn
import av_log
import av_settings
import av_message

class BODY:
	def __init__(self,template=None,conn=None):
		self.init(template,conn)
		
	def init(self,template=None,conn=None):
		self.usetempl = False
		self.templ = None
		self.buff = ""
		self.conn = conn
		
		if (template is not None):
			self.usetempl = True
			self.templ = TemplateManager().prepare("templates/"+template+".tmpl")

	def append(self,text):
		self.buff += text
	
	def prepend(self,text):
		self.buff = text + self.buff
	
	def get(self):
		if (self.usetempl):
			return self.conn.tproc.process(self.templ)
		return self.buff
	
	def clear(self):
		self.buff = ""
	
	def set(self,text):
		self.clear()
		self.append(text)

class HTML:
	""" Class to hold all connection data and more """
	def __init__(self):
		""" Set all variables to their default values

		does not return anything """
		self.body = BODY()
		
	def attach(self,conn):
		self.conn = conn
		self.db = conn.db
		self.user = conn.user
		self.utils = conn.utils
		
		self.theme = av_settings.getSetting(conn,"theme")

	def content(self,ctype):
		""" Print out content type of choice """
		self.conn.tproc.set("sys_content-type", ctype)

	def header(self,title):
		""" Returns the html header

		returns string """
		self.conn.tproc.set("sys_charset", av_settings.getSetting(self.conn,"charset"))
		
		header = self.conn.parse(av_settings.getSetting(self.conn,"title"))
		if (title.__len__()>0):
			header += " - " + title
		self.conn.tproc.set("sys_header", header)
		
		self.conn.tproc.set("sys_stylesheet", self.conn.makeURL("themes/$THEME$/stylesheet.css",static=1))

		self.conn.tproc.set("sys_javascript", self.conn.makeURL("script.js",static=1))

		#text += self.menu(reqlogin,(nomenuerror,title))

	def heading(self,head):
		""" Returns the page heading in html

		returns string """
		self.conn.tproc.set("sys_title", head)
	
	def subheading(self,head):
		""" Returns the page subheading in html
	
		returns string """
		return "<div class=\"subtitle\">" + head + "</div>\n<br />\n"

	def actionlist(self,labels,urls,rows=1):
		""" Returns HTML that creates a list of
		HTML links to pages
	
		returns string """
		if (labels.__len__()!=urls.__len__()):
			return "ERROR!" # FIXME: return something more sensable
		text =  "<table class=\"plain\">\n"
		text += "<tr>\n"
	
		lpr = int(round(labels.__len__() / float(rows)))
	
		for index in range(labels.__len__()):
			text += "<td style=\"width: " + str(int(100.0/float(lpr))) + "%;\"><table class=\"action\"><td class=\"action\"><a href=\"" + urls[index] + "\">" + labels[index] + "</a></td></table></td>\n"
			if ((index+1)%lpr==0):
				text += "</tr>\n"
				if ((index+1)<labels.__len__()):
					text += "<tr>\n"
	
		text += "</table>\n"
	
		return text
	
	def areyousure(self,urls,question):
		""" Returns the html question with yes/no response URLs
	
		returns string """
		text = question + "<br />\n"
		text += self.actionlist(["Yes","No"],urls)
	
		return text;

	def menu(self,reqlogin):
		""" Returns the html menu

		returns string """

		tproc = self.conn.tproc
		templ = TemplateManager().prepare("templates/menu.tmpl")

		usesid = 0
		if (self.user.isloggedin()):
			usesid = 1
			tproc.set("sys_loggedin",1)

		# Scripts
		tproc.set("sys_main.py", self.conn.makeURL("main.py",usesid=usesid))
		tproc.set("sys_login.py", self.conn.makeURL("login.py",usesid=usesid))
		tproc.set("sys_logout.py", self.conn.makeURL("logout.py",usesid=usesid))

		# Static files
		tproc.set("sys_logo.png", self.conn.makeURL("themes/$THEME$/images/logo.png",static=1))
		
		# Rules
		tproc.set("sys_rules_ls.py", self.conn.makeURL("rules_ls.py",usesid=usesid))
		tproc.set("sys_rules_edit.py-add", self.conn.makeURL("rules_edit.py",params={"rulen":"new"},usesid=usesid))
		if (av_settings.getSetting(self.conn,"showrules")):
			tproc.set("sys_showrules",1)
			if (self.user.getPERM()["rules"]):
				tproc.set("sys_showaddrules",1)
				
		# Messages
		tproc.set("sys_mess_ls.py", self.conn.makeURL("mess_ls.py",usesid=usesid))
		tproc.set("sys_mess_comp.py", self.conn.makeURL("mess_comp.py",usesid=usesid))
		if (self.user.getPERM()["mess"]):
			tproc.set("sys_showmess",1)
			if (av_message.newMessage(self.conn)):
				tproc.set("sys_newmess"," - <b>NEW!</b>")
			if (self.user.getPERM()["messs"]):
				tproc.set("sys_showcomposemess",1)
								
		# Equipment
		tproc.set("sys_eq_ls.py", self.conn.makeURL("eq_ls.py",usesid=usesid))
		tproc.set("sys_eq_leases.py", self.conn.makeURL("eq_leases.py",usesid=usesid))
		tproc.set("sys_eq_history.py", self.conn.makeURL("eq_history.py",usesid=usesid))
		tproc.set("sys_eq_edit.py-add", self.conn.makeURL("eq_edit.py",params={"eqn":"new"},usesid=usesid))
		if (self.user.getPERM()["eq"]):
			tproc.set("sys_showeq",1)
			if (self.user.getPERM()["eqh"]):
				tproc.set("sys_showeqhist",1)
			if (self.user.getPERM()["eqa"] or self.user.getPERM()["eqm"] or self.user.getPERM()["eqr"]):
				tproc.set("sys_showeqmgr",1)
			if (self.user.getPERM()["eqa"]):
				tproc.set("sys_showeqadd",1)
		# Work
		tproc.set("sys_work_ls.py", self.conn.makeURL("work_ls.py",usesid=usesid))
		tproc.set("sys_work_status.py", self.conn.makeURL("work_status.py",usesid=usesid))
		tproc.set("sys_work_history.py", self.conn.makeURL("work_history.py",usesid=usesid))
		tproc.set("sys_work_edit.py-add", self.conn.makeURL("work_edit.py",params={"work":"new"},usesid=usesid))
		if (self.user.getPERM()["work"]):
			tproc.set("sys_showwork",1)
			if (self.user.getPERM()["workh"]):
				tproc.set("sys_showworkhist",1)
			if (self.user.getPERM()["worka"]):
				tproc.set("sys_showworkadd",1)

		# Room
		tproc.set("sys_room_ls.py", self.conn.makeURL("room_ls.py",usesid=usesid))
		tproc.set("sys_room_edit.py-add", self.conn.makeURL("room_edit.py",params={"room":"new"},usesid=usesid))
		if (self.user.getPERM()["room"]):
			tproc.set("sys_showroom",1)
		
		# User
		tproc.set("sys_user_ls.py", self.conn.makeURL("user_ls.py",usesid=usesid))
		tproc.set("sys_user_edit.py-add", self.conn.makeURL("user_edit.py",params={"user":"new"},usesid=usesid))
		if (self.user.getPERM()["user"]):
			tproc.set("sys_showuser",1)

		# Logs
		tproc.set("sys_log.py", self.conn.makeURL("log.py",usesid=usesid))
		tproc.set("sys_eq_log.py", self.conn.makeURL("eq_log.py",usesid=usesid))
		tproc.set("sys_sec_log.py", self.conn.makeURL("sec_log.py",usesid=usesid))
		tproc.set("sys_sys_log.py", self.conn.makeURL("sys_log.py",usesid=usesid))
		tproc.set("sys_user_log.py", self.conn.makeURL("user_log.py",usesid=usesid))
		tproc.set("sys_work_log.py", self.conn.makeURL("work_log.py",usesid=usesid))
		if (self.user.getPERM()["eql"] or self.user.getPERM()["userl"] or self.user.getPERM()["workl"] or self.user.getPERM()["secl"] or self.user.getPERM()["sysl"]):
			tproc.set("sys_showlog",1)
			if (self.user.getPERM()["eql"]):
				tproc.set("sys_showeqlog",1)
			if (self.user.getPERM()["secl"]):
				tproc.set("sys_showseclog",1)
			if (self.user.getPERM()["sysl"]):
				tproc.set("sys_showsyslog",1)
			if (self.user.getPERM()["userl"]):
				tproc.set("sys_showuserlog",1)
			if (self.user.getPERM()["workl"]):
				tproc.set("sys_showworklog",1)
					
		# System Settings
		tproc.set("sys_settings_ls.py", self.conn.makeURL("settings_ls.py",usesid=usesid))
		if (self.user.getPERM()["settings"]):
			tproc.set("sys_showsettings",1)

		# Passwd
		tproc.set("sys_passwd.py", self.conn.makeURL("passwd.py",usesid=usesid))

		# FIXME: if an error occured and reqlogin==0, nothing happens, user gets the page :-(
#		if ((reqlogin) and (not noerror[0]) and (not self.user.isloggedin()) and (not self.user.timedout())):
#			print self.header(noerror[1],reqlogin,1)
#			print self.heading("Error")
#			text = "ERROR" # default text, will be changed at run time
#			if (self.user.TEXTBUF["logincheckerror"]):
#				text  = self.user.TEXTBUF["logincheckerror"]
#			if (text=="ERROR"): # This should not happen
#				text = "<br />Sorry, but you must log in to access this page. To log in, select the appropreate option from the menu."
#				av_log.seclog("User attempted to access " + self.user.currentpage() + " without specifying SESSION ID")
#			print text
#			print self.footer(1)
#			self.conn.exit()
#		if (self.user.timedout() and (not noerror[0])):
#			print self.header(noerror[1],reqlogin,1)
#			print self.heading("Warning")
#			text  = "<br />Sorry, but you session expired. Whenever you are inactive for more than " + av_settings.getSetting(self.conn,"timeout") + " minute"
#			if (int(av_settings.getSetting(self.conn,"timeout")) > 1):
#				text += "s"
#			text += " you will be automatically logged out. To log back in, select the apropreate option from the menu."
#			print text
#			print self.footer(1)
#			self.conn.exit()

		tproc.set("sys_menu",tproc.process(templ))

	def footer(self,copyright=1):
		""" Prints out the html footer

		returns string """

		if (copyright):
			self.conn.tproc.set("sys_footer", "<tr><td class=\"main\"><div class=\"footer\">" + av_settings.getSetting(self.conn,"name") + " " + av_settings.getSetting(self.conn,"version") + " Copyright &copy; 2003, 2004 <a href=\"" + self.conn.makeURL("about.py") + "\">Josef &quot;Jeff&quot; Sipek</a></div>\n</td></tr>\n")

if __name__ == "__main__":
	print "ERROR!"