Beispiel #1
0
 def post(self):
     data = request.get_json() or {}
     title = data.get("title")
     abstract = data.get("abstract")
     content = data.get("content")
     category = data.get("category")
     author = token_auth.current_user()
     if not title or not abstract or not content:
         return invalid_api_usage("No title, abstract or content provided",
                                  400)
     if Article.query.filter_by(title=title).first():
         return invalid_api_usage("Title is already exist", 400)
     item = Article(title,
                    abstract,
                    content,
                    category=category,
                    author=author.uid)
     item.author = author.uid
     db.session.add(item)
     db.session.commit()
     return {
         "title": item.title,
         "abstract": item.abstract,
         "content": item.content,
         "category": item.category,
         "slug": item.slug,
         "authorName":
         User.query.filter_by(uid=item.author).first().username,
         "created": item.created.isoformat(),
         "updated": item.updated.isoformat(),
     }, 201
Beispiel #2
0
 def post(self):
     data = request.get_json() or {}
     name = data.get("name")
     if not name:
         return invalid_api_usage("No name provided", 400)
     if Category.query.filter_by(name=name).first():
         return invalid_api_usage("Name is already exist", 400)
     category = Category(name)
     db.session.add(category)
     db.session.commit()
     return {
         "name": category.name,
         "slug": category.slug,
     }, 201
Beispiel #3
0
 def post(self):
     data = request.get_json() or {}
     article = data.get("article")
     if not article:
         return invalid_api_usage("No article provided", 400)
     if not Article.query.filter_by(slug=article).first():
         return invalid_api_usage("Article is not exist", 400)
     about = About.query.first()
     if about:
         about.article = article
     else:
         about = About(article)
         db.session.add(about)
     db.session.commit()
     return {"message": "success"}
Beispiel #4
0
 def post(self):
     data = request.get_json() or {}
     username = data.get("username")
     password = data.get("password")
     email = data.get("email")
     if not username or not password or not email:
         return invalid_api_usage("No username, password, email provided",
                                  400)
     if not check_password(password):
         return invalid_api_usage("Invalid password", 400)
     user = User(username, email)
     user.set_password(password)
     db.session.add(user)
     db.session.commit()
     return {"message": "success"}, 201
Beispiel #5
0
 def post(self):
     user = token_auth.current_user()
     item = User.query.filter_by(uid=user.uid).first()
     if "avatar" not in request.files:
         return invalid_api_usage("No avatar provided", 400)
     file = request.files["avatar"]
     if not User.allow_avatar_file(file.filename):
         return invalid_api_usage("Invalid avatar", 400)
     filename = secure_filename(file.filename)
     item.delete_avatar_file()
     item.avatar = item.avatar_upload_to(filename)
     file.save(
         os.path.join(current_app.config["UPLOAD_FOLDER"], item.avatar))
     db.session.commit()
     return {"message": "success"}
Beispiel #6
0
 def get(self, slug):
     item = Article.query.filter_by(slug=slug).first()
     if not item:
         return invalid_api_usage("No such article", 404)
     next = Article.query.filter(Article.created > item.created).order_by(
         Article.created).first()
     previous = Article.query.filter(
         Article.created < item.created).order_by(
             Article.created.desc()).first()
     author = User.query.filter_by(uid=item.author).first()
     return {
         "title": item.title,
         "abstract": item.abstract,
         "content": item.content,
         "slug": item.slug,
         "created": item.created.isoformat(),
         "updated": item.updated.isoformat(),
         "authorName": author.username,
         "avatar": url_for("world.media",
                           path=author.avatar,
                           _external=True),
         "category": item.category,
         "next": next.slug if next else None,
         "previous": previous.slug if previous else None,
     }
Beispiel #7
0
 def delete(self, slug):
     item = Article.query.filter_by(slug=slug).first()
     if not item:
         return invalid_api_usage("No such article", 204)
     db.session.delete(item)
     db.session.commit()
     return {"message": "success"}
Beispiel #8
0
 def post(self):
     data = request.get_json() or {}
     username = data.get("username")
     password = data.get("password")
     if not username or not password:
         return invalid_api_usage("No username or password provided", 400)
     if not verify_password(username, password):
         return invalid_api_usage("Invalid username or password", 400)
     user = User.query.filter(
         or_(User.username == username, User.email == username)).first()
     token = Token.query.filter_by(user=user.uid).first()
     if token:
         return {"token": token.value}
     token = Token(user.uid)
     db.session.add(token)
     db.session.commit()
     return {"token": token.value}
Beispiel #9
0
 def delete(self, slug):
     item = Category.query.filter_by(slug=slug).first()
     if not item:
         return invalid_api_usage("No such category", 204)
     articles = Article.query.filter_by(category=slug).all()
     for article in articles:
         article.category = None
     db.session.delete(item)
     db.session.commit()
     return {"message": "success"}
Beispiel #10
0
 def get(self):
     about = About.query.first()
     if not about:
         return invalid_api_usage("No article is associated with about",
                                  404)
     article = Article.query.filter_by(slug=about.article).first()
     if not article:
         return invalid_api_usage("No such article", 404)
     author = User.query.filter_by(uid=article.author).first()
     return {
         "title": article.title,
         "abstract": article.abstract,
         "content": article.content,
         "slug": article.slug,
         "created": article.created.isoformat(),
         "updated": article.updated.isoformat(),
         "authorName": author.username,
         "avatar": url_for("world.media",
                           path=author.avatar,
                           _external=True),
         "category": article.category,
     }
Beispiel #11
0
    def put(self):
        user = token_auth.current_user()
        item = User.query.filter_by(uid=user.uid).first()
        data = request.get_json() or {}
        username = data.get("username")
        email = data.get("email")
        if not username or not email:
            return invalid_api_usage("No username or email provided", 400)

        attrs = dict(username=username, email=email)
        for name, value in attrs.items():
            setattr(item, name, value)
        db.session.commit()

        return {
            "uid": item.uid,
            "username": item.username,
            "email": item.email,
            "avatar": url_for("world.media", path=item.avatar, _external=True),
            "isAdmin": item.is_admin,
        }
Beispiel #12
0
 def get(self, slug):
     item = Category.query.filter_by(slug=slug).first()
     if not item:
         return invalid_api_usage("No such category", 404)
     return {
         "name":
         item.name,
         "slug":
         item.slug,
         "articles":
         [{
             "title": article.title,
             "abstract": article.abstract,
             "slug": article.slug,
             "authorName":
             User.query.filter_by(uid=article.author).first().username,
             "item": article.category,
             "created": article.created.isoformat(),
             "updated": article.updated.isoformat(),
             "views": article.views,
         }
          for article in Article.query.filter_by(category=item.slug).all()],
     }