Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve image handling #34

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions greybook/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,18 @@ def reviewed_comments_count(self):

def delete(self):
upload_path = current_app.config['GREYBOOK_UPLOAD_PATH']
upload_url = url_for('blog.get_image', filename='')
images = re.findall(rf'<img.*?src="{upload_url}(.*?)"', self.body)
images = self.extract_images()
for image in images:
file_path = upload_path / image
if file_path.exists():
file_path.unlink()
db.session.delete(self)
db.session.commit()

def extract_images(self):
upload_url = url_for('blog.get_image', filename='')
return re.findall(rf'<img.*?src="{upload_url}(.*?)"', self.body)


class Comment(db.Model):
__tablename__ = 'comment'
Expand Down
2 changes: 1 addition & 1 deletion greybook/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BaseConfig:
GREYBOOK_SLOW_QUERY_THRESHOLD = 1

GREYBOOK_UPLOAD_PATH = os.getenv('GREYBOOK_UPLOAD_PATH', BASE_DIR / 'uploads')
GREYBOOK_ALLOWED_IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif']
GREYBOOK_ALLOWED_IMAGE_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif']
GREYBOOK_LOGGING_PATH = os.getenv('GREYBOOK_LOGGING_PATH', BASE_DIR / 'logs/greybook.log')
GREYBOOK_ERROR_EMAIL_SUBJECT = '[Greybook] Application Error'

Expand Down
42 changes: 21 additions & 21 deletions greybook/static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
dayjs.extend(window.dayjs_plugin_relativeTime)
dayjs.extend(window.dayjs_plugin_utc)
dayjs.extend(window.dayjs_plugin_localizedFormat)
dayjs.extend(window.dayjs_plugin_relativeTime);
dayjs.extend(window.dayjs_plugin_utc);
dayjs.extend(window.dayjs_plugin_localizedFormat);

const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));

function renderAllDatetime() {
// render normal time
const elements = document.querySelectorAll('.dayjs')
const elements = document.querySelectorAll('.dayjs');
elements.forEach(elem => {
const date = dayjs.utc(elem.innerHTML)
const format = elem.dataset.format ?? 'LL'
elem.innerHTML = date.local().format(format)
})
const date = dayjs.utc(elem.innerHTML);
const format = elem.dataset.format ?? 'LL';
elem.innerHTML = date.local().format(format);
});
// render from now time
const fromNowElements = document.querySelectorAll('.dayjs-from-now')
const fromNowElements = document.querySelectorAll('.dayjs-from-now');
fromNowElements.forEach(elem => {
const date = dayjs.utc(elem.innerHTML)
elem.innerHTML = date.local().fromNow()
})
const date = dayjs.utc(elem.innerHTML);
elem.innerHTML = date.local().fromNow();
});
// render tooltip time
const toolTipElements = document.querySelectorAll('.dayjs-tooltip')
const toolTipElements = document.querySelectorAll('.dayjs-tooltip');
toolTipElements.forEach(elem => {
const date = dayjs.utc(elem.dataset.timestamp)
const format = elem.dataset.format ?? 'LLL'
elem.dataset.bsTitle = date.local().format(format)
const tooltip = new bootstrap.Tooltip(elem)
})
const date = dayjs.utc(elem.dataset.timestamp);
const format = elem.dataset.format ?? 'LLL';
elem.dataset.bsTitle = date.local().format(format);
const tooltip = new bootstrap.Tooltip(elem);
});
}

document.addEventListener('DOMContentLoaded', renderAllDatetime)
document.addEventListener('DOMContentLoaded', renderAllDatetime);
5 changes: 1 addition & 4 deletions greybook/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ def redirect_back(default='blog.index', **kwargs):


def allowed_file(filename):
return (
'.' in filename
and filename.rsplit('.', 1)[1].lower() in current_app.config['GREYBOOK_ALLOWED_IMAGE_EXTENSIONS']
)
return '.' in filename and Path(filename).suffix.lower() in current_app.config['GREYBOOK_ALLOWED_IMAGE_EXTENSIONS']


def random_filename(old_filename):
Expand Down
10 changes: 7 additions & 3 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pathlib
from unittest.mock import patch

from greybook.core.extensions import db
Expand Down Expand Up @@ -76,9 +75,14 @@ def test_delete_post_with_images(self, mock_exists, mock_unlink):
post.body = '<img src="/uploads/test.png"> <img alt="" src="/uploads/test2.png">'
db.session.commit()

expected_images = ['test.png', 'test2.png']
with patch('greybook.models.url_for', return_value='/uploads/'):
extracted_images = post.extract_images()
self.assertEqual(extracted_images, expected_images)

self.client.post('/admin/post/1/delete')
self.assertEqual(pathlib.Path.unlink.call_count, 2)
self.assertEqual(pathlib.Path.exists.call_count, 2)
self.assertEqual(mock_exists.call_count, 2)
self.assertEqual(mock_unlink.call_count, 2)

def test_delete_comment(self):
response = self.client.get('/admin/comment/1/delete', follow_redirects=True)
Expand Down
Loading