Skip to content
This repository has been archived by the owner on Jun 13, 2020. It is now read-only.

Commit

Permalink
Add slug to Game model
Browse files Browse the repository at this point in the history
  • Loading branch information
isg89 committed Apr 25, 2020
1 parent 1685ecb commit aa4fee8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 36 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ python-dotenv = "*"
flask-migrate = "*"
gunicorn = "*"
requests = "*"
python-slugify = "*"

[requires]
python_version = "3.8"
110 changes: 80 additions & 30 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions api/v1/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask_restx import Namespace, Resource, Api, fields
from extensions import guard, db, upload_img, delete_img
from marshmallow import ValidationError, INCLUDE
from utilities import get_auto_increment, base64_to_pillow_img, pillow_img_to_bytes
from utilities import get_auto_increment, base64_to_pillow_img, pillow_img_to_bytes, slugify_text
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import or_, and_

Expand Down Expand Up @@ -99,7 +99,8 @@ def post(self):
# Set Authenticated User
current_user = flask_praetorian.current_user()
req['user'] = user_schema.dump(current_user)

req['slug'] = slugify_text(req['title'])

# Validate
try:
new_game = game_schema.load(req)
Expand Down Expand Up @@ -153,7 +154,7 @@ def get(self, id):
db.session.commit()
except SQLAlchemyError:
return { 'error': 'Unable to update view count' }, 500

return game_schema.dump(game)

@flask_praetorian.roles_required('admin')
Expand All @@ -171,6 +172,7 @@ def put(self, id):

icon_name = game.icon
banner_name = game.banner
req['slug'] = slugify_text(req['title'])

# Validate
try:
Expand Down
4 changes: 2 additions & 2 deletions api/v1/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ class GameSchema(ma.ModelSchema):
class Meta:
model = Game
sqla_session = db.session
fields = ("id", "title", "synopsis", "icon", "banner", "developer", "genres", "releases", "score", "library_count", "score_rank", "popularity_rank", "trending_page_views", "user")
fields = ("id", "title", "slug", "synopsis", "icon", "banner", "developer", "genres", "releases", "score", "library_count", "score_rank", "popularity_rank", "trending_page_views", "user")

game_schema = GameSchema(exclude=['user'])
games_schema = GameSchema(many=True, exclude=['releases'])
nested_game_schema = GameSchema(only=("id", "title", "synopsis", "icon", "banner", "developer"))
nested_game_schema = GameSchema(only=("id", "title", "slug", "synopsis", "icon", "banner", "developer"))

# LibraryEntrySchema
class LibraryEntrySchema(ma.ModelSchema):
Expand Down
1 change: 1 addition & 0 deletions models/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Game(db.Model):

id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, unique=True, nullable=False)
slug = db.Column(db.String, unique=True, nullable=False)
synopsis = db.Column(db.Text, nullable=False)
icon = db.Column(db.String, unique=True, nullable=False)
banner = db.Column(db.String, unique=True, nullable=False)
Expand Down
6 changes: 5 additions & 1 deletion utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from PIL import Image
from io import BytesIO
import base64
from slugify import slugify

def get_auto_increment(table_name):
"""
Expand Down Expand Up @@ -91,4 +92,7 @@ def base64_validation(base64_string, max_file_size):
if file_type != 'jpeg' and file_type != 'png':
return False

return True
return True

def slugify_text(text):
return slugify(text, replacements=([['\'', '']]))

0 comments on commit aa4fee8

Please sign in to comment.