Skip to content

Commit

Permalink
Merge pull request #33 from greyli/pathlib
Browse files Browse the repository at this point in the history
Replace os.path with pathlib
  • Loading branch information
greyli authored Aug 12, 2024
2 parents 0e6ca5b + 0c5acb2 commit c186782
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 27 deletions.
5 changes: 3 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from pathlib import Path

from dotenv import load_dotenv

dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
dotenv_path = Path(__file__).resolve().parent / '.env'
if dotenv_path.exists():
load_dotenv(dotenv_path)

from greybook import create_app # noqa
Expand Down
4 changes: 1 addition & 3 deletions greybook/blueprints/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from flask import Blueprint, abort, current_app, flash, redirect, render_template, request, url_for
from flask_ckeditor import upload_fail, upload_success
from flask_login import current_user, login_required
Expand Down Expand Up @@ -280,6 +278,6 @@ def upload_image():
if not allowed_file(f.filename):
return upload_fail('Image only!')
filename = random_filename(f.filename)
f.save(os.path.join(current_app.config['GREYBOOK_UPLOAD_PATH'], filename))
f.save(current_app.config['GREYBOOK_UPLOAD_PATH'] / filename)
url = url_for('blog.get_image', filename=filename)
return upload_success(url, filename)
8 changes: 3 additions & 5 deletions greybook/core/commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

import click
from flask import current_app
from sqlalchemy import select
Expand Down Expand Up @@ -56,9 +54,9 @@ def init_blog_command(username, password):

db.session.commit()

upload_path = os.path.join(current_app.config['GREYBOOK_UPLOAD_PATH'])
if not os.path.exists(upload_path):
os.makedirs(upload_path)
upload_path = current_app.config['GREYBOOK_UPLOAD_PATH']
if not upload_path.exists():
upload_path.mkdir(parents=True, exist_ok=True)
click.echo('Created the upload folder.')

@app.cli.command('lorem')
Expand Down
7 changes: 3 additions & 4 deletions greybook/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import re
from datetime import datetime, timezone
from typing import List, Optional
Expand Down Expand Up @@ -88,9 +87,9 @@ def delete(self):
upload_url = url_for('blog.get_image', filename='')
images = re.findall(rf'<img.*?src="{upload_url}(.*?)"', self.body)
for image in images:
file_path = os.path.join(upload_path, image)
if os.path.exists(file_path):
os.remove(file_path)
file_path = upload_path / image
if file_path.exists():
file_path.unlink()
db.session.delete(self)
db.session.commit()

Expand Down
4 changes: 2 additions & 2 deletions greybook/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import uuid
from pathlib import Path
from urllib.parse import urljoin, urlparse

from flask import current_app, redirect, request, url_for
Expand Down Expand Up @@ -28,6 +28,6 @@ def allowed_file(filename):


def random_filename(old_filename):
ext = os.path.splitext(old_filename)[1]
ext = Path(old_filename).suffix
new_filename = uuid.uuid4().hex + ext
return new_filename
21 changes: 10 additions & 11 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from unittest.mock import call, patch
import pathlib
from unittest.mock import patch

from greybook.core.extensions import db
from greybook.models import Category, Comment, Post
Expand Down Expand Up @@ -66,20 +66,19 @@ def test_delete_post(self):
data = response.get_data(as_text=True)
self.assertIn('Post deleted.', data)

@patch('os.remove')
@patch('os.path.exists')
def test_delete_post_with_images(self, mock_exists, mock_remove):
@patch('pathlib.Path.unlink')
@patch('pathlib.Path.exists')
def test_delete_post_with_images(self, mock_exists, mock_unlink):
mock_exists.return_value = True
mock_unlink.return_value = None

post = db.session.get(Post, 1)
post.body = '<img src="/uploads/test.png"> <img alt="" src="/uploads/test2.png">'
db.session.commit()

image_path = os.path.join(self.app.config['GREYBOOK_UPLOAD_PATH'], 'test.png')
image_path2 = os.path.join(self.app.config['GREYBOOK_UPLOAD_PATH'], 'test2.png')

mock_exists.return_value = True
self.client.post('/admin/post/1/delete')
self.assertEqual(os.remove.call_count, 2)
mock_remove.assert_has_calls([call(image_path), call(image_path2)])
self.assertEqual(pathlib.Path.unlink.call_count, 2)
self.assertEqual(pathlib.Path.exists.call_count, 2)

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

0 comments on commit c186782

Please sign in to comment.