def addItems(): form = cgi.FieldStorage(); user = users.get_current_user(); sName = form['addon'].value; sName1 = sName.split(" by "); sName2 = sName1[0]; surveyName = sName2.replace("__"," "); rawOptions = form['additems'].value; rawOptions += ','; optionList = rawOptions.split(','); counter = 0; for option in optionList: itemPresent = db.GqlQuery("SELECT * FROM Item WHERE name = :1 AND survey = :2 and owner = :3",option,surveyName,user); counter1 = itemPresent.count(); if (counter1 > 0): counter = counter + 1; if (counter > 0): return render_template('failure.html',message="One or more items that you tried to add are already present in this category.None of the items were added."); else: surveys = db.GqlQuery("SELECT * FROM Survey WHERE name = :1 and owner = :2",surveyName,user); oldItemList = []; oldExpiration =""; for sur in surveys: if (sur.name == surveyName): oldItemList = sur.values; oldExpiration = sur.expiration; for newItems in optionList: oldItemList.append(newItems); newItemList = oldItemList; db.delete(surveys); newSurvey = Survey(name=surveyName,owner=user,values=newItemList,expiration=oldExpiration); newSurvey.put(); for option in optionList: if option: item = Item(name=option); item.survey = surveyName; item.owner = user; search1 = Search(name=option,entityType="item",survey=surveyName,owner=user); item.put(); search1.put(); return render_template('survey_confirmation.html',survey=surveyName,option=optionList);
def renameCategory(): form = cgi.FieldStorage(); user = users.get_current_user(); newSurveyName = form['renameText'].value.strip(); oldSurveyName = form['renamesurvey'].value; newNameCheck = db.GqlQuery("SELECT * FROM Survey WHERE owner = :1 AND name = :2",user,newSurveyName); counter = newNameCheck.count(); if (counter > 0): return render_template('failure.html',message="You already have a survey with given name. Please try with some other name"); else: oldSurvey = db.GqlQuery("SELECT * FROM Survey WHERE owner = :1 AND name = :2",user,oldSurveyName); for old in oldSurvey: newSurvey = Survey(name=newSurveyName,values=old.values,owner=old.owner,expiration=old.expiration); newSurvey.put(); oldItems = db.GqlQuery("SELECT * FROM Item WHERE survey = :1 and owner = :2",oldSurveyName,user); for old in oldItems: newItem = Item(name=old.name,image=old.image,survey=newSurveyName,owner=old.owner); newItem.put(); oldVotes = db.GqlQuery("SELECT * FROM Vote WHERE survey = :1 and owner = :2",oldSurveyName,user); for old in oldVotes: newVote = Vote(name=old.name,owner=old.owner,win=old.win,survey=newSurveyName); newVote.put(); oldComments = db.GqlQuery("SELECT * FROM Comments WHERE itemOwner = :1 AND survey = :2",user,oldSurveyName); for old in oldComments: newComment = Comments(itemName=old.itemName,itemOwner=old.itemOwner,comment=old.comment,survey=newSurveyName); newComment.put(); oldCommentValidator = db.GqlQuery("SELECT * FROM CommentValidator where itemOwner = :1 and survey = :2",user,oldSurveyName); for old in oldCommentValidator: newCommentValidator = CommentValidator(itemName=old.itemName,itemOwner=old.itemOwner,commenter=old.commenter,survey=newSurveyName); newCommentValidator.put(); oldSearch = db.GqlQuery("SELECT * FROM Search WHERE survey = :1 AND owner = :2",oldSurveyName,user); for old in oldSearch: if(old.entityType == "survey"): newSearch = Search(name=newSurveyName,entityType=old.entityType,survey=newSurveyName,owner=old.owner); newSearch.put(); else: newSearch = Search(name=old.name,entityType=old.entityType,survey=newSurveyName,owner=old.owner); newSearch.put(); deleteGeneric(surveyName=oldSurveyName); return render_template('failure.html',message="Update Successful!");
def addSurvey(): form = cgi.FieldStorage(); user = users.get_current_user(); surveyName = form['sname'].value.strip(); rawOptions = form['options'].value; rawOptions += ','; optionList = rawOptions.split(','); surveyExpireDate = ""; if (form.has_key("dateValue")): expireDate = form['dateValue'].value; expireHour = int(form['hours'].value); expireMinutes = int(form['minutes'].value); expireSeconds = int(form['seconds'].value); rawDate = expireDate.split("/"); month = int(rawDate[0]); day = int(rawDate[1]); year = int(rawDate[2]); surveyExpireDate = datetime.datetime(year,month,day,expireHour,expireMinutes,expireSeconds); else: surveyExpireDate = datetime.datetime(2012,12,30,0,0,0); surveys = db.GqlQuery("SELECT * FROM Survey WHERE name = :1 and owner = :2",surveyName,user); counter = surveys.count(); if (counter == 0): survey = Survey(name=surveyName,expiration=surveyExpireDate); survey.values = optionList; survey.owner = user; survey.put(); search = Search(name=surveyName,entityType="survey",survey=surveyName,owner=user); search.put(); for option in optionList: if option: item = Item(name=option); item.survey = surveyName; item.owner = user; item.put(); search1 = Search(name=option,entityType="item",survey=surveyName,owner=user); search1.put(); return render_template('survey_confirmation.html',survey=surveyName,option=optionList); else: return render_template('failure.html',message="You already have created this survey! Please create a different survey.");
def uploadFile(): try: form = cgi.FieldStorage(); item = form["filename"]; data = ""; if item.file: data = item.file; doc = xml.dom.minidom.parse(data) items = doc.getElementsByTagName("NAME"); owner = users.get_current_user(); optionList = []; for node in items: optionList.append(node.firstChild.nodeValue); surveyName = optionList[0].strip(); optionList.remove(surveyName); surveyCheck = db.GqlQuery("SELECT * FROM Survey WHERE name = :1 and owner = :2",surveyName,owner); surveyCount = surveyCheck.count(); if (surveyCount > 0): ##Get the old item list from Survey. oldList = []; oldExpiration = ""; for sur in surveyCheck: oldList = oldList+sur.values; oldExpiration = sur.expiration; addList = []; ## Items that are present in both list; removeList = [] ## Items to be deleted. listCounter = 0; tempList = optionList; for old in oldList: if(old in optionList): addList.append(old); optionList.remove(old); listCounter = listCounter + 1; else: removeList.append(old); if (len(addList) == len(tempList)) and (listCounter == len(tempList))and (len(removeList) == 0): removeList = tempList; for remove in removeList: oldItem = db.GqlQuery("SELECT * FROM Item WHERE name = :1 and owner = :2 and survey = :3",remove,owner,surveyName); db.delete(oldItem); oldSearch = db.GqlQuery("SELECT * FROM Search WHERE name = :1 and owner = :2 and survey = :3 and entityType = :4",remove,owner,surveyName,"item"); db.delete(oldSearch); oldComment = db.GqlQuery("SELECT * FROM Comments WHERE itemName = :1 AND itemOwner = :2 AND survey = :3",remove,owner,surveyName); db.delete(oldComment); oldCommentValidator = db.GqlQuery("SELECT * FROM CommentValidator WHERE itemName = :1 AND itemOwner = :2 AND survey = :3",remove,owner,surveyName); db.delete(oldCommentValidator); oldVote = db.GqlQuery("SELECT * FROM Vote WHERE name = :1 AND owner = :2 AND survey = :3",remove,owner,surveyName); db.delete(oldVote); for item in optionList: newItem = Item(name=item,survey=surveyName,owner=owner); search1 = Search(name=item,owner=owner,entityType="item",survey=surveyName); newItem.put(); search1.put(); for add in addList: optionList.append(add); db.delete(surveyCheck); newSurvey = Survey(name=surveyName,owner=owner,values=optionList,expiration=oldExpiration); newSurvey.put(); else: surveyExpireDate = datetime.datetime(2012,12,30,00,00,00); newSurvey = Survey(name=surveyName,owner=owner,values=optionList,expiration=surveyExpireDate); newSurvey.put(); search = Search(name=surveyName,owner=owner,entityType="Survey",survey=surveyName); search.put(); for item in optionList: newItem = Item(name=item,survey=surveyName,owner=owner); search1 = Search(name=item,owner=owner,entityType="item",survey=surveyName); newItem.put(); search1.put(); return render_template('survey_confirmation.html',survey=surveyName,option=optionList); except: return render_template('failure.html',message="The file you uploaded was incorrect!");