示例#1
0
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)
示例#2
0
def changeLikeStatus():
    userId = auth.user.id
    articleType = request.vars.articleType
    articleId = request.vars.articleId

    if (not(utilityFunctions.checkIfVariableIsInt(articleId))):
        return "Invalid"

    if not(databaseQueries.checkIfArticleExists(db, articleId, articleType)):
        return "Invalid"

    if(databaseQueries.hasUserLikedArticle(db, articleId, articleType, userId)) :
        databaseQueries.unlike(db, articleId, articleType, userId)
    else:
        databaseQueries.like(db, articleId, articleType)

    return databaseQueries.getNumberOfLikes(db, articleId, articleType)