Skip to content

Commit

Permalink
Merge com novas funcionalidades
Browse files Browse the repository at this point in the history
  • Loading branch information
thisiscleverson committed Apr 9, 2024
2 parents 1ea8790 + 5673373 commit dddff70
Show file tree
Hide file tree
Showing 15 changed files with 373 additions and 259 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ jobs:
EXCLUDE: ".env, /test/, .gitignore, README.md"
SCRIPT_AFTER: |
make deploy
run: make deploy
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ status:
build:
docker compose up --build

deploy: build stop
sudo cp ./app.service /etc/systemd/system/ && \
sudo systemctl enable app && \
sudo systemctl restart app
deploy:
cd /var/www/cleverson.online \
docker-compose down \
docker-compose pull \
docker-compose up -d
29 changes: 14 additions & 15 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from flask_session import Session

from .models import config_models
from .errorhandler import page_not_found, internal_error


db_dir = '/.database'

Expand All @@ -18,27 +20,25 @@ def create_register_blueprint(app):
from .auth import auth
from .admin import admin
from .blog import blog

from .api import api

app.register_blueprint(auth)
app.register_blueprint(admin)
app.register_blueprint(blog)
app.register_blueprint(api)


def erro_handler(app):
@app.errorhandler(404)
def page_not_found(error):
return render_template('errorHandler/404.html'), 404

@app.errorhandler(500)
def internal_error(error):
return render_template('errorHandler/500.html'), 500
def create_register_erro_handler(app):
app.register_error_handler(404, page_not_found)
app.register_error_handler(500, internal_error)


def create_app():
app = Flask(__name__,
template_folder='../templates',
static_folder='../static'
)
app = Flask(
__name__,
template_folder='../templates',
static_folder='../static'
)

app.config["SQLALCHEMY_DATABASE_URI"] = f'sqlite:///{config_database_path(db_dir)}'
app.config["SESSION_PERMANENT"] = False
Expand All @@ -48,8 +48,7 @@ def create_app():
config_models(app)
Migrate(app, app.db)
create_register_blueprint(app)

erro_handler(app)
create_register_erro_handler(app)

return app

Expand Down
164 changes: 22 additions & 142 deletions app/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,155 +8,35 @@

@admin.route('/admin', methods=['GET'])
def home():
if not session.get("token"):
return redirect('/login')
if not session.get("token"):
return redirect('/login')

draft_list = Contents.query.filter(Contents.status == "draft").all()
contents_list = Contents.query.filter(Contents.status == "published").all()
draft_list = Contents.query.filter(Contents.status == "draft").all()
contents_list = Contents.query.filter(Contents.status == "published").all()

return render_template('admin/home.html', draft_list=draft_list, contents_list=contents_list)
return render_template('admin/home.html', draft_list=draft_list, contents_list=contents_list)


@admin.route('/publish', methods=['GET', 'POST'])
def publish():
if not session.get("token"):
return redirect('/login')

if request.method == "POST":
title, content, description = get_title_content_description()

if data_valid(title, content):
insert_content(
title=title,
body=content,
status='published',
accessType='public',
description=description
)
return redirect('/')

flash("O titulo ou texto não está preenchido adequadamente! Por favor, verifique se você preencheu os campo corretamente!")

return render_template('admin/editor.html', is_draft_mode=True)


@admin.route('/update/<id>', methods=['GET', 'POST', 'PUT'])
def update(id):
if not session.get("token"):
return redirect('/login')

if request.method == "POST":
data = request.get_json()
@admin.route('/editar/<id>', methods=['GET'])
def editor(id):
if not session.get("token"):
return redirect('/login')

if request.method == "GET":
post_data = Contents.query.filter(Contents.id == id).first()

title = data['title']
body = data['body']
description = data['description']

if data_valid(title, body):
update_content(id=id, title=title,body=body, description=description)
return jsonify({"status_code": 200, 'success': True}), 200
title = post_data.title
body = post_data.body
status = post_data.status
description = post_data.description

return jsonify({"status_code":200, "success":True, "message":"O titulo ou texto não está preenchido adequadamente! Por favor, verifique se você preencheu os campo corretamente!"}), 200

if request.method == 'PUT':
data = request.get_json()

title = data['title']
body = data['body']
description = data['description']

if data_valid(title, body):
update_content(id=id, title=title,body=body, status='published', description=description)
return jsonify({"status_code": 200, 'success': True}), 200

if request.method == "GET":
title, body, description = obtain_draft_title_and_body(id)
return render_template('admin/editor.html', id=id, title=title, body=body, description=description, is_draft_mode=False)

return render_template('admin/editor.html', id=id, title=title, body=body, description=description, is_draft_mode=True, status=status)

@admin.route('/draft', methods=['POST'])
def draft():
if request.method == "POST":
data = request.get_json()

title = data['title']
content = data['body']
description = data['description']

if data_valid(title, content):
insert_content(
title=title,
body=content,
status='draft',
accessType='public',
description=description
)
return jsonify({"status_code": 200, 'success': True}), 200

return jsonify({
"error": {
"status_code": 400,
"message": "Os campos título e texto não foram preenchidos adequadamente. Por favor, verifique se você preencheu os campos corretamente."
}
}), 400

return jsonify({"status_code": 400, "erro": "Método não permitido"}), 405


@admin.route('/delete/post/<post_id>', methods=['DELETE'])
def delete_post(post_id):
if not session.get("token"):
return jsonify({
"status": "error",
"message": "Você não tem permissão para deletar este post.",
"code": 403
}), 403

Contents.query.filter(Contents.id == post_id).delete()
db.session.commit()

return jsonify({"status_code":200, "success":True}), 200

def obtain_draft_title_and_body(id: str):
query = Contents.query.filter(Contents.id == id)
draft_data = query.first()
title, body, description = draft_data.title, draft_data.body, draft_data.description
return title, body, description

def get_title_content_description():
return request.form.get("title"), request.form.get("markdown-content"), request.form.get('description')

def data_valid(title: str, content: str) -> bool:
return len(title.strip()) > 0 and len(content.strip()) > 0

def update_content(id:str, title: str, body: str,description:str, status:str = None) -> None:
if status is None:
Contents.query.filter_by(id=id).update({Contents.title:title, Contents.body:body, Contents.description:description})
db.session.commit()
return
Contents.query.filter_by(id=id).update({Contents.title:title, Contents.body:body, Contents.status:status, Contents.description:description})
db.session.commit()

def generate_slug(title:str) -> str:
title = unidecode(title)
slug = re.sub(r'[^\w\s-]', '', title.lower())
slug = re.sub(r'\s', '-', slug)
return slug
@admin.route('/publicar', methods=['GET', 'POST'])
def publish():
if not session.get("token"):
return redirect('/login')

def insert_content(title: str, body: str, status: str, accessType: str, description:str) -> None:
"""
status: "published" or "draft"
accessType: "public" or "private"
"""
db.session.add(
Contents(
id=str(uuid4()),
title=title,
body=body,
slug=generate_slug(title),
status=status,
accessType=accessType,
description=description
)
)
db.session.commit()
return render_template('admin/editor.html', status=None)
Loading

0 comments on commit dddff70

Please sign in to comment.