from django.shortcuts import render from django.shortcuts import redirect from django.http import HttpResponseRedirect, HttpResponse from django.contrib.auth.models import User from django import forms from django.conf import settings import simplejson import datetime import json from .datagrid import querySuffix, getStartRows, getFilteringParameters from .dbtable_alumni import DBtable_alumni # This is the absolute path to the download folder, usually at "project_root/theme/SmartAdmin/static/media/download/" # To be figured out: ideally, we should use 'project_root/static/media/download", which does not rely on the theme. DOWNLOAD_DIRECTORY = settings.MEDIA_ROOT + "/download/" # This is the relative path to the download folder, usually at "/theme/SmartAdmin/static/media/download/" without project_root. # To be figured out: ideally, we should use '/static/media/download", which does not rely on the theme. DOWNLOAD_DIRECTORY_LINK = settings.MEDIA_URL + '/download/' # this is a symbolic link to DOWNLOAD_DIRECTORY # We always use report to return variables report = {} class DBtable_alumni(object): ''' The class stores all the information about the table [KI_Directory].[dbo].[KI_Directory] Typical usage of the class dbtable = DBtable_alumni() row = dbtable.retrieveRecord(mitid, krebid, firstname, lastname) dbtable.close() ''' def __init__(self, whichDB): #print "Dbtable_cell: __init__" self.db = DBconnection(whichDB) #print "Dbtable_cell: __init__: db okay" self.tablemodel = "data_alumni_alumni" if whichDB=="DJANGO": self.tablemodel = Alumni else: self.tablemodel = Alumni self.tablename = "data_alumni_alumni" def __formatRecord(self, record): ''' Given the record from either one row in an excel file or from client-side table, reformat it accordingly so it can be stored into the database table. Input record, the dictionary in its original format. Output record, the dictionary reformated ready for storing into database table. Notes: Used in save(request). ''' record_new = {} for key, value in record.items(): key_new = str(key) if key_new=='employment_cancer_related': if value is None: value = -1 elif value=="Yes": value = 1 elif value=="No": value = 0 else: value = -1 #print "cancer_related", value elif key_new=='position': if value is None: value = 'Other' elif len(value)==0: value = 'Other' elif value=="": value = 'Other' #print "position", value elif key_new=='type': if value is None: value = 'Other' elif len(value)==0: value = 'Other' elif value=="": value = 'Other' #print "type", value record_new[key_new] = value #print "Save record: ", record[u'cancer_related'] #print "Save record: ", record[u'position'] record_new['date_uploaded'] = time.strftime("%Y-%m-%d") return record_new def storeOneRecord(self, record): ''' Given the record from either one row in an excel file or from client-side table, reformat it accordingly so it can be stored into the database table. Input record, the record in its original format. Output record, the record reformated ready for storing into database table. Notes: Used in save(request). ''' record_new = self.__formatRecord(record) self.db.storeOneRecord(self.tablemodel, record_new) def __sqlQuery_select_alumni_Excel_byIDs(self, orderby, allids, downloadallterms=False): ''' Retrieve all records. Input Output A SQL query used for retrieving records SELECT * FROM djangocms.alumni_alumni A order by A.last_name_ki; ''' sqlquery = " SELECT * " sqlquery += "FROM alumni_alumni A " if downloadallterms: # download all records sqlquery += " " else: # download only selected ids n = len(allids) # download only those records on the list sqlquery += " WHERE A.id in (" ni = 0 for id in allids: sqlquery += str(id) ni += 1 if ni