This commit is contained in:
age 2008-04-27 06:31:57 +00:00
parent f9fd39d83b
commit 0aeba866bc
3 changed files with 148 additions and 46 deletions

View file

@ -12,11 +12,11 @@ class DataBase:
user="szene", \
passwd="szene", \
use_unicode=1)
print "Verbindung zur DB steht"
print "Connection to databse established"
def close (self):
self.db.close()
print "Verbindung getrennt"
print "Connection to database detached"
def get_latest_uid_category(self, type):
c = self.db.cursor()
@ -40,21 +40,64 @@ class DataBase:
print "latest uid for %s is: %i" % (type, j)
return j
def prepare_new_month(self, month):
#TODO Monatsvorlage rekursiv kopieren
# IDs der Kategorien aus config
next_month = str(month)
parentid = str(34)
def create_rubrik(self, rubrik, parentid, new_month):
latest_month_id = ("SELECT `uid` FROM `pages` WHERE `title` = '" + new_month + "' AND `pid` = '""" + parentid + "'")
self.db.query(latest_month_id)
monthid = self.db.store_result().fetch_row()[0][0]
#print "%s %s %s" % (rubrik, parentid, new_month)
#print monthid
#monthid = monthid[0][0]
get_rubrik_id = ("""SELECT `uid` FROM `pages` WHERE `title` = '"""+ rubrik + """' AND `pid` = '""" + str(monthid) + """' LIMIT 1""")
self.db.query(get_rubrik_id)
rubrik_id = self.db.store_result().fetch_row()
## create dir for the rubrik if it doesn't exist yet
if not rubrik_id:
sqlqueue = " \
INSERT INTO `pages` (`pid`,`title`,`doktype`,`urltype`,`tx_rgmenu_special`,`sorting`,`cruser_id`) \
VALUES ("\
+ "'" + str(monthid) + "', " \
+ "'" + str(rubrik) + \
"', '254','1','5','8','1');"
self.db.query(sqlqueue)
print "Created rubrik %s under monthid %s pid %s" % (rubrik, parentid, monthid)
#print sqlqueue
get_rubrik_id = ("""SELECT `uid` FROM `pages` WHERE `title` = '"""+ rubrik + """' AND `pid` = '""" + str(monthid) + """' LIMIT 1""")
self.db.query(get_rubrik_id)
rubrik_id = self.db.store_result().fetch_row()[0][0]
if not rubrik_id:
print "Couldn't create rubrik %s" % rubrik
sys.exit()
return rubrik_id
def prepare_new_month(self, parentid, month):
'''create a new folder in typo3 under the given parentid
'''
new_month = str(month)
#parentid = str(34)
doktype = str(254)
perms_user = str(31)
perms_group = str(27)
hidden = str(1)
tx_rgmenu_special = str(5)
tstamp = str(int(mktime(datetime.now().timetuple())))
## delete entries with same parent id and month to avoid duplicates
#latest_month_id = ("DELETE FROM `pages` WHERE `title` = '" + new_month + "' AND `pid` = '" + parentid + "'")
## better don't delete, just inform
latest_month_id = ("""SELECT `uid` FROM `pages` WHERE `title` = '"""+ new_month + """' AND `pid` = '""" + parentid + """' LIMIT 1""")
self.db.query(latest_month_id)
monthid = self.db.store_result().fetch_row()
self.monthid = monthid
if monthid:
print "Caution, the month >%s< alread exists in this dir >%s< !" % (new_month, parentid)
print "It's ID is >%s<. I'm cowardly stopping now." % monthid[0][0]
print "Rename or delete it manually to go on with this action."
return
## insert new month (as typo3 folder)
sqlqueue="INSERT INTO `pages` (`pid`, `title`, `doktype`, `tstamp`, `perms_user`, `perms_group`, `hidden`, `tx_rgmenu_special`) \
VALUES (" \
+ "'" + parentid + "', " \
+ "'" + next_month + "', " \
+ "'" + new_month + "', " \
+ "'" + doktype + "', " \
+ "'" + tstamp + "', " \
+ "'" + perms_user + "', " \
@ -62,18 +105,62 @@ class DataBase:
+ "'" + hidden + "', " \
+ "'" + tx_rgmenu_special \
+ "');"
print sqlqueue
#print sqlqueue
c = self.db.cursor()
c.execute(sqlqueue)
## neues Verz fuer Kategorie anlegen
#INSERT INTO 'pages' ('pid','title','doktype','urltype','tx_rgmenu_special','sorting','cruser_id') VALUES ('89','Ballermann','254','1','5','8''1');
## get uid of newly created month, there should be exactly one
latest_month_id = ("SELECT `uid` FROM `pages` WHERE `title` = '" + new_month + "' AND `pid` = '""" + parentid + "'")
self.db.query(latest_month_id)
monthid = self.db.store_result().fetch_row()[0][0]
print "The new month has the ID >%s<" % monthid
def prepare_event(self, event_dic, month, day):
'''
## create categories
for title in categories:
sqlqueue = " \
INSERT INTO `pages` (`pid`,`title`,`doktype`,`urltype`,`tx_rgmenu_special`,`sorting`,`cruser_id`) \
VALUES ("\
+ "'" + str(monthid) + "', " \
+ "'" + title + \
"', '254','1','5','8','1');"
c.execute(sqlqueue)
print sqlqueue
'''
def delete_month_entries(self, monthid):
'''create a new folder in typo3 under the given parentid
'''
c = self.db.cursor()
rubrik_query = ("""SELECT `uid` FROM `pages` WHERE `pid` = """ + monthid )
c.execute(rubrik_query)
rubrik_ids = c.fetchall()
print rubrik_ids
for r_id in rubrik_ids:
self.delete_event_in_rubrik(r_id)
def delete_event_in_rubrik(self, rubrikid):
print rubrikid[0]
rubrikid = str(rubrikid[0])
#delete_query = ("""SELECT `uid` FROM `tx_skcalendar_event` WHERE `pid` = """ + rubrikid )
delete_query = ("""DELETE FROM `tx_skcalendar_event` WHERE `pid` = """ + rubrikid )
c = self.db.cursor()
c.execute(delete_query)
#rubrik_ids = c.fetchall()
#print rubrik_ids
def prepare_event(self, event_dic, month, day, parentid, rubrikid):
'''hiermit wird eine neue Veranstaltung in passender Form
fuer die VCE Extension vorbereitet'''
event_pid = "29"
event_title = event_dic["WAS_XML"]
## event_pid is the uid of the current rubrik
event_pid = rubrikid
if int(event_pid) <= int(parentid):
print "Event ID is smaller than parent ID."
print "%s <= %s" % (event_pid, parentid)
print "This should not happen, so I stop processing here."
sys.exit()
event_title = "<p class=\"uhrzeit\">%s</p><p class=\"titel\">%s</p><p class=\"veranst\">%s</p>" \
% (event_dic["WANN_START_XML"], event_dic["WAS_XML"], event_dic["WO_XML"])
event_description = event_dic["INFORMATION"]
event_startdate = self.calc_correct_day(month, day)
event_targetgroup = "5"

View file

@ -1,8 +1,6 @@
[month]
## welcher Monat soll eingepflegt werden (als Zahl)
number = 5
## Typo3 ID des Monatsverzeichnisses
pid = 89
number = 8
[organizer]
pid = 1
@ -15,16 +13,10 @@ pid = 1
######################################################################
## ab hier nichts aendern, es sei denn der XML Export wurde veraendert
[xmltags]
tags = "INFORMATION", "WANN_START_XML", "WANN_ENDE", "WO_XML", "WAS_XML", "RUBRIK_XML", "SONSTIGES"
[static]
## Typo3 ID des Kalenderverz. (Hauptverzeichnis)
pid = 34
## Namen der XML Tags
xmltags = "INFORMATION", "WANN_START_XML", "WANN_ENDE", "WO_XML", "WAS_XML", "RUBRIK_XML", "SONSTIGES"
[category]
pid = 58
Live = 67
Sonstiges = 66
Lesungen = 65
Bar = 64
Sport = 63
Familie = 61
Party = 60
Kino = 59

View file

@ -9,10 +9,14 @@ import os, mysql_connect, configobj
def main():
usage = "usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option("-f", "--file", dest="filename",
help="read xml data from FILENAME")
parser.add_option("--insert-xml", dest="filename",
help="read events from xml FILENAME")
parser.add_option("-c", "--config", dest="configfile",
help="read DB config from FILENAME")
parser.add_option("--add-month", dest="newmonth", default=False, action="store_true",
help="create new template for a month")
parser.add_option("--delete-events", dest="delevents",
help="delete alle events under a month ID")
parser.add_option("-v", "--verbose", dest="verbose",
help="change Loglevel (0-3)")
(options, args) = parser.parse_args()
@ -27,15 +31,31 @@ def main():
else:
print parser.print_help()
return
if options.filename:
print "reading XML file: %s " % options.filename
if not os.access(options.filename, os.R_OK) or not os.path.isfile(options.filename):
print "Error: could not read configuration file (%s)\n" % options.filename
return
else:
print parser.print_help()
#else:
#print parser.print_help()
#return
if options.newmonth:
create_new_month(prefs["static"]["pid"], prefs["month"]["number"])
return
if options.delevents:
test = mysql_connect.DataBase()
test.connect()
test.delete_month_entries(options.delevents)
test.close()
return
#if not options.filename or not options.newmonth:
#print parser.print_help()
#return
loglevel = 0
if options.verbose: loglevel = int(options.verbose)
@ -43,13 +63,9 @@ def main():
dic = readXML(options.filename)
if "xmltags" in prefs.keys() :
xmltags = prefs["xmltags"]["tags"]
if loglevel >= 2: print xmltags
else:
print "Warning: using hardcoded XML structure.. "
xmltags = ["INFORMATION", "WANN_START_XML", "WANN_ENDE", "WO_XML", "WAS_XML", "RUBRIK_XML", "SONSTIGES"]
print
xmltags = prefs["static"]["xmltags"]
if loglevel >= 2: print xmltags
if loglevel >= 1: print "XML Quelle: ", dic["FMPDSORESULT"]["DATABASE"]
#print "Data 0", dic["FMPDSORESULT"]["ROW"][0]
#print "Data 1", dic["FMPDSORESULT"]["ROW"][1]
@ -57,7 +73,9 @@ def main():
month = prefs["month"]["number"]
days = 0
data = []
rubrikid = prefs["static"]["pid"]
test = mysql_connect.DataBase()
test.connect()
for item in dic["FMPDSORESULT"]["ROW"]:
entry += 1
@ -68,7 +86,9 @@ def main():
#f = open(filename,mode="a")
#f.write(text)
#print text
data.append(test.prepare_event(item, month, days))
if len(item["RUBRIK_XML"]) > 1:
rubrikid = test.create_rubrik(item["RUBRIK_XML"], prefs["static"]["pid"], month)
data.append(test.prepare_event(item, month, days, prefs["static"]["pid"], rubrikid))
'''
for content in xmltags:
text = ("%s: %s") % (content, item[content])
@ -85,10 +105,8 @@ def main():
print "Es gibt %i Eintraege in der xml Datei aber nur %i wurden durchlaufen" % (entries, entry)
## finally put the mess into mysql db
test.connect()
print "Datenbank fuellen.."
#test.prepare_new_month("Mai")
#test.insert_event(data)
test.insert_event(data)
test.close()
@ -167,8 +185,6 @@ def readXML(filename):
return nodeToDic(dom)
def get_preferences(config_file):
"""Load the CryptoBox configuration.
"""
import StringIO
try:
prefs = configobj.ConfigObj(config_file)
@ -183,6 +199,13 @@ def get_preferences(config_file):
raise CBConfigError("failed to load config file (%s): %s" % (config_file, err_msg))
return prefs
def create_new_month(parentid, month):
print "Creating new monthly template.."
db = mysql_connect.DataBase()
db.connect()
db.prepare_new_month(parentid, month)
db.close()
if __name__ == "__main__":
main()