Commit 002d4f60 authored by Lukas Böhm's avatar Lukas Böhm 🎱
Browse files

fixed timers

parent b50419ec
import os
from dotenv import load_dotenv
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from redis import Redis
from rq import Queue
basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
datadir = os.path.abspath(os.path.join(basedir, 'data'))
load_dotenv(dotenv_path=os.path.join(basedir, '.env'), override=True)
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') or 'jonas ist ein kek'
......@@ -18,14 +15,15 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 # 5 MB
app.config['REDIS_URI'] = os.getenv('REDIS_URL') or 'redis://'
app.jinja_env.globals['len'] = len
db = SQLAlchemy(app)
task_queue = Queue(connection=Redis.from_url(app.config['REDIS_URI']))
# task_queue = Queue(connection=Redis.from_url(app.config['REDIS_URI']))
# task_queue = Queue(connection=Redis(host='redis'))
task_queue = Queue(connection=Redis(host='localhost'))
from app.models import *
db.create_all()
db.session.commit()
......
from wtforms import StringField, SubmitField, DateField, SelectField, FieldList, FormField, FileField, TextAreaField, \
MultipleFileField, SelectMultipleField, BooleanField, IntegerField, PasswordField
from wtforms import StringField, SubmitField, SelectField, MultipleFileField, BooleanField, PasswordField
from wtforms.validators import DataRequired, Optional, length
from flask_wtf import FlaskForm
......
import os
from datetime import timedelta, datetime
from datetime import timedelta, datetime, timezone
from flask import current_app
from werkzeug.security import generate_password_hash
from app import db
......@@ -31,7 +29,7 @@ class Share(db.Model):
# set attributes
self.id = share_id
self.name = name
self.expires = datetime.now() + timedelta(minutes=float(timer)) if timer > 0 else None
self.expires = datetime.now(timezone.utc) + timedelta(minutes=float(timer)) if timer > 0 else None
self.download_limit = download_limit if download_limit > 0 else 100000000
self.is_public = is_public
self.password = generate_password_hash(password) if password else None
......
import os
from datetime import datetime, timedelta
from datetime import timedelta
from werkzeug.exceptions import HTTPException
from app import app, db
from flask import redirect, url_for, render_template, send_from_directory, current_app, abort
from flask import redirect, url_for, render_template, send_from_directory, current_app, abort, request
from app.models import Share, Attachment
from app.forms import UploadForm
from app import task_queue as que
from shutil import make_archive, rmtree
def get_and_check_share(share_id):
share = Share.query.get(share_id)
if share is None:
......@@ -22,6 +20,7 @@ def get_and_check_share(share_id):
def delete_share(share_id):
print(f'delete share: { share_id }')
with app.app_context():
share = Share.query.get_or_404(str(share_id))
# delete files
......@@ -71,12 +70,11 @@ def public():
@app.route('/upload', methods=['GET', 'POST'])
def upload():
form = UploadForm()
if form.validate_on_submit():
# create share
share = Share(name=str(form.name.data),
timer=int(form.timer.data),
download_limit=int(form.download_limit.data)
timer=form.timer.data,
download_limit=form.download_limit.data
)
db.session.add(share)
db.session.flush()
......@@ -86,12 +84,13 @@ def upload():
# create zip
make_archive(
os.path.join(current_app.config['MEDIA_LOCATION'], share.id),
'zip', # TODO MORE FILE FORMATS LIKE .tar and .tar.gz
'zip',
os.path.join(current_app.config['MEDIA_LOCATION'], share.id)
)
# put timer into scheduler queue
print(share.expires)
if share.expires:
que.enqueue_at(share.expires, delete_share, share.id)
que.enqueue_at(datetime=share.expires, f=delete_share, args=[share.id])
# wrap it up
db.session.commit()
return url_for('shared', share_id=share.id)
......
......@@ -10,7 +10,7 @@
{# TODO live search bar
<div class="row">
<input type="text" class="form-control" placeholder="TODO TODO TODO TODO TODO"/>
<input type="text" class="form-control" placeholder="Suche ..."/>
</div> #}
{% for row in shares|batch(3, 'kek') %}
......
......@@ -2,22 +2,22 @@ version: "3.8"
services:
redis:
container_name: "chiefsend-redis"
hostname: "chiefsend-redis"
container_name: "redis"
hostname: "redis"
image: "redis:latest"
ports:
- "6379:6379"
command: [ "redis-server", "--appendonly", "yes" ]
# ports:
# - "5000:6379"
#command: [ "redis-server", "--appendonly", "yes" ]
rq:
container_name: "chiefsend-rq"
image: "geekinutah/python-rq-worker"
environment:
- REDIS_HOST=chiefsend-redis
- REDIS_HOST=redis
links:
- redis:redis
- redis
web:
container_name: "chiefsend-web"
build: .
......
......@@ -3,4 +3,3 @@ Flask-SQLAlchemy
rq
redis
Flask-WTF
python-dotenv
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment