Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
drorganvidez committed Apr 20, 2024
2 parents 3f5e988 + d17cc76 commit 750ecc1
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 44 deletions.
3 changes: 2 additions & 1 deletion Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ RUN apk add --no-cache mysql-client \
# Set the working directory in the container to /app
WORKDIR /app

# Copy the contents of the local app/ directory to the /app directory in the container
# Copy files
COPY app/ ./app
COPY core/ ./core

# Copy requirements.txt into the working directory /app
COPY requirements.txt .
Expand Down
42 changes: 11 additions & 31 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import os
import logging

from flask import Flask, render_template
from flask import Flask
from flask_login import current_user
from flask_sqlalchemy import SQLAlchemy
from dotenv import load_dotenv
from flask_migrate import Migrate

from app.managers.blueprint_manager import BlueprintManager
from app.managers.config_manager import ConfigManager
from core.managers.blueprint_manager import BlueprintManager
from core.managers.config_manager import ConfigManager
from core.managers.error_handler_manager import ErrorHandlerManager
from core.managers.logging_manager import LoggingManager

# Load environment variables
load_dotenv()
Expand Down Expand Up @@ -43,12 +44,13 @@ def load_user(user_id):
from app.blueprints.auth.models import User
return User.query.get(int(user_id))

# Logging
logging.basicConfig(filename='app.log', level=logging.ERROR,
format='%(asctime)s:%(levelname)s:%(message)s')
# Set up logging
logging_manager = LoggingManager(app)
logging_manager.setup_logging()

# Custom error handlers
register_error_handlers(app)
# Initialize error handler manager
error_handler_manager = ErrorHandlerManager(app)
error_handler_manager.register_error_handlers()

# Injecting environment variables into jinja context
@app.context_processor
Expand All @@ -62,28 +64,6 @@ def inject_vars_into_jinja():
return app


def register_error_handlers(app):
@app.errorhandler(500)
def base_error_handler(e):
app.logger.error('Internal Server Error: %s', str(e)) # Error logging
return render_template('500.html'), 500

@app.errorhandler(404)
def error_404_handler(e):
app.logger.warning('Page Not Found: %s', str(e)) # Warning logging
return render_template('404.html'), 404

@app.errorhandler(401)
def error_401_handler(e):
app.logger.warning('Unauthorized Access: %s', str(e)) # Warning logging
return render_template('401.html'), 401

@app.errorhandler(400)
def error_400_handler(e):
app.logger.warning('Bad Request: %s', str(e)) # Warning logging
return render_template('400.html'), 400


def get_test_client():
"""
Function to get the test client of the application.
Expand Down
2 changes: 1 addition & 1 deletion app/blueprints/auth/seeders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from app.blueprints.auth.models import User
from app.blueprints.profile.models import UserProfile
from app.seeders.BaseSeeder import BaseSeeder
from core.seeders.BaseSeeder import BaseSeeder


class AuthSeeder(BaseSeeder):
Expand Down
2 changes: 1 addition & 1 deletion app/blueprints/profile/repositories.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app.blueprints.profile.models import UserProfile
from app.repositories.BaseRepository import BaseRepository
from core.repositories.BaseRepository import BaseRepository


class UserProfileRepository(BaseRepository):
Expand Down
2 changes: 1 addition & 1 deletion app/blueprints/profile/services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app.blueprints.profile.repositories import UserProfileRepository
from app.services.BaseService import BaseService
from core.services.BaseService import BaseService


class UserProfileService(BaseService):
Expand Down
2 changes: 1 addition & 1 deletion app/blueprints/zenodo/repositories.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app.blueprints.zenodo.models import Zenodo
from app.repositories.BaseRepository import BaseRepository
from core.repositories.BaseRepository import BaseRepository


class ZenodoRepository(BaseRepository):
Expand Down
2 changes: 1 addition & 1 deletion app/blueprints/zenodo/services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app.blueprints.zenodo.repositories import ZenodoRepository
from app.services.BaseService import BaseService
from core.services.BaseService import BaseService

import os

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions core/managers/error_handler_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from flask import render_template


class ErrorHandlerManager:
def __init__(self, app):
self.app = app

def register_error_handlers(self):
@self.app.errorhandler(500)
def internal_error(e):
self.app.logger.error('Internal Server Error: %s', str(e))
return render_template('500.html'), 500

@self.app.errorhandler(404)
def not_found_error(e):
self.app.logger.warning('Page Not Found: %s', str(e))
return render_template('404.html'), 404

@self.app.errorhandler(401)
def unauthorized_error(e):
self.app.logger.warning('Unauthorized Access: %s', str(e))
return render_template('401.html'), 401

@self.app.errorhandler(400)
def bad_request_error(e):
self.app.logger.warning('Bad Request: %s', str(e))
return render_template('400.html'), 400
29 changes: 29 additions & 0 deletions core/managers/logging_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logging
from logging.handlers import RotatingFileHandler


class LoggingManager:
def __init__(self, app):
self.app = app

def setup_logging(self):
# Configure log format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# Configure the log file with file rotation
file_handler = RotatingFileHandler('app.log', maxBytes=10240, backupCount=10)
file_handler.setLevel(logging.ERROR)
file_handler.setFormatter(formatter)

# Add handler to app logger
self.app.logger.addHandler(file_handler)

# Configure console log if necessary
if self.app.debug:
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
self.app.logger.addHandler(stream_handler)

# Set the overall log level
self.app.logger.setLevel(logging.INFO)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added core/services/__init__.py
Empty file.
2 changes: 0 additions & 2 deletions public/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion rosemary/commands/db_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import click
from flask.cli import with_appcontext

from app.seeders.BaseSeeder import BaseSeeder
from core.seeders.BaseSeeder import BaseSeeder
from rosemary.commands.db_reset import db_reset


Expand Down
2 changes: 1 addition & 1 deletion rosemary/commands/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def linter():

# Define the directories to be checked with flake8
directories = ["/app/app", "/app/rosemary"]
directories = ["/app/app", "/app/rosemary", "/app/core"]

# Run flake8 in each directory
for directory in directories:
Expand Down
2 changes: 1 addition & 1 deletion rosemary/templates/blueprint_repositories.py.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app.blueprints.{{ blueprint_name }}.models import {{ blueprint_name | pascalcase }}
from app.repositories.BaseRepository import BaseRepository
from core.repositories.BaseRepository import BaseRepository


class {{ blueprint_name | pascalcase }}Repository(BaseRepository):
Expand Down
2 changes: 1 addition & 1 deletion rosemary/templates/blueprint_seeders.py.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app.seeders.BaseSeeder import BaseSeeder
from core.seeders.BaseSeeder import BaseSeeder


class {{ blueprint_name | pascalcase }}Seeder(BaseSeeder):
Expand Down
2 changes: 1 addition & 1 deletion rosemary/templates/blueprint_services.py.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app.blueprints.{{ blueprint_name }}.repositories import {{ blueprint_name | pascalcase }}Repository
from app.services.BaseService import BaseService
from core.services.BaseService import BaseService


class {{ blueprint_name | pascalcase }}(BaseService):
Expand Down

0 comments on commit 750ecc1

Please sign in to comment.