r/learnpython • u/Car2oon_ • 1d ago
My app I need advice
#!/usr/bin/env python3
import argparse
import logging
import os
import sys
from pathlib import Path
import maintain
def configure_logging(log_directory: Path) -> None:
log_directory.mkdir(parents=True, exist_ok=True)
from logging.handlers import RotatingFileHandler
file_handler = RotatingFileHandler(
log_directory / "automation.log",
maxBytes=5_000_000,
backupCount=5,
encoding="utf-8",
)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s UTC %(levelname)s %(message)s",
handlers=[
file_handler,
logging.StreamHandler(sys.stdout),
],
)
# Ensure the timestamps labeled UTC actually use UTC.
import time
logging.Formatter.converter = time.gmtime
def run_maintenance(
app_directory: Path,
public_directory: Path,
) -> int:
config_path = app_directory / "sources.json"
database_path = app_directory / "hustlefinder.sqlite3"
if not config_path.is_file():
logging.error("Missing configuration: %s", config_path)
return 1
public_directory.mkdir(parents=True, exist_ok=True)
# The existing module reads these globals during its operations.
maintain.CONFIG = str(config_path)
maintain.DB = str(database_path)
logging.info("Maintenance started")
try:
configuration = maintain.config()
if not isinstance(configuration.get("sources"), list):
raise ValueError("'sources' must be an array")
if not isinstance(
configuration.get("allowed_link_hosts", []), list
):
raise ValueError("'allowed_link_hosts' must be an array")
with maintain.database():
pass
except Exception:
logging.exception("Configuration or database initialization failed")
return 1
failed = False
# Continue other maintenance even when a feed fails.
try:
failures = maintain.refresh()
if failures:
failed = True
logging.error(
"%d feed refresh(es) failed; inspect the runs table",
failures,
)
else:
logging.info("Feed refresh completed")
except Exception:
failed = True
logging.exception("Feed refresh failed")
try:
maintain.check()
logging.info("Application-link checks completed")
except Exception:
failed = True
logging.exception("Application-link checks failed")
try:
output_path = public_directory / "jobs.json"
# Existing export uses a temporary file followed by replacement.
# Only approved/published records enter the public export.
maintain.export(str(output_path), public=True)
logging.info("Published job data exported to %s", output_path)
except Exception:
failed = True
logging.exception("Public export failed")
logging.info(
"Maintenance finished: %s",
"completed with errors" if failed else "successful",
)
return 1 if failed else 0
def main() -> int:
parser = argparse.ArgumentParser(
description="Run HustleFinder listing maintenance"
)
parser.add_argument(
"--app-dir",
type=Path,
default=Path(__file__).resolve().parent,
)
parser.add_argument(
"--public-dir",
type=Path,
required=True,
)
args = parser.parse_args()
app_directory = args.app_dir.resolve()
public_directory = args.public_dir.resolve()
# Private files should not become publicly served files.
if (
public_directory == app_directory
or public_directory in app_directory.parents
):
parser.error(
"--public-dir must not be the app directory "
"or a parent of the app directory"
)
os.umask(0o027)
configure_logging(app_directory / "logs")
try:
return run_maintenance(app_directory, public_directory)
except Exception:
logging.exception("Unexpected maintenance failure")
return 1
if __name__ == "__main__":
raise SystemExit(main())
5
u/astolfoballsHD 1d ago
My advice would definitely be to upload this somewhere that allows proper code formatting and link back here
1
1
u/desrtfx 1d ago
Yeah, let's take our Crystal Balls out and do some divination to maybe figure what you want. It definitely is not possible from the code since it lost any and all formatting and indentation, which makes Python code ambiguous.
You want help. It is your obligation to provide your code in a properly formatted, readable way (best on something like github), and to tell us what you need help with/advice for. We are not here to do blind guessing and even less so if you don't even tell us anything and just throw a wall of unformatted, undindented code at us.
0
u/Car2oon_ 1d ago edited 1d ago
I already have an HTML/CSS/JavaScript prototype with job search, category filters, an employer submission form, and resource links. I also have a separate automation package intended to refresh listings and check job links, but it hasn’t been deployed.
Here’s what I need help coding:
Connect a shared database. Replace the demo-only setup so everyone sees the same approved job listings.
Make the employer form actually submit jobs. Right now, submissions save only in the visitor’s browser. I need them saved to the database as “pending review.”
Build a secure administrator review page. Let me approve, reject, edit, and close listings. Only approved, active jobs should appear publicly.
Connect search and filters to the real listings. Keep the existing interface working with the database.
Review and deploy the automation package. Connect approved employer sources, schedule updates, and flag expired or broken application links. Check the existing code before assuming any of this already works.
Deploy and test the app. Get the website, database, and scheduled tasks working together, then test the full submission-to-approval process.
0
u/Car2oon_ 1d ago
automation_worker.py
This worker imports the existing maintain.py rather than creating a second copy of its logic. I don’t know rather that’s good or bad
8
u/happinessMachiine 1d ago
Like general advice, or were you going to elaborate on something more specific?