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 format(self, users = []): if(self.publish_date != ""): self.publish_date_formatted = (self.publish_date - timedelta(hours=3)).strftime("%Y-%m-%d %H:%M") commentAuthor = CollectionHelper.firstOrDefault(users, {"key":"oid", "value": self.author}) if(commentAuthor is not None): self.author_name = commentAuthor.name
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 logUser(): HttpHelper.setJsonContentType() login_dict = HttpHelper.postBodyToDict() call = MongoProvider.UserCall() mongoUsers = call.getByQuery({ "$and": [{ "email": login_dict["email"] }, { "password": login_dict["password"] }] }) mongoUser = CollectionHelper.firstOrDefault(mongoUsers) if (mongoUser is None): return dumps(None) else: d = dict(User.User.getInstance(mongoUser)) HttpHelper.setSessionKey("logged_user", d) return dumps(d)
def signinUser(): HttpHelper.setJsonContentType() signin_dict = HttpHelper.postBodyToDict() call = MongoProvider.UserCall() mongoUsers = call.getByQuery({"email": signin_dict["email"]}) mongoUser = CollectionHelper.firstOrDefault(mongoUsers) if (mongoUser is not None): return dumps( dict( Validation.Validation( False, "A user with the same email already exists!"))) else: user = User.User.getInstance(signin_dict) validation = user.validate() if (validation.success == True): user.mongoSerialization() ret = call.insert(dict(user)) return dumps(dict(validation))
def format(self, users=[]): if (self.publish_date != ""): self.publish_date_formatted = ( self.publish_date - timedelta(hours=3)).strftime("%Y-%m-%d %H:%M") self.tags_joined = ",".join(list(map(lambda x: x.value, self.tags))) self.categories_joined = ",".join( list(map(lambda x: x.value, self.categories))) if (users is not None): articleAuthor = CollectionHelper.firstOrDefault( users, { "key": "oid", "value": self.author }) if (articleAuthor is not None): self.author_name = articleAuthor.name if (self.comments is not None): for comment in self.comments: comment.format(users)
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))