Compare commits

..

2 Commits

Author SHA1 Message Date
0aea1eb0f1 + prepare_photos script 2024-10-09 17:36:19 +04:00
24ad080a61 Backend
+ Added requirements.txt
* Moved api keys to env
2024-10-09 17:35:59 +04:00
7 changed files with 110 additions and 29 deletions

1
_deploy/prepare_photos/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
env

View File

@ -0,0 +1,75 @@
import os
from string import Template
from typing import List
from natsort import os_sorted
from tqdm.auto import tqdm
from wand.image import Image
CONFIG_TEMPLATE = Template('{id: uuidv4(), category: "$category", image: "$filename", thumbSize: {w: $width, h: $height}},\n')
def run(gallery_folder, resize_config='400x400>') -> List[str]:
"""
For each image in given folder, generate the thumbnail and prepare a config string
@param gallery_folder: Path to gallery folder
@param resize_config: Options are described here: https://docs.wand-py.org/en/stable/guide/resizecrop.html#transform-images
"""
result = []
for current_dir, dirs, files in os.walk(gallery_folder):
with tqdm(os_sorted(files), desc='Processing images', leave=False) as files_pbar:
for file in files_pbar:
if current_dir.startswith('_'):
tqdm.write(f"Skipping: {file} from {current_dir}")
continue
# Don't go deeper
if current_dir != gallery_folder:
continue
file_path = os.path.join(current_dir, file)
category = os.path.basename(current_dir)
files_pbar.set_postfix({"Working on": file_path})
# Create thumbnail folder
thumbnail_folder = os.path.join(current_dir, "thumbs")
os.makedirs(thumbnail_folder, exist_ok=True)
try:
with Image(filename=file_path) as img:
# Generate thumbnail
img.auto_orient()
img.transform(resize=resize_config)
# Save it to file
thumbnail_path = os.path.join(thumbnail_folder, file)
img.save(filename=thumbnail_path)
# Use thumbnail size later
width = img.width
height = img.height
except:
tqdm.write(f"Failed to open file:{file_path}")
continue
# Template string
config_string = CONFIG_TEMPLATE.substitute(category=category,
filename=file,
width=width,
height=height)
result.append(config_string)
return result
if __name__ == '__main__':
config_strings = run('/Users/phzhik/repo/alex-sharoff/public/gallery/interior')
with open("result.js", "w") as f:
f.writelines(config_strings)
tqdm.write('Done')

View File

@ -0,0 +1,3 @@
natsort==8.4.0
tqdm==4.66.5
Wand==0.6.13

4
backend/.env.template Normal file
View File

@ -0,0 +1,4 @@
RECAPTCHA_PUBLIC_KEY=""
RECAPTCHA_PRIVATE_KEY=""
FLASK_APIKEY=""
DEPLOY_PATH="/home/c/cn52774/alex-sharoff/public_html/backend"

View File

@ -1,21 +0,0 @@
import os
# TODO: store keys in env variables
RECAPTCHA_PUBLIC_KEY = "RECAPTCHA_PUBLIC_KEY"
RECAPTCHA_PRIVATE_KEY = "RECAPTCHA_PRIVATE_KEY-L4DsNDlza17N7dEz36"
FLASK_APIKEY = "FLASK_APIKEY"
if 'ALEX_DEBUG' in os.environ:
DEPLOY_PATH = '.'
else:
DEPLOY_PATH = '/home/c/cn52774/alex-sharoff/public_html/backend'
CONTACT_REQUEST_TEMPLATE = """
Обращение через форму обратной связи с сайта alex-sharoff.ru:
Имя: {name}
Контакты: {contacts}
-------
{message}
"""

View File

@ -9,12 +9,15 @@ import telegram_send
from wtforms import StringField
from wtforms.validators import DataRequired
import config as cfg
from environs import Env
# Set locale to UTF-8 as server's one is ANSI - this breaks cyrilic loggng
env = Env()
env.read_env()
# Set locale to UTF-8 as server's one is ANSI - this breaks cyrillic logging
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
logging.basicConfig(filename=f"{cfg.DEPLOY_PATH}/log.log",
logging.basicConfig(filename=f'{env.str("DEPLOY_PATH")}/log.log',
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
@ -24,12 +27,12 @@ logger = logging.getLogger()
app = Flask(__name__)
app.secret_key = cfg.FLASK_APIKEY
app.secret_key = env.str("FLASK_APIKEY")
app.config["APPLICATION_ROOT"] = "/api"
app.config["WTF_CSRF_ENABLED"] = False
app.config["RECAPTCHA_PUBLIC_KEY"] = cfg.RECAPTCHA_PUBLIC_KEY
app.config["RECAPTCHA_PRIVATE_KEY"] = cfg.RECAPTCHA_PRIVATE_KEY
app.config["RECAPTCHA_PUBLIC_KEY"] = env.str("RECAPTCHA_PUBLIC_KEY")
app.config["RECAPTCHA_PRIVATE_KEY"] = env.str("RECAPTCHA_PRIVATE_KEY")
app.config['EXECUTOR_TYPE'] = 'process'
app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
@ -38,7 +41,7 @@ executor = Executor(app)
def send_telegram(message, silent=False):
telegram_send.send(messages=[message], conf=f'{cfg.DEPLOY_PATH}/telegram.conf', silent=silent)
telegram_send.send(messages=[message], conf=f'{env.str("DEPLOY_PATH")}/telegram.conf', silent=silent)
class ContactForm(FlaskForm):
@ -52,8 +55,17 @@ class ContactForm(FlaskForm):
def contact():
form = ContactForm(request.form)
msg_template = """
Обращение через форму обратной связи с сайта alex-sharoff.ru:
Имя: {name}
Контакты: {contacts}
-------
{message}
"""
if form.validate_on_submit():
text = cfg.CONTACT_REQUEST_TEMPLATE.format(
text = msg_template.format(
name=form.name.data,
contacts=form.contacts.data,
message=form.message.data

7
backend/requirements.txt Normal file
View File

@ -0,0 +1,7 @@
environs==11.0.0
Flask==2.3.2
Flask-Cors==3.0.10
Flask-Executor==1.0.0
Flask-WTF==1.1.1
telegram-send==0.34
WTForms==3.0.1