def submitTake(): response.view = "takes/submitTake.html" response.title = "Submit Take" topicsList = databaseQueries.getGlobalTopicsList(db) numOfTopics = len(topicsList) fields = [] options = [] for i in range(1, numOfTopics): topicMapping = topicsList[i] options += [topicMapping["topicName"]] fields += [Field('topics','string')] fields += [Field('takeTags','string')] fields += [field for field in db.takes] form = SQLFORM.factory(*fields, _id = "tagform-full") if form.process().accepted: selectedTopics = form.vars.topics selectedTags = form.vars.takeTags takeId = databaseQueries.addTake(db, form.vars.takeTitle, form.vars.takeContent) for i in range(1, numOfTopics): topicMapping = topicsList[i] if(topicMapping["topicName"] in selectedTopics): databaseQueries.addTakeTopicMapping(db, takeId, i) for tag in selectedTags.split(","): tagId = databaseQueries.getTagId(db, tag) if(tagId == 0): tagId = databaseQueries.insertTag(db, tag) databaseQueries.addTakeTagMapping(db, takeId, tagId) redirect(URL('takes','viewTake',vars=dict(takeId = takeId))) elif form.errors: response.flash = 'Errors found in the form.' else: response.flash = 'Please fill the form.' return dict(form=form, options = options, preSelectedTopics = [], existingTags = "")
def editTake(): response.view = "takes/submitTake.html" response.title = "Edit Take" userId = auth.user.id takeId = request.vars.takeId if not(utilityFunctions.isTakeIdValid(takeId,db)): redirect(URL('default','index')) if(not(databaseQueries.checkIfUserTakePairExists(db, userId, takeId))): redirect(URL('default','index')) takeTopicMappings = databaseQueries.getTopicMappings(db, takeId) preSelectedTopics = [] topicsList = databaseQueries.getGlobalTopicsList(db) numOfTopics = len(topicsList) preSelectedTags = databaseQueries.getTakeTags(db, takeId) existingTags = "" for row in preSelectedTags: existingTags = existingTags + row.tags.tagName + ", " existingTags = existingTags + "end[]" existingTags = existingTags.replace(", end[]","") fields = [] options = [] for i in range(1, numOfTopics): topicMapping = topicsList[i] options += [topicMapping["topicName"]] if(i in takeTopicMappings): preSelectedTopics += [topicMapping["topicName"]] fields += [Field('topics','string')] fields += [Field('takeTags','string')] fields += [field for field in db.takes] form = SQLFORM.factory(*fields) takeInfo = databaseQueries.getTakeInfo(db, takeId) form.vars.takeTitle = takeInfo.takeTitle form.vars.takeContent = takeInfo.takeContent form.vars.topics = ['Entertainment', 'Technology', 'Friendship'] if form.process().accepted: databaseQueries.updateTake(db, form.vars.takeTitle, form.vars.takeContent, takeId) selectedTopics = form.vars.topics selectedTags = form.vars.takeTags for i in range(1, numOfTopics): topicMapping = topicsList[i] if(topicMapping["topicName"] in selectedTopics): databaseQueries.addTakeTopicMapping(db, takeId, i) else: databaseQueries.removeTakeTopicMapping(db, takeId, i) databaseQueries.removeTakeTags(db, takeId) for tag in selectedTags.split(","): tagId = databaseQueries.getTagId(db, tag) if(tagId == 0): tagId = databaseQueries.insertTag(db, tag) databaseQueries.addTakeTagMapping(db, takeId, tagId) redirect(URL('takes','viewTake',vars=dict(takeId = request.vars.takeId))) elif form.errors: response.flash = 'Errors found in the form.' else: response.flash = 'Please fill the form.' return dict(form=form, options = options, preSelectedTopics = preSelectedTopics, existingTags = existingTags)