mgt c7 finish

This commit is contained in:
finn 2024-08-03 11:45:37 -07:00
parent 5b0cd1d22f
commit d7a0167cd6
11 changed files with 57 additions and 8 deletions

View File

@ -1,3 +1,3 @@
FLASK_APP=microblog.py FLASK_APP=microblog.py
FLASK_DEBUG=0 FLASK_DEBUG=1

View File

@ -11,7 +11,7 @@ RUN target=/root/.cache/pip \
# Need to make this explicit as part of expansion, no migrations or venv # Need to make this explicit as part of expansion, no migrations or venv
COPY . . COPY . .
#ENV FLASK_APP app.py ENV FLASK_APP microblog.py
# This might be scary to leave on # This might be scary to leave on
#ENV FLASK_ENV development #ENV FLASK_ENV development

View File

@ -54,6 +54,9 @@ Dockerfile needs dockerignore or preferably explicitly defined copies for:
- requirements - requirements
- not dotflaskenv, vars set with dockerfile - not dotflaskenv, vars set with dockerfile
## docker setup:
- py logger handler StreamHandler is buggy to the point of being useless. Use logfile and link to stdout in Docker build
## notes: ## notes:
- compose has entry that overrides flask with uwsgi for prod - compose has entry that overrides flask with uwsgi for prod
- miminal environment vars come through project env, pass through compose - miminal environment vars come through project env, pass through compose

View File

@ -3,7 +3,7 @@ from config import Config
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate from flask_migrate import Migrate
from flask_login import LoginManager from flask_login import LoginManager
import logging import logging, sys
from logging.handlers import SMTPHandler from logging.handlers import SMTPHandler
app = Flask(__name__) app = Flask(__name__)
@ -13,5 +13,25 @@ migrate = Migrate(app, db)
login = LoginManager(app) login = LoginManager(app)
login.login_view = 'login' login.login_view = 'login'
if not app.debug:
if app.config['MAIL_SERVER']:
auth = None
secure = None
mail_handler = SMTPHandler(
mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
fromaddr=app.config['FROM_ADDRESS'],
toaddrs=app.config['ADMINS'], subject='MB failure.',
credentials=auth, secure=secure)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
if app.config['DC_LOGGING']:
print('#################### DEBUGHERE', file=sys.stderr)
dclog = logging.StreamHandler(stream=sys.stderr)
dclog.setLevel(logging.INFO)
dclog.propagate = False
app.logger.addHandler(dclog)
from app import routes, models, errors from app import routes, models, errors

View File

@ -32,3 +32,17 @@ class EditProfileForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()]) username = StringField('Username', validators=[DataRequired()])
about_me = TextAreaField('About me', validators=[Length(min=0, max=140)]) about_me = TextAreaField('About me', validators=[Length(min=0, max=140)])
submit = SubmitField('Update') submit = SubmitField('Update')
def __init__(self, original_username, *args, **kwargs):
super().__init__(*args, **kwargs)
self.original_username = original_username
def validate_username(self, username):
if username.data != self.original_username:
user = db.session.scalar(sa.select(User).where(User.username == username.data))
if user is not None:
raise ValidationError('Please use a different username.')

View File

@ -18,6 +18,7 @@ def before_request():
@app.route('/index') @app.route('/index')
@login_required @login_required
def index(): def index():
app.logger.info("AAAAAAAAAAAAAAAAAAAAA")
user = {'username': 'Finnaa'} user = {'username': 'Finnaa'}
posts = [ posts = [
@ -74,6 +75,7 @@ def register():
@login_required @login_required
def user(username): def user(username):
user = db.first_or_404(sa.select(User).where(User.username == username)) user = db.first_or_404(sa.select(User).where(User.username == username))
#app.logger.info('PROFILE DEBUG ###############################')
posts = [ posts = [
{'author': user, 'body': 'Test1'}, {'author': user, 'body': 'Test1'},
{'author': user, 'body': 'Test2?'} {'author': user, 'body': 'Test2?'}
@ -83,7 +85,7 @@ def user(username):
@app.route('/edit_profile', methods=['GET', 'POST']) @app.route('/edit_profile', methods=['GET', 'POST'])
@login_required @login_required
def edit_profile(): def edit_profile():
form = EditProfileForm() form = EditProfileForm(current_user.username)
if form.validate_on_submit(): if form.validate_on_submit():
current_user.username = form.username.data current_user.username = form.username.data
current_user.about_me = form.about_me.data current_user.about_me = form.about_me.data

View File

@ -5,14 +5,18 @@ basedir = os.path.abspath(os.path.dirname(__file__))
class Config: class Config:
SECRET_KEY = os.environ.get('FLASK_SECRET_KEY') or 'flasksk' SECRET_KEY = os.environ.get('FLASK_SECRET_KEY') or 'flasksk'
#SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'zapp.db') SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'zapp.db')
SQLALCHEMY_DATABASE_URI = 'mariadb+mariadbconnector://flasku:' + os.environ.get('MYSQL_PASSWORD') + '@db:3306/flask' #SQLALCHEMY_DATABASE_URI = 'mariadb+mariadbconnector://flasku:' + os.environ.get('MYSQL_PASSWORD') + '@db:3306/flask'
MAIL_SERVER = 'pmb' #MAIL_SERVER = 'pmb'
MAIL_SERVER = ''
MAIL_PORT = 25 MAIL_PORT = 25
MAIL_USE_TLS = False MAIL_USE_TLS = False
MAIL_USERNAME = '' MAIL_USERNAME = ''
MAIL_PASSWORD = '' MAIL_PASSWORD = ''
ADMINS = [os.environ.get('ADMIN_EMAIL')] ADMINS = [os.environ.get('ADMIN_EMAIL')]
FROM_ADDRESS = os.environ.get('FROM_ADDRESS')
DC_LOGGING = True

View File

@ -21,4 +21,5 @@ SQLAlchemy==2.0.31
typing_extensions==4.12.2 typing_extensions==4.12.2
Werkzeug==3.0.3 Werkzeug==3.0.3
WTForms==3.1.2 WTForms==3.1.2
uwsgi
mariadb mariadb

View File

@ -38,6 +38,7 @@ services:
- TOKEN_I=${DOTENV_TOKEN_I} - TOKEN_I=${DOTENV_TOKEN_I}
- TOKEN_C=${DOTENV_TOKEN_C} - TOKEN_C=${DOTENV_TOKEN_C}
- ADMIN_EMAIL=${ADMIN_EMAIL} - ADMIN_EMAIL=${ADMIN_EMAIL}
- FROM_ADDRESS=${GITEA_MAIL_FROM}
#ports: #ports:
# - 8000:8000 # - 8000:8000
expose: expose:

View File

@ -37,6 +37,8 @@ services:
- MYSQL_PASSWORD=${DOTENV_MYSQL_FLASK_PASSWORD} - MYSQL_PASSWORD=${DOTENV_MYSQL_FLASK_PASSWORD}
- TOKEN_I=${DOTENV_TOKEN_I} - TOKEN_I=${DOTENV_TOKEN_I}
- TOKEN_C=${DOTENV_TOKEN_C} - TOKEN_C=${DOTENV_TOKEN_C}
- ADMIN_EMAIL=${ADMIN_EMAIL}
- FROM_ADDRESS=${GITEA_MAIL_FROM}
#ports: #ports:
# - 8000:8000 # - 8000:8000
expose: expose:

View File

@ -3,7 +3,7 @@ services:
image: mariadb:lts image: mariadb:lts
restart: always restart: always
healthcheck: healthcheck:
#10-focal test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="${DOTENV_MYSQL_ROOT_PASSWORD}" --silent'] #test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="${DOTENV_MYSQL_ROOT_PASSWORD}" --silent']
test: ['CMD', 'healthcheck.sh', '--connect', '--innodb_initialized'] test: ['CMD', 'healthcheck.sh', '--connect', '--innodb_initialized']
interval: 10s interval: 10s
retries: 5 retries: 5
@ -37,6 +37,8 @@ services:
- MYSQL_PASSWORD=${DOTENV_MYSQL_FLASK_PASSWORD} - MYSQL_PASSWORD=${DOTENV_MYSQL_FLASK_PASSWORD}
- TOKEN_I=${DOTENV_TOKEN_I} - TOKEN_I=${DOTENV_TOKEN_I}
- TOKEN_C=${DOTENV_TOKEN_C} - TOKEN_C=${DOTENV_TOKEN_C}
- ADMIN_EMAIL=${ADMIN_EMAIL}
- FROM_ADDRESS=${GITEA_MAIL_FROM}
#ports: #ports:
# - 8000:8000 # - 8000:8000
expose: expose: