def create_these_cals(db, credentials, instructorUserList, thisTerm, domain, calExists, dstLastYear, dstThisYear, dstNextYear, holidayList, instance, setTrace=False): if setTrace: pdb.set_trace() rightNow = datetime.now() now = rightNow.strftime("%Y%m%d-%H_%M_%S") processLog = "blackboardCal%d-%s" % (instance, now) logName= "%s.log" % processLog logfile = open(logName, 'w') logName= "%s.err" % processLog errorfile = open(logName, 'w') logName = "%s.enc" % processLog eventsfile = open(logName, 'w') subClasses = db.cursor(MySQLdb.cursors.DictCursor) # used to grab class information subMeetings = db.cursor(MySQLdb.cursors.DictCursor) # used to grab class meeting information subUpdateTable = db.cursor() # used to update the database with calendar information # setting some variables entry = "" requestor="" defaultSummary = "This is the \"Active\" calendar maintained by the instructor for this course. You were automatically " defaultSummary = defaultSummary + "subscribed to this calendar when you enrolled in this course. Initial scheduling information was " defaultSummary = defaultSummary + "automatically added from the course record in UAonline, and may be changed by the instructor(s) as " defaultSummary = defaultSummary + "necessary. The instructor(s) may choose to delete this calendar- or remove your subscription to it- " defaultSummary = defaultSummary + "at the end of the term it represents (or at any other time.)" # grab all the classes this instructor teaches this term for instructorUsername in instructorUserList: if setTrace: pdb.set_trace() query = """select * from ssr2fcx_courses as class, camp_code as cc where class.campusCode = cc.Code and cc.University='UAF' and instructorUsername = "******" and term=%s""" % (instructorUsername, thisTerm) for i in range(0,5): try: classLines = subClasses.execute(query) except: exctype, error = sys.exc_info()[:2] errorCode = F.error_code_cleanup(error) error = "** MySQL error => type = %r, error = %r, errorCode = %r\n" % (exctype, error, errorCode) errorfile.write(error) logfile.write(error) print error time.sleep(SLEEPY) continue break while True: if setTrace: pdb.set_trace() classRow = subClasses.fetchone() if classRow == None: break uaUsername = classRow['instructorUsername'].rstrip() uaEmail = "%s%s" %(uaUsername, domain) print "uaEmail = ", uaEmail coCrn = classRow['crn'] print "coCrn = ", coCrn if (coCrn not in calExists): # Don't create the calendar if it already exists entry = "calendar coCrn = %s does not yet exist\n" % coCrn if(uaEmail != requestor): # different owner than last iteration entry = "new calendar owner - uaEmail = %s, requestor = %s" % (uaEmail, requestor) print entry entry = "%s\n" % entry logfile.write(entry) requestor = uaEmail credentials.requestor = requestor # create / build connection ####################################################### # Build a service object for interacting with the API # ####################################################### credentials.requestor = requestor (entry, service) = F.return_service(credentials.requestor, credentials) if (entry == ""): # success entry = 'service successfully created' print entry logfile.write('%r\n' % (entry)) else: print entry logfile.write(entry) errorfile.write(entry) continue # no reason to keep going if connection hasn't been made ######################### # connection successful # ######################### title = "%s %s %s - %s" % (classRow['subjectDesc'], classRow['courseNum'], classRow['sequenceNum'], thisTerm) summary = "CRN %s: %s" % (classRow['crn'], defaultSummary) (entry, calId) = F.create_cal(thisTerm, coCrn, title, summary, service) if ("LIMIT" in entry): error = entry + "Quiting program Daily Limit Reached\n" print error errorfile.write(error) logfile.write(error) print "instance #%s returning" % instance eventsfile.close() errorfile.close() logfile.close() subClasses.close subMeetings.close subUpdateTable.close return elif (calId == "FAILED"): # calendar creation failed error = "ERROR: %s - Calendar creation Failed, entry = %s\n" % (coCrn, entry) print error errorfile.write(error) continue # no reason to proceed if no calendar was created to add events to else: print " ", entry logfile.write(entry) calExists[coCrn] = "exists" # create events query = """select * from ssr2fcx_meeting where term = %s AND crn = %s""" % (thisTerm, coCrn) for i in range(0,5): try: meet = subMeetings.execute(query) except: exctype, error = sys.exc_info()[:2] errorCode = F.error_code_cleanup(error) error = "** MySQL error => type = %r, error = %r, errorCode = %r\n" % (exctype, error, errorCode) errorfile.write(error) logfile.write(error) print error time.sleep(SLEEPY) continue break # loop through class meeting information query while True: meetRow = subMeetings.fetchone() if meetRow == None: break meetInfo = "%s %s %s, %s, %s" % (classRow['subjectDesc'], classRow['courseNum'], classRow['sequenceNum'], thisTerm, coCrn) (entry, eventID) = F.create_event(meetRow, meetInfo, service, calId, dstLastYear, dstThisYear, dstNextYear) if ("LIMIT" in entry): error = entry + "Quiting program Daily Limit Reached\n" print error errorfile.write(error) logfile.write(entry) eventsfile.close() errorfile.close() logfile.close() subClasses.close subMeetings.close subUpdateTable.close return elif ("ERROR" in entry): # event creation failed print entry entry = "%s\n" % entry logfile.write(entry) tempVar = entry.split('= ') eventsfile.write(tempVar[1]) error = "tempVar[1] = %s\n" % tempVar[1] print error errorfile.write(error) else: print entry logfile.write('%r\n' % (entry)) # cancel events that occur during holidays (like spring break) if (eventID > ""): entry = F.cancel_holiday_classes(holidayList, calId, eventID, service) if ('SUCCESS' in entry): # success entry = "%s\n" % entry print entry logfile.write(entry) else: # failure entry = " FAILED to remove classes on holidays. %s\n" % entry print entry logfile.write(entry) errorfile.write(error) # insert calendar information into database uQuery = """INSERT into classCalendars SET CRN = '%s', term = '%s', calId = '%s', ownerUsername = '******'""" % \ (coCrn, thisTerm, MySQLdb.escape_string(calId), uaUsername) for i in range(0,5): try: subUpdateTable.execute(uQuery) except: exctype, error = sys.exc_info()[:2] errorCode = F.error_code_cleanup(error) error = "** MySQL error => type = %r, error = %r, errorCode = %r\n" % (exctype, error, errorCode) errorfile.write(error) logfile.write(error) print error time.sleep(SLEEPY) continue break db.commit() else: entry = "calendar = %s already exists" % coCrn print entry logfile.write(entry) subClasses.close subMeetings.close subUpdateTable.close print "instance #%s done!" % instance eventsfile.close() errorfile.close() logfile.close() return
sched = db.cursor(MySQLdb.cursors.DictCursor) # used to grab a list of classes scheduled this term ######################### # # # Splitting up the work # # # ######################### query = """select count(distinct(instructorUsername)) as totalCount from ssr2fcx_courses as class, camp_code as cc where term = %s and class.campusCode = cc.Code and cc.University='UAF'""" % (thisTerm) for i in range(0,5): try: classesLines = classes.execute(query) except: exctype, error = sys.exc_info()[:2] errorCode = F.error_code_cleanup(error) error = "** MySQL error => type = %r, error = %r, errorCode = %r\n" % (exctype, error, errorCode) errfile.write(error) loggfile.write(error) print error time.sleep(SLEEPY) continue break instructorCount = classes.fetchall() instructorCountHalf = instructorCount[0]['totalCount'] / 2; instructorCountLeft = instructorCount[0]['totalCount'] - instructorCountHalf firstHalf=[] query = """select distinct(instructorUsername) as name from ssr2fcx_courses as class, camp_code as cc where term = %s and instructorUsername >''