codekasten/xml2typo3/mysql_connect.py

238 lines
9.2 KiB
Python
Raw Normal View History

2008-01-03 20:59:36 +01:00
#/usr/bin/env python
import sys
import MySQLdb
2008-01-11 04:55:55 +01:00
from time import *
from datetime import *
2008-01-03 20:59:36 +01:00
2008-01-11 04:55:55 +01:00
class DataBase:
2008-04-16 16:19:32 +02:00
def connect(self):
self.db = MySQLdb.connect(db="szene",\
host="localhost", \
user="szene", \
passwd="szene", \
use_unicode=1)
2008-04-27 08:31:57 +02:00
print "Connection to databse established"
2008-01-06 15:20:10 +01:00
2008-04-16 16:19:32 +02:00
def close (self):
self.db.close()
2008-04-27 08:31:57 +02:00
print "Connection to database detached"
2008-01-11 04:55:55 +01:00
2008-04-16 16:19:32 +02:00
def get_latest_uid_category(self, type):
c = self.db.cursor()
sqlqueue="SELECT uid FROM tx_skcalendar_" + type
c.execute(sqlqueue)
j=1
for i in c:
if i[0] > j:
j = i[0]
print "latest uid for %s is: %i" % (type, j)
return j
2008-01-03 20:59:36 +01:00
2008-04-19 11:51:31 +02:00
def get_latest_monthid(self, type):
c = self.db.cursor()
sqlqueue="SELECT uid FROM tx_skcalendar_" + type
c.execute(sqlqueue)
j=1
for i in c:
if i[0] > j:
j = i[0]
print "latest uid for %s is: %i" % (type, j)
return j
2008-04-27 08:31:57 +02:00
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)
2008-04-16 16:19:32 +02:00
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())))
2008-04-27 08:31:57 +02:00
## 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)
2008-04-16 16:19:32 +02:00
sqlqueue="INSERT INTO `pages` (`pid`, `title`, `doktype`, `tstamp`, `perms_user`, `perms_group`, `hidden`, `tx_rgmenu_special`) \
VALUES (" \
+ "'" + parentid + "', " \
2008-04-27 08:31:57 +02:00
+ "'" + new_month + "', " \
2008-04-16 16:19:32 +02:00
+ "'" + doktype + "', " \
+ "'" + tstamp + "', " \
+ "'" + perms_user + "', " \
+ "'" + perms_group + "', " \
+ "'" + hidden + "', " \
+ "'" + tx_rgmenu_special \
+ "');"
2008-04-27 08:31:57 +02:00
#print sqlqueue
2008-04-16 16:19:32 +02:00
c = self.db.cursor()
c.execute(sqlqueue)
2008-01-11 21:39:49 +01:00
2008-04-27 08:31:57 +02:00
## 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
'''
## 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
2008-04-19 11:51:31 +02:00
2008-04-27 08:31:57 +02:00
def prepare_event(self, event_dic, month, day, parentid, rubrikid):
2008-04-16 16:19:32 +02:00
'''hiermit wird eine neue Veranstaltung in passender Form
fuer die VCE Extension vorbereitet'''
2008-04-27 08:31:57 +02:00
## 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"])
2008-04-16 16:19:32 +02:00
event_description = event_dic["INFORMATION"]
2008-04-19 11:51:31 +02:00
event_startdate = self.calc_correct_day(month, day)
2008-04-16 16:19:32 +02:00
event_targetgroup = "5"
event_category = "1"
event_organizer = "1"
event_location = "1"
event_tstamp = str(int(mktime(datetime.now().timetuple())))
event_crdate = str(int(mktime(datetime.now().timetuple())))
## felder ohne default wert mit leerem string besetzen
event_image = ""
event_link = ""
event_exeptions = ""
event_l18n_diffsource = ""
2008-04-19 11:51:31 +02:00
event = (event_pid, event_title, event_description, event_startdate, event_targetgroup, event_category, event_organizer, event_location, event_tstamp, event_crdate, event_image, event_link, event_exeptions, event_l18n_diffsource)
#print event
return event
2008-01-03 20:59:36 +01:00
2008-04-16 16:19:32 +02:00
def insert_event(self, dataset):
'''den MySQL Befehl absetzen'''
#sqlqueue="INSERT INTO `tx_skcalendar_event` (`pid`) VALUES ('29');"
#c.execute(sqlqueue)
'''
sqlqueue="INSERT INTO `tx_skcalendar_event` (`pid` , `title`, `description`, `targetgroup`, `category`, `organizer`, `location`, `tstamp` , `crdate` ) VALUES (" \
+ "'" + self.event_pid + "', "\
+ "'" + self.event_title + "', "\
+ "'" + self.event_description + "', "\
+ "'" + self.event_targetgroup + "', "\
+ "'" + self.event_category + "', "\
+ "'" + self.event_organizer + "', "\
+ "'" + self.event_location + "', "\
+ "'" + self.event_tstamp + "', "\
+ "'" + self.event_crdate \
+ "');"
print "folgende Datensaetze einfuegen:"
for i in dataset:
print i
#c.executemany("""INSERT INTO `tx_skcalendar_event` (`pid` , `title`, `description`, `startdate`, `targetgroup`, `category`, `organizer`, `location`, `tstamp` , `crdate`, `image`, `link`, `exeptions`, `l18n_diffsource`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", data)
'''
#data = self.prepare_event()
c = self.db.cursor()
c.executemany("""INSERT INTO `tx_skcalendar_event` (`pid` , `title`, `description`, `startdate`, `targetgroup`, `category`, `organizer`, `location`, `tstamp` , `crdate`, `image`, `link`, `exeptions`, `l18n_diffsource`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", dataset)
2008-01-11 04:55:55 +01:00
2008-04-16 16:19:32 +02:00
def insert_organizer(self):
#get_organizers_from_config()
return
2008-04-19 11:51:31 +02:00
def calc_correct_day(self, month, day):
return str(int(mktime((datetime(2008,int(month),int(day),11,11,00)).timetuple())))
2008-04-16 16:19:32 +02:00
2008-01-11 04:55:55 +01:00
if __name__ == '__main__':
2008-04-16 16:19:32 +02:00
test = DataBase()
test.connect()
#test.get_latest_uid_category("organizer")
datasource = XMLSource()
#test.insert_event()
test.close()
sys.exit(0)
2008-01-03 20:59:36 +01:00
2008-01-06 15:20:10 +01:00
'''
## zur Umwandlungs von unix timestamps in mysql un umgekehrt
from time import *
from datetime import *
# von mysql (unix) timestamp in python datetime umwandeln
datetime.fromtimestamp(1199142000)
# und zurueck
mktime((datetime(2008,01,31,21,8,00)).timetuple())
2008-01-11 04:55:55 +01:00
## Python & MySQL
# http://www.kitebird.com/articles/pydbapi.html
# http://dustman.net/andy/python/python-and-mysql
2008-01-06 15:20:10 +01:00
'''