Verified Commit d17ceec1 authored by Agate's avatar Agate 💬

See #297: linting of various, uncommon errors

parent ab80dffe
from django.conf.urls import include, url
from dynamic_preferences.api.viewsets import GlobalPreferencesViewSet
from dynamic_preferences.users.viewsets import UserPreferencesViewSet
from rest_framework import routers
from rest_framework.urlpatterns import format_suffix_patterns
from rest_framework_jwt import views as jwt_views
from funkwhale_api.activity import views as activity_views
from funkwhale_api.instance import views as instance_views
from funkwhale_api.music import views
from funkwhale_api.playlists import views as playlists_views
from funkwhale_api.subsonic.views import SubsonicViewSet
......
......@@ -2,7 +2,7 @@ import os
import django
from .routing import application
from .routing import application # noqa
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
......
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.conf.urls import url
......
......@@ -11,7 +11,6 @@ https://docs.djangoproject.com/en/dev/ref/settings/
from __future__ import absolute_import, unicode_literals
import datetime
import os
from urllib.parse import urlparse, urlsplit
import environ
......@@ -23,7 +22,6 @@ ROOT_DIR = environ.Path(__file__) - 3 # (/a/b/myfile.py - 3 = /)
APPS_DIR = ROOT_DIR.path("funkwhale_api")
env = environ.Env()
try:
env.read_env(ROOT_DIR.file(".env"))
except FileNotFoundError:
......@@ -333,12 +331,12 @@ CACHES["default"]["OPTIONS"] = {
}
########## CELERY
# CELERY
INSTALLED_APPS += ("funkwhale_api.taskapp.celery.CeleryConfig",)
CELERY_BROKER_URL = env(
"CELERY_BROKER_URL", default=env("CACHE_URL", default=CACHE_DEFAULT)
)
########## END CELERY
# END CELERY
# Location of root django.contrib.admin URL, use {% url 'admin:index' %}
# Your common stuff: Below this line define 3rd party library settings
......
......@@ -10,6 +10,7 @@ Local settings
from .common import * # noqa
# DEBUG
# ------------------------------------------------------------------------------
DEBUG = env.bool("DJANGO_DEBUG", default=True)
......@@ -49,10 +50,10 @@ INSTALLED_APPS += ("debug_toolbar",)
# ------------------------------------------------------------------------------
TEST_RUNNER = "django.test.runner.DiscoverRunner"
########## CELERY
# CELERY
# In development, all tasks will be executed locally by blocking until the task returns
CELERY_TASK_ALWAYS_EAGER = False
########## END CELERY
# END CELERY
# Your local stuff: Below this line define 3rd party library settings
......
......@@ -11,8 +11,6 @@ Production Configurations
"""
from __future__ import absolute_import, unicode_literals
from django.utils import six
from .common import * # noqa
# SECRET CONFIGURATION
......
......@@ -6,7 +6,6 @@ from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views import defaults as default_views
from django.views.generic import TemplateView
urlpatterns = [
# Django Admin, use {% url 'admin:index' %}
......
......@@ -35,7 +35,7 @@ def get_actor_data(actor_url):
response.raise_for_status()
try:
return response.json()
except:
except Exception:
raise ValueError("Invalid actor payload: {}".format(response.text))
......
......@@ -334,7 +334,7 @@ class FollowSerializer(serializers.Serializer):
return models.Follow.objects.get_or_create(
actor=self.validated_data["actor"],
target=self.validated_data["object"],
**kwargs,
**kwargs, # noqa
)[0]
def to_representation(self, instance):
......@@ -345,7 +345,6 @@ class FollowSerializer(serializers.Serializer):
"object": instance.target.url,
"type": "Follow",
}
return ret
class APIFollowSerializer(serializers.ModelSerializer):
......
from django import forms
from django.core import paginator
from django.db import transaction
from django.http import HttpResponse
from django.http import HttpResponse, Http404
from django.urls import reverse
from rest_framework import mixins, response, viewsets
from rest_framework.decorators import detail_route, list_route
......@@ -144,7 +144,7 @@ class MusicFilesViewSet(FederationMixin, viewsets.GenericViewSet):
else:
try:
page_number = int(page)
except:
except Exception:
return response.Response({"page": ["Invalid page number"]}, status=400)
p = paginator.Paginator(
qs, preferences.get("federation__collection_page_size")
......
......@@ -259,13 +259,13 @@ class Work(APIModelMixin):
import_hooks = [import_lyrics, link_recordings]
def fetch_lyrics(self):
l = self.lyrics.first()
if l:
return l
lyric = self.lyrics.first()
if lyric:
return lyric
data = self.api.get(self.mbid, includes=["url-rels"])["work"]
l = import_lyrics(self, {}, data)
lyric = import_lyrics(self, {}, data)
return l
return lyric
class Lyrics(models.Model):
......@@ -606,7 +606,7 @@ def update_request_status(sender, instance, created, **kwargs):
if not instance.import_request:
return
if not created and not "status" in update_fields:
if not created and "status" not in update_fields:
return
r_status = instance.import_request.status
......
......@@ -256,7 +256,7 @@ def import_job_run(self, import_job, replace=False, use_acoustid=False):
if not settings.DEBUG:
try:
self.retry(exc=exc, countdown=30, max_retries=3)
except:
except Exception:
mark_errored(exc)
raise
mark_errored(exc)
......
......@@ -4,7 +4,7 @@ import urllib
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db import models, transaction
from django.db import transaction
from django.db.models import Count
from django.db.models.functions import Length
from django.utils import timezone
......
import os
import acoustid
from django.core.files import File
from django.db import transaction
from funkwhale_api.music import metadata, models
from funkwhale_api.providers.acoustid import get_acoustid_client
from funkwhale_api.taskapp import celery
@transaction.atomic
......@@ -49,33 +43,3 @@ def import_track_data_from_path(path):
defaults={"title": data.get("title"), "position": position},
)[0]
return track
def import_metadata_with_musicbrainz(path):
pass
@celery.app.task(name="audiofile.from_path")
def from_path(path):
acoustid_track_id = None
try:
client = get_acoustid_client()
result = client.get_best_match(path)
acoustid_track_id = result["id"]
except acoustid.WebServiceError:
track = import_track_data_from_path(path)
except (TypeError, KeyError):
track = import_metadata_without_musicbrainz(path)
else:
track, created = models.Track.get_or_create_from_api(
mbid=result["recordings"][0]["id"]
)
if track.files.count() > 0:
raise ValueError("File already exists for track {}".format(track.pk))
track_file = models.TrackFile(track=track, acoustid_track_id=acoustid_track_id)
track_file.audio_file.save(os.path.basename(path), File(open(path, "rb")))
track_file.save()
return track_file
......@@ -24,7 +24,7 @@ class RadioSessionFactory(factory.django.DjangoModelFactory):
@registry.register(name="radios.CustomRadioSession")
class RadioSessionFactory(factory.django.DjangoModelFactory):
class CustomRadioSessionFactory(factory.django.DjangoModelFactory):
user = factory.SubFactory(UserFactory)
radio_type = "custom"
custom_radio = factory.SubFactory(
......
......@@ -175,7 +175,6 @@ class TagFilter(RadioFilter):
"type": "list",
"subtype": "string",
"autocomplete": reverse_lazy("api:v1:tags-list"),
"autocomplete_qs": "",
"autocomplete_fields": {
"remoteValues": "results",
"name": "name",
......
......@@ -456,6 +456,6 @@ class SubsonicViewSet(viewsets.GenericViewSet):
{"error": {"code": 0, "message": "Invalid payload"}}
)
if serializer.validated_data["submission"]:
l = serializer.save()
record.send(l)
listening = serializer.save()
record.send(listening)
return response.Response({})
......@@ -90,7 +90,8 @@ class User(AbstractUser):
perms[p] = v
return perms
def has_permissions(self, *perms, operator="and"):
def has_permissions(self, *perms, **kwargs):
operator = kwargs.pop("operator", "and")
if operator not in ["and", "or"]:
raise ValueError("Invalid operator {}".format(operator))
permissions = self.get_permissions()
......
[flake8]
max-line-length = 120
exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules
exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules,tests/data,tests/music/conftest.py
ignore = F405,W503,E203
[isort]
skip_glob = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules
......
search = {}
......@@ -13,7 +11,7 @@ search["8 bit adventure"] = {
"etag": '"gMxXHe-zinKdE9lTnzKu8vjcmDI/GxK-wHBWUYfrJsd1dijBPTufrVE"',
"snippet": {
"liveBroadcastContent": "none",
"description": "Make sure to apply adhesive evenly before use. GET IT HERE: http://adhesivewombat.bandcamp.com/album/marsupial-madness Facebook: ...",
"description": "Description",
"channelId": "UCps63j3krzAG4OyXeEyuhFw",
"title": "AdhesiveWombat - 8 Bit Adventure",
"channelTitle": "AdhesiveWombat",
......
......@@ -617,7 +617,7 @@ def lyricswiki_content():
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="generator" content="MediaWiki 1.19.24" />
<meta name="keywords" content="Chop Suey! lyrics,System Of A Down Chop Suey! lyrics,Chop Suey! by System Of A Down lyrics,lyrics,LyricWiki,LyricWikia,lyricwiki,System Of A Down:Chop Suey!,System Of A Down,System Of A Down:Toxicity (2001),Enter Shikari,Enter Shikari:Chop Suey!,&quot;Weird Al&quot; Yankovic,&quot;Weird Al&quot; Yankovic:Angry White Boy Polka,Renard,Renard:Physicality,System Of A Down:Chop Suey!/pt,Daron Malakian" />
<meta name="description" content="Chop Suey! This song is by System of a Down and appears on the album Toxicity (2001)." />
......
......@@ -4,7 +4,7 @@ from funkwhale_api.music import lyrics as lyrics_utils
from funkwhale_api.music import models, tasks
def test_works_import_lyrics_if_any(lyricswiki_content, mocker, factories):
def test_lyrics_tasks(lyricswiki_content, mocker, factories):
mocker.patch(
"funkwhale_api.music.lyrics._get_html", return_value=lyricswiki_content
)
......@@ -14,7 +14,7 @@ def test_works_import_lyrics_if_any(lyricswiki_content, mocker, factories):
tasks.fetch_content(lyrics_id=lyrics.pk)
lyrics.refresh_from_db()
self.assertIn("Grab a brush and put on a little makeup", lyrics.content)
assert "Grab a brush and put on a little makeup" in lyrics.content
def test_clean_content():
......@@ -32,10 +32,10 @@ def test_markdown_rendering(factories):
content = """Hello
Is it me you're looking for?"""
l = factories["music.Lyrics"](content=content)
lyrics = factories["music.Lyrics"](content=content)
expected = "<p>Hello<br />\nIs it me you're looking for?</p>"
assert expected == l.content_rendered
assert expected == lyrics.content_rendered
def test_works_import_lyrics_if_any(
......
......@@ -63,7 +63,6 @@ def test_track_file_serializer(factories, to_api_date):
"path": tf.path,
"source": tf.source,
"filename": tf.filename,
"mimetype": tf.mimetype,
"track": tf.track.pk,
"duration": tf.duration,
"mimetype": tf.mimetype,
......
from funkwhale_api.music import models
......@@ -36,7 +35,7 @@ def test_can_get_work_from_recording(factories, mocker, works, tracks):
)
mbid = "e2ecabc4-1b9d-30b2-8f30-3596ec423dc5"
assert recording.work == None
assert recording.work is None
work = recording.get_work()
......
def test_can_bind_import_batch_to_request(factories):
request = factories["requests.ImportRequest"]()
......
......@@ -24,7 +24,7 @@ def test_xml_renderer_dict_to_xml():
def test_xml_renderer():
payload = {"hello": "world"}
expected = b'<?xml version="1.0" encoding="UTF-8"?>\n<subsonic-response hello="world" status="ok" version="1.16.0" xmlns="http://subsonic.org/restapi" />'
expected = b'<?xml version="1.0" encoding="UTF-8"?>\n<subsonic-response hello="world" status="ok" version="1.16.0" xmlns="http://subsonic.org/restapi" />' # noqa
renderer = renderers.SubsonicXMLRenderer()
rendered = renderer.render(payload)
......
......@@ -33,7 +33,7 @@ def test_can_get_search_results_from_funkwhale(preferences, mocker, api_client,
"id": "0HxZn6CzOIo",
"url": "https://www.youtube.com/watch?v=0HxZn6CzOIo",
"type": "youtube#video",
"description": "Make sure to apply adhesive evenly before use. GET IT HERE: http://adhesivewombat.bandcamp.com/album/marsupial-madness Facebook: ...",
"description": "Description",
"channelId": "UCps63j3krzAG4OyXeEyuhFw",
"title": "AdhesiveWombat - 8 Bit Adventure",
"channelTitle": "AdhesiveWombat",
......@@ -82,7 +82,7 @@ def test_can_send_multiple_queries_at_once_from_funwkhale(
"id": "0HxZn6CzOIo",
"url": "https://www.youtube.com/watch?v=0HxZn6CzOIo",
"type": "youtube#video",
"description": "Make sure to apply adhesive evenly before use. GET IT HERE: http://adhesivewombat.bandcamp.com/album/marsupial-madness Facebook: ...",
"description": "Description",
"channelId": "UCps63j3krzAG4OyXeEyuhFw",
"title": "AdhesiveWombat - 8 Bit Adventure",
"channelTitle": "AdhesiveWombat",
......
......@@ -87,8 +87,6 @@ def test_has_user_permission_logged_in_multiple_or(
request = api_request.get("/")
setattr(request, "user", user)
result = permission.has_permission(request, view)
assert (
result
== user.has_permissions("federation", "library", operator="or")
== expected
)
has_permission_result = user.has_permissions("federation", "library", operator="or")
assert result == has_permission_result == expected
......@@ -161,7 +161,7 @@ def test_user_can_request_new_subsonic_token(logged_in_api_client):
assert response.data == {"subsonic_api_token": user.subsonic_api_token}
def test_user_can_get_new_subsonic_token(logged_in_api_client):
def test_user_can_get_subsonic_token(logged_in_api_client):
user = logged_in_api_client.user
user.subsonic_api_token = "test"
user.save()
......@@ -176,24 +176,6 @@ def test_user_can_get_new_subsonic_token(logged_in_api_client):
assert response.data == {"subsonic_api_token": "test"}
def test_user_can_request_new_subsonic_token(logged_in_api_client):
user = logged_in_api_client.user
user.subsonic_api_token = "test"
user.save()
url = reverse(
"api:v1:users:users-subsonic-token", kwargs={"username": user.username}
)
response = logged_in_api_client.post(url)
assert response.status_code == 200
user.refresh_from_db()
assert user.subsonic_api_token != "test"
assert user.subsonic_api_token is not None
assert response.data == {"subsonic_api_token": user.subsonic_api_token}
def test_user_can_delete_subsonic_token(logged_in_api_client):
user = logged_in_api_client.user
user.subsonic_api_token = "test"
......
Markdown is supported
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