def deleteArticle(object_id): HttpHelper.setJsonContentType() call = MongoProvider.ArticleCall() ret = call.delete(object_id) return dumps(True)
def createArticle(): HttpHelper.setJsonContentType() user = HttpHelper.getSessionKey("logged_user") if (user is None): return dumps( dict(Validation.Validation(False, "You are not logged in"))) article_dict = HttpHelper.postBodyToDict() article = Article.Article.getInstance(article_dict) call = MongoProvider.ArticleCall() mongoArticles = call.getByQuery({"slug": article.slug}) mongoArticle = CollectionHelper.firstOrDefault(mongoArticles) if (mongoArticle is not None): return dumps( dict( Validation.Validation( False, "There is an article with same slug already"))) validation = article.validate() if (validation.success == True): article.mongoSerialization() article.setPublishTimeToNow() article.setAuthor(user["oid"]) article.text = formatTextHtml(article.text) ret = call.insert(dict(article)) validation.id = str(ret.inserted_id) return dumps(dict(validation))
def getArticleBySlug(slug): HttpHelper.setJsonContentType() users = getAllUsers() call = MongoProvider.ArticleCall() mongoArticles = call.getByQuery({"slug": slug}) mongoArticle = CollectionHelper.firstOrDefault(mongoArticles) if (mongoArticle is None): return dumps(None) else: article = Article.Article.getInstance(mongoArticle, True, users) return dumps(dict(article))
def createComment(object_id): HttpHelper.setJsonContentType() user = HttpHelper.getSessionKey("logged_user") if (user is None): return dumps( dict(Validation.Validation(False, "You are not logged in"))) comment_dict = HttpHelper.postBodyToDict() comment = Comment.Comment.getInstance(comment_dict) validation = comment.validate() if (validation.success == True): comment.setPublishTimeToNow() comment.setAuthor(user["oid"]) comment.mongoSerialization(True) call = MongoProvider.ArticleCall() mongoArticles = call.getByID(object_id) mongoArticle = CollectionHelper.firstOrDefault(mongoArticles) if (mongoArticle is not None): article = Article.Article.getInstance(mongoArticle, True) article_comments = article.comments if (article_comments is None): article_comments = [] article_comments.append(comment) article_comments_dicts = list( map(lambda x: dict(x), article_comments)) call = MongoProvider.ArticleCall() ret = call.updateEntireDocument( object_id, {"comments": article_comments_dicts}) return dumps(dict(validation))
def updateArticle(object_id): HttpHelper.setJsonContentType() article_dict = HttpHelper.postBodyToDict() article = Article.Article.getInstance(article_dict) validation = article.validate() if (validation.success == True): article.mongoSerialization(True) article_edit_dict = dict(article) call = MongoProvider.ArticleCall() article_edit_dict["text"] = formatTextHtml(article_edit_dict["text"]) ret = call.updateEntireDocument(object_id, article_edit_dict) return dumps(dict(validation))
def getArticles(): HttpHelper.setJsonContentType() tag = request.query['tags'] category = request.query['categories'] hasTags = tag.strip() != "" hasCategories = category.strip() != "" users = getAllUsers() call = MongoProvider.ArticleCall() mongoArticles = [] if hasTags == False and hasCategories == False: mongoArticles = list(call.get()) else: tagQuery = "true" categoryQuery = "true" if hasTags == True: tagQuery = 'this.tags.map(function(t) { return t.value; }).indexOf("' + tag + '") != -1' if hasCategories == True: categoryQuery = 'this.categories.map(function(t) { return t.value; }).indexOf("' + category + '") != -1' mongoArticles = call.getByQuery( {"$where": tagQuery + ' && ' + categoryQuery}) articles = list( map(lambda x: Article.Article.getInstance(x, True, users), mongoArticles)) articles_sorted = sorted(articles, key=attrgetter('publish_date'), reverse=True) dicts = map(lambda x: dict(x), articles_sorted) return dumps(dicts)