def write_file(self,fileName,name): if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')): db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='DATABASE', user='******') else: db = MySQLdb.connect(host='HOSTIP', port=3306, db='DATABASE', user='******',passwd='PASSWORD') cursor = db.cursor() threadname= cgi.escape(self.request.get('threadNm'))#Get threadname from html username=self.request.get('usernameHd')#Get username from html img_img = image = self.request.get("image") #self.response.write('Creating file %s\n' % filename) write_retry_params = gcs.RetryParams(backoff_factor=1.1) gcs_file = gcs.open(fileName, 'w', content_type='image/jpg', retry_params=write_retry_params, options={'x-goog-acl': 'public-read'} ) gcs_file.write(img_img) gcs_file.close() #url of where image going to upload url='Path of googleCloud Bucket'+name try:#Inseting url,username and thread name to table cursor.execute('INSERT INTO thread (username,imageref,thread_name) VALUES (%s,%s,%s)',(username,url,threadname)) except: self.response.write('Thread Already Exists') db.commit() cursor.execute('SELECT thread_name FROM thread') threadlist=[] for row1 in cursor.fetchall(): threadlist.append(row1[0]) db.close() self.tmp_filenames_to_clean_up.append(fileName) #passing threadlist,username and threadname variables={'threadlist' :threadlist,'usernameSaved':username,'threadname':threadname} template = JINJA_ENVIRONMENT.get_template('home.html') self.response.write(template.render(variables))
def write_file(self,fileName,name): if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')): db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='DATABASE', user='******') else: db = MySQLdb.connect(host='HOSTIP', port=3306, db='DATABASE', user='******',passwd='PASSWORD') cursor = db.cursor() threadname= cgi.escape(self.request.get('threadNm'))#Get threadname from html username=self.request.get('usernameHd')#Get username from html img_img = image = self.request.get("image") #self.response.write('Creating file %s\n' % filename) write_retry_params = gcs.RetryParams(backoff_factor=1.1) gcs_file = gcs.open(fileName, 'w', content_type='image/jpg', retry_params=write_retry_params, options={'x-goog-acl': 'public-read'} ) gcs_file.write(img_img) gcs_file.close() #url of where image going to upload url='https://console.developers.google.com/m/cloudstorage/b/cloudbucket12/o/'+name try:#Inseting url,username and thread name to table cursor.execute('INSERT INTO thread (username,imageref,thread_name) VALUES (%s,%s,%s)',(username,url,threadname)) except: self.response.write('Thread Already Exists') db.commit() cursor.execute('SELECT thread_name FROM thread') threadlist=[] for row1 in cursor.fetchall(): threadlist.append(row1[0]) db.close() self.tmp_filenames_to_clean_up.append(fileName) #passing threadlist,username and threadname variables={'threadlist' :threadlist,'usernameSaved':username,'threadname':threadname} template = JINJA_ENVIRONMENT.get_template('home.html') self.response.write(template.render(variables))
def RealMain(argv, data=None): os.environ['LC_ALL'] = 'C' options, args = parser.parse_args(argv[1:]) srv = GetRpcServer(options) backup = Proxy(BackupService_Stub(srv)) db = PgSQL.connect(database=options.dbname, client_encoding="utf-8", unicode_results=1) db.cursor().execute("set client_encoding to unicode") store = LocalStore(db) print 'BEGIN BACKUP' for kind_name in KINDS: sys.stdout.write('\n') cnt = 0 last_key = '' while True: sys.stdout.write('\r%-18s ... ' % kind_name) r = NextChunkRequest() r.kind = kind_name r.last_key = last_key r = backup.NextChunk(r) if not r.entity: break for entity in r.entity: cnt += 1 sys.stdout.write('\r%-18s ... %5d ' % (kind_name, cnt)) o = pickle.load(cStringIO.StringIO(entity.data)) getattr(store, 'save_%s' % kind_name)(entity, o) last_key = entity.key db.commit() sys.stdout.write('\n') print 'BACKUP DONE' db.commit() db.close()
def post(self): threadname = self.request.get('threadnametorm') loggedUserName=self.request.get('usernameLoggedIn') if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')): db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='DATABASE', user='******') else: db = MySQLdb.connect(host='HOSTIP', port=3306, db='DATABASE', user='******',passwd='PASSWORD') cursor = db.cursor() cursor.execute("DELETE FROM thread WHERE thread_name = '%s';"% threadname.strip()) db.commit() cursor.execute('SELECT thread_name FROM thread') threadlist=[] for row1 in cursor.fetchall(): threadlist.append(row1[0]) db.close() variables={'threadlist' :threadlist,'usernameSaved':loggedUserName,'threadname':threadname} template = JINJA_ENVIRONMENT.get_template('home.html') self.response.write(template.render(variables))
def signUp(self,username,password,cnpassword): try: if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')): db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='DATABASE', user='******') else: db = MySQLdb.connect(host='HOSTIP', port=3306, db='DATABASE', user='******',passwd='PASSWORD') cursor = db.cursor() start_timeForInsert = time.time()#Start time of insert query #Inserting new user details inside table cursor.execute('INSERT INTO user (username,password) VALUES (%s,%s)',(username,password)) self.response.write('<br />') start_time = time.time() template = JINJA_ENVIRONMENT.get_template('sign_up.html') self.response.write(template.render()) db.commit() db.close() except DeadlineExceededError, error_message: logging.exception('Failed, exception happened - %s' % error_message) self.response.write('<br />')
if comments is not None: comments=comments+'ed=ed'+loggedUserName+'cmnet'+commentsNew comments_split=comments.split("ed=ed")#Comments are added in format as username+cmnet+comment+ed=ed+username2+cmnet+comment+ed=ed for items in comments_split: list.append(items.split('cmnet')) else: comments=loggedUserName+'cmnet'+commentsNew list.append(comments.split('cmnet')) try:#Updating table to insert Comment into database cursor.execute ('UPDATE thread SET comments=%s WHERE thread_name=%s',(comments,threadname)) except: self.response.write('Error in update') db.commit() db.close() variables = {'imageLink': imageref,'userName': userName,'commentsList':list,'threadname':threadname,'usernameSaved':loggedUserName} template = JINJA_ENVIRONMENT.get_template('thread.html') self.response.write(template.render(variables)) #Remove photo from database class RemovePhoto(webapp2.RequestHandler): def post(self): threadname = self.request.get('threadnametorm') loggedUserName=self.request.get('usernameLoggedIn') if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')): db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='DATABASE', user='******') else: db = MySQLdb.connect(host='HOSTIP', port=3306, db='DATABASE', user='******',passwd='PASSWORD')
def post(self): try: upload_files = self.get_uploads('xlfile') TABLE_NAME = self.request.get('table_name') self.response.out.write('Table Name received. Name=%s' % (TABLE_NAME)) self.response.out.write('File upload received. File count=%d' % len(upload_files)) if len(upload_files) > 0: blob_info = upload_files[0] self.response.out.write('Blob stored key=%s' % (blob_info.key())) user_file = UserFile(blob_key=blob_info.key()) user_file.put() blob_reader = blobstore.BlobReader(blob_info.key()) blob_iterator = BlobIterator(blob_reader) reader = csv.reader(blob_iterator, skipinitialspace=True, delimiter=',') #!!!!!AYHUN !!!!! # IMPLEMENT PARSER HERE #object to use: csv iterator called "reader" # Create/Stick into database count = 1 big_dict = {} num_cols = 0 titles = [] for row in reader: #self.response.out.write(count) #self.response.out.write(row) if count == 1: self.response.out.write("Metadata:<br>") num_cols = 0 for elts in row: catg_list = [elts] big_dict[num_cols] = catg_list num_cols += 1 self.response.out.write(elts + ", ") self.response.out.write("<br>Content:") else: iter_num = 0 for elts in row: big_dict[iter_num].append(elts) iter_num += 1 self.response.out.write(elts + ", ") self.response.out.write("<br>") count += 1 self.response.out.write("Final dict:" + str(big_dict)) ###############SQL CODE -----> use big_dict (dictionary object) to generate SQL code #big_dict current format: dictionary object where key is 0-number of cols in csv file #value is list of values in each row, where the first element of the list is the name of column #use this value to create values for the attributes in database and remainder of list to fill the #the data corresponding to the attribute self.response.out.write('Starting database part.............<br>') self.response.out.write(os.getenv('SERVER_SOFTWARE') + "<br>") if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')): self.response.out.write("I should be running on the cloud right now 1<br>") #db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='gsdmarin', user='******', passwd='1234') db = MySQLdb.connect(host='127.0.0.1', port=3306, db='gsdmarin', user='******', passwd='1234') self.response.out.write("I should be running on the cloud right now 2<br>") else: self.response.out.write("I should be running on a normal computer right now 1<br>") #Cant connect to localhost + if we fall in here we need error handling! Unacceptable. db = MySQLdb.connect(host='173.194.82.159', port=3306, db='gsdmarin', user='******', passwd='1234') self.response.out.write("I should be running on a normal computer right now 2<br>") # Alternatively, connect to a Google Cloud SQL instance using: # db = MySQLdb.connect(host='ip-address-of-google-cloud-sql-instance', port=3306, user='******', charset='utf 8') cursor = db.cursor() # !!!! Code that decides the name of the table goes here #TABLE_NAME = "EMPLOYEE" ------> THIS HAS BEEN DONE IN LINE 87 e.g TABLE_NAME = self.request.get('table_name') # ----------------------------------------------------------------------------------------------------- ####### AYHUN!!!!!!!! # print file name and content to console, just for seeing it self.response.out.write('File name:<br>') self.response.out.write(TABLE_NAME+"<br>") self.response.out.write('File content:<br>') self.response.out.write(big_dict) self.response.out.write('<br>') # query the 'information_schema' to see if a table that has the 'TABLE_NAME' as its name exists table_exists = False sql = "SELECT * FROM information_schema.tables WHERE table_name = '%s'" % TABLE_NAME self.response.out.write("query:" + sql + "<br>") self.response.out.write("result:<br>") cursor.execute(sql) for row in cursor.fetchall(): table_exists = True self.response.out.write( ' '.join(str(e) for e in row) + '<br>') if table_exists: self.response.out.write("Table %s already exists." % TABLE_NAME) self.response.out.write("<br>**********Database before insertion:") sql = "SELECT * FROM gsdmarin.%s" % TABLE_NAME cursor.execute(sql) for row in cursor.fetchall(): self.response.out.write('<br>' + ' '.join(str(e) for e in row) ) self.response.out.write('<br>************************************') # TODO: insert to existing table here sql = [] sql.append("INSERT INTO `gsdmarin`.`%s`" % TABLE_NAME) sql.append("(`%s`," % big_dict[0][0]) for i in range(1,num_cols-1): sql.append("`%s`," % big_dict[i][0]) sql.append("`%s`)" % big_dict[num_cols - 1][0]) sql.append("VALUES") for r in range(1,len(big_dict[0])): if r == 1: sql.append("('"+big_dict[0][1]+"',") else: sql.append(",('"+big_dict[0][1]+"',") for c in range(1,num_cols-1): sql.append("'"+big_dict[c][r]+"',") sql.append("'"+big_dict[num_cols-1][r]+"')") sql.append(';') self.response.out.write("<br>insertion query:<br>" + ' '.join(sql)) try: cursor.execute(' '.join(sql)) db.commit() except Exception, e: db.rollback self.response.out.write("<br>**********Database after insertion:") sql = "SELECT * FROM gsdmarin.%s" % TABLE_NAME cursor.execute(sql) for row in cursor.fetchall(): self.response.out.write("<br>" + ' '.join(str(e) for e in row) ) self.response.out.write('<br>************************************') else: