Skip to content
Open
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
11 changes: 4 additions & 7 deletions filebrowser_safe/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ def __init__(self, attrs=None):
self.directory = attrs.get('directory', '')
self.extensions = attrs.get('extensions', '')
self.format = attrs.get('format', '')
if attrs is not None:
self.attrs = attrs.copy()
else:
self.attrs = {}
self.attrs = attrs.copy() if attrs is not None else {}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FileBrowseWidget.__init__ refactored with the following changes:

  • Replace if statement with if expression (assign-if-exp)


def render(self, name, value, attrs=None):
if value is None:
value = ""
directory = self.directory
if self.directory:
if callable(self.directory):
if directory:
if callable(directory):
Comment on lines -41 to +39
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FileBrowseWidget.render refactored with the following changes:

  • Use previously assigned local variable (use-assigned-variable)

directory = self.directory()
directory = os.path.normpath(datetime.datetime.now().strftime(directory))
fullpath = os.path.join(get_directory(), directory)
Expand Down Expand Up @@ -77,7 +74,7 @@ def clean(self, value):
if value == '':
return value
file_extension = os.path.splitext(value)[1].lower().split("?")[0]
if self.extensions and not file_extension in self.extensions:
if self.extensions and file_extension not in self.extensions:
Comment on lines -80 to +77
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FileBrowseFormField.clean refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

raise forms.ValidationError(self.error_messages['extension'] % {'ext': file_extension, 'allowed': ", ".join(self.extensions)})
return value

Expand Down
11 changes: 2 additions & 9 deletions filebrowser_safe/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ def url_join(*args):
"""
URL join routine.
"""
if args[0].startswith("http://"):
url = "http://"
else:
url = "/"
url = "http://" if args[0].startswith("http://") else "/"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function url_join refactored with the following changes:

  • Replace if statement with if expression (assign-if-exp)

for arg in args:
arg = str(arg).replace("\\", "/")
arg_split = arg.split("/")
Expand Down Expand Up @@ -211,11 +208,7 @@ def convert_filename(value):
v = re.sub(r'[^\w\s-]', '', v).strip()
normalized.append(v)

if len(normalized) > 1:
value = '.'.join(normalized)
else:
value = normalized[0]

value = '.'.join(normalized) if len(normalized) > 1 else normalized[0]
Comment on lines -214 to +211
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function convert_filename refactored with the following changes:

  • Replace if statement with if expression (assign-if-exp)

if CONVERT_FILENAME:
value = value.replace(" ", "_").lower()

Expand Down
10 changes: 5 additions & 5 deletions filebrowser_safe/templatetags/fb_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ def pagination(context):
if not paginator.num_pages or paginator.num_pages == 1:
page_range = []
else:
ON_EACH_SIDE = 3
ON_ENDS = 2

Comment on lines -20 to -22
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function pagination refactored with the following changes:

  • Move assignments closer to their usage (move-assign)
  • Replace range(0, x) with range(x) (remove-zero-from-range)

# If there are 10 or fewer pages, display links to every page.
# Otherwise, do some fancy
if paginator.num_pages <= 10:
Expand All @@ -29,12 +26,15 @@ def pagination(context):
# links at either end of the list of pages, and there are always
# ON_EACH_SIDE links at either end of the "current page" link.
page_range = []
ON_EACH_SIDE = 3
ON_ENDS = 2

if page_num > (ON_EACH_SIDE + ON_ENDS):
page_range.extend(list(range(0, ON_EACH_SIDE - 1)))
page_range.extend(list(range(ON_EACH_SIDE - 1)))
page_range.append(DOT)
page_range.extend(list(range(page_num - ON_EACH_SIDE, page_num + 1)))
else:
page_range.extend(list(range(0, page_num + 1)))
page_range.extend(list(range(page_num + 1)))
if page_num < (paginator.num_pages - ON_EACH_SIDE - ON_ENDS - 1):
page_range.extend(list(range(page_num + 1, page_num + ON_EACH_SIDE + 1)))
page_range.append(DOT)
Expand Down
2 changes: 1 addition & 1 deletion filebrowser_safe/templatetags/fb_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def render(self, context):
format = ''
if filetype and format and filetype in SELECT_FORMATS[format]:
selectable = True
elif filetype and format and filetype not in SELECT_FORMATS[format]:
elif filetype and format:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SelectableNode.render refactored with the following changes:

  • Remove redundant conditional (remove-redundant-if)

selectable = False
else:
selectable = True
Expand Down
14 changes: 6 additions & 8 deletions filebrowser_safe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ def browse(request):

# INITIAL VARIABLES
results_var = {'results_total': 0, 'results_current': 0, 'delete_total': 0, 'images_total': 0, 'select_total': 0}
counter = {}
for k, v in EXTENSIONS.items():
counter[k] = 0

counter = {k: 0 for k, v in EXTENSIONS.items()}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function browse refactored with the following changes:

  • Remove redundant conditional (remove-redundant-if)
  • Convert for loop into dictionary comprehension (dict-comprehension)

dir_list, file_list = default_storage.listdir(abs_path)
files = []
for file in dir_list + file_list:
Expand Down Expand Up @@ -140,7 +137,7 @@ def browse(request):
results_var['images_total'] += 1
if fileobject.filetype != 'Folder':
results_var['delete_total'] += 1
elif fileobject.filetype == 'Folder' and fileobject.is_empty:
elif fileobject.is_empty:
results_var['delete_total'] += 1
if query.get('type') and query.get('type') in SELECT_FORMATS and fileobject.filetype in SELECT_FORMATS[query.get('type')]:
results_var['select_total'] += 1
Expand Down Expand Up @@ -296,9 +293,10 @@ def _check_file(request):
fileArray = {}
if request.method == 'POST':
for k, v in list(request.POST.items()):
if k != "folder":
if default_storage.exists(os.path.join(get_directory(), folder, v)):
fileArray[k] = v
if k != "folder" and default_storage.exists(
os.path.join(get_directory(), folder, v)
):
fileArray[k] = v
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _check_file refactored with the following changes:

  • Merge nested if conditions (merge-nested-ifs)

return HttpResponse(dumps(fileArray))


Expand Down