def deleteTake(): response.view = "takes/deleteTake.html" response.title = "Delete 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')) #No need to check if row == None. The above checkIfUserTakePairExists function does that for you ! row = databaseQueries.getTakeInfo(db, takeId) form = FORM( INPUT(_type='submit', _value='Yes', _name='yesDelete'), INPUT(_type='submit', _value="No", _name='noDelete') ) if form.process().accepted: if(request.vars.yesDelete): databaseQueries.deleteTake(db, takeId) redirect(URL('default','index')) elif form.errors: response.flash = 'Errors found in the form.' return dict(form = form, takeTitle = row.takeTitle)
def viewTake(): response.view = 'takes/viewTake.html' takeId = request.vars.takeId if not(utilityFunctions.isTakeIdValid(takeId,db)): redirect(URL('default','index')) userId = (auth.user.id) if (auth.is_logged_in()) else 0 preSelectedTags = databaseQueries.getTakeTags(db, takeId) existingTags = "" for row in preSelectedTags: existingTags = existingTags + "<a href='" + URL('takes','tagFeed',vars=dict(tagId = row.tags.id)) + "'>" + row.tags.tagName + "</a>, " existingTags = existingTags + "end[]" existingTags = existingTags.replace(", end[]",".") row = databaseQueries.getTakeInfo(db, takeId) authorUserId = row.userId numberOfLikes = databaseQueries.getNumberOfLikes(db, takeId, "Take") isTakeLiked = databaseQueries.hasUserLikedArticle(db, takeId, "Take", userId) response.title = row.takeTitle response.subtitle = "Posted on " + str(row.timeOfTake) fields = [Field("commentContent","text")] form = SQLFORM.factory(*fields, labels = {"commentContent":""}, submit_button = "Comment") if form.process().accepted: redirect(URL('takes','postComment',vars=dict(takeId = takeId, commentContent = form.vars.commentContent))) elif form.errors: response.flash = 'Errors found in the form.' commentRows = databaseQueries.getTakeComments(db, takeId) isCommentLiked = [] commentLikeCount = [] for commentRow in commentRows: commentId = commentRow.comments.id isCommentLiked.append(databaseQueries.hasUserLikedArticle(db, commentId, "Comment", userId)) commentLikeCount.append(databaseQueries.getNumberOfLikes(db, commentId, "Comment")) editLink = "" deleteLink = "" if(databaseQueries.checkIfUserTakePairExists(db, userId , takeId)): editLink = URL('takes','editTake',vars=dict(takeId = takeId)) deleteLink = URL('takes','deleteTake',vars=dict(takeId = takeId)) textarea = form.element('textarea') textarea['_cols'] = 1000 textarea['_rows'] = 2 authorNumberOfFollowers = databaseQueries.getNumberOfFollowers(db, authorUserId) authorName = databaseQueries.getUserName(db, authorUserId) profilePicLink = databaseQueries.getUserProfilePicture(db, authorUserId, None) return dict(takeId = takeId, takeContent = row.takeContent, numberOfLikes = numberOfLikes, editLink = editLink, deleteLink = deleteLink, isTakeLiked = isTakeLiked, form = form, comments = commentRows, isCommentLiked = isCommentLiked, existingTags = existingTags, commentLikeCount = commentLikeCount, profilePicLink = profilePicLink, authorUserId = authorUserId, authorNumberOfFollowers = authorNumberOfFollowers, authorName = authorName)
def isTakeIdValid(takeId, db): if not (checkIfVariableIsInt(takeId)): return False row = databaseQueries.getTakeInfo(db, takeId) if row == None: return False return True
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)