Blacked the code

This commit is contained in:
Eliot Berriot 2018-06-09 15:36:16 +02:00
commit 62ca3bd736
No known key found for this signature in database
GPG key ID: DD6965E2476E5C27
279 changed files with 8890 additions and 9556 deletions

View file

@ -5,4 +5,4 @@ class InstanceActivityConsumer(JsonAuthConsumer):
groups = ["instance_activity"]
def event_send(self, message):
self.send_json(message['data'])
self.send_json(message["data"])

View file

@ -3,91 +3,83 @@ from django.forms import widgets
from dynamic_preferences import types
from dynamic_preferences.registries import global_preferences_registry
raven = types.Section('raven')
instance = types.Section('instance')
raven = types.Section("raven")
instance = types.Section("instance")
@global_preferences_registry.register
class InstanceName(types.StringPreference):
show_in_api = True
section = instance
name = 'name'
default = ''
verbose_name = 'Public name'
help_text = 'The public name of your instance, displayed in the about page.'
field_kwargs = {
'required': False,
}
name = "name"
default = ""
verbose_name = "Public name"
help_text = "The public name of your instance, displayed in the about page."
field_kwargs = {"required": False}
@global_preferences_registry.register
class InstanceShortDescription(types.StringPreference):
show_in_api = True
section = instance
name = 'short_description'
default = ''
verbose_name = 'Short description'
help_text = 'Instance succinct description, displayed in the about page.'
field_kwargs = {
'required': False,
}
name = "short_description"
default = ""
verbose_name = "Short description"
help_text = "Instance succinct description, displayed in the about page."
field_kwargs = {"required": False}
@global_preferences_registry.register
class InstanceLongDescription(types.StringPreference):
show_in_api = True
section = instance
name = 'long_description'
verbose_name = 'Long description'
default = ''
help_text = 'Instance long description, displayed in the about page (markdown allowed).'
name = "long_description"
verbose_name = "Long description"
default = ""
help_text = (
"Instance long description, displayed in the about page (markdown allowed)."
)
widget = widgets.Textarea
field_kwargs = {
'required': False,
}
field_kwargs = {"required": False}
@global_preferences_registry.register
class RavenDSN(types.StringPreference):
show_in_api = True
section = raven
name = 'front_dsn'
default = 'https://9e0562d46b09442bb8f6844e50cbca2b@sentry.eliotberriot.com/4'
verbose_name = 'Raven DSN key (front-end)'
name = "front_dsn"
default = "https://9e0562d46b09442bb8f6844e50cbca2b@sentry.eliotberriot.com/4"
verbose_name = "Raven DSN key (front-end)"
help_text = (
'A Raven DSN key used to report front-ent errors to '
'a sentry instance. Keeping the default one will report errors to '
'Funkwhale developers.'
"A Raven DSN key used to report front-ent errors to "
"a sentry instance. Keeping the default one will report errors to "
"Funkwhale developers."
)
field_kwargs = {
'required': False,
}
field_kwargs = {"required": False}
@global_preferences_registry.register
class RavenEnabled(types.BooleanPreference):
show_in_api = True
section = raven
name = 'front_enabled'
name = "front_enabled"
default = False
verbose_name = (
'Report front-end errors with Raven'
)
verbose_name = "Report front-end errors with Raven"
@global_preferences_registry.register
class InstanceNodeinfoEnabled(types.BooleanPreference):
show_in_api = False
section = instance
name = 'nodeinfo_enabled'
name = "nodeinfo_enabled"
default = True
verbose_name = 'Enable nodeinfo endpoint'
verbose_name = "Enable nodeinfo endpoint"
help_text = (
'This endpoint is needed for your about page to work. '
'It\'s also helpful for the various monitoring '
'tools that map and analyzize the fediverse, '
'but you can disable it completely if needed.'
"This endpoint is needed for your about page to work. "
"It's also helpful for the various monitoring "
"tools that map and analyzize the fediverse, "
"but you can disable it completely if needed."
)
@ -95,13 +87,13 @@ class InstanceNodeinfoEnabled(types.BooleanPreference):
class InstanceNodeinfoPrivate(types.BooleanPreference):
show_in_api = False
section = instance
name = 'nodeinfo_private'
name = "nodeinfo_private"
default = False
verbose_name = 'Private mode in nodeinfo'
verbose_name = "Private mode in nodeinfo"
help_text = (
'Indicate in the nodeinfo endpoint that you do not want your instance '
'to be tracked by third-party services. '
'There is no guarantee these tools will honor this setting though.'
"Indicate in the nodeinfo endpoint that you do not want your instance "
"to be tracked by third-party services. "
"There is no guarantee these tools will honor this setting though."
)
@ -109,10 +101,10 @@ class InstanceNodeinfoPrivate(types.BooleanPreference):
class InstanceNodeinfoStatsEnabled(types.BooleanPreference):
show_in_api = False
section = instance
name = 'nodeinfo_stats_enabled'
name = "nodeinfo_stats_enabled"
default = True
verbose_name = 'Enable usage and library stats in nodeinfo endpoint'
verbose_name = "Enable usage and library stats in nodeinfo endpoint"
help_text = (
'Disable this if you don\'t want to share usage and library statistics '
'in the nodeinfo endpoint but don\'t want to disable it completely.'
"Disable this if you don't want to share usage and library statistics "
"in the nodeinfo endpoint but don't want to disable it completely."
)

View file

@ -6,70 +6,47 @@ from funkwhale_api.common import preferences
from . import stats
store = memoize.djangocache.Cache('default')
memo = memoize.Memoizer(store, namespace='instance:stats')
store = memoize.djangocache.Cache("default")
memo = memoize.Memoizer(store, namespace="instance:stats")
def get():
share_stats = preferences.get('instance__nodeinfo_stats_enabled')
private = preferences.get('instance__nodeinfo_private')
share_stats = preferences.get("instance__nodeinfo_stats_enabled")
private = preferences.get("instance__nodeinfo_private")
data = {
'version': '2.0',
'software': {
'name': 'funkwhale',
'version': funkwhale_api.__version__
},
'protocols': ['activitypub'],
'services': {
'inbound': [],
'outbound': []
},
'openRegistrations': preferences.get('users__registration_enabled'),
'usage': {
'users': {
'total': 0,
}
},
'metadata': {
'private': preferences.get('instance__nodeinfo_private'),
'shortDescription': preferences.get('instance__short_description'),
'longDescription': preferences.get('instance__long_description'),
'nodeName': preferences.get('instance__name'),
'library': {
'federationEnabled': preferences.get('federation__enabled'),
'federationNeedsApproval': preferences.get('federation__music_needs_approval'),
'anonymousCanListen': preferences.get('common__api_authentication_required'),
"version": "2.0",
"software": {"name": "funkwhale", "version": funkwhale_api.__version__},
"protocols": ["activitypub"],
"services": {"inbound": [], "outbound": []},
"openRegistrations": preferences.get("users__registration_enabled"),
"usage": {"users": {"total": 0}},
"metadata": {
"private": preferences.get("instance__nodeinfo_private"),
"shortDescription": preferences.get("instance__short_description"),
"longDescription": preferences.get("instance__long_description"),
"nodeName": preferences.get("instance__name"),
"library": {
"federationEnabled": preferences.get("federation__enabled"),
"federationNeedsApproval": preferences.get(
"federation__music_needs_approval"
),
"anonymousCanListen": preferences.get(
"common__api_authentication_required"
),
},
}
},
}
if share_stats:
getter = memo(
lambda: stats.get(),
max_age=600
)
getter = memo(lambda: stats.get(), max_age=600)
statistics = getter()
data['usage']['users']['total'] = statistics['users']
data['metadata']['library']['tracks'] = {
'total': statistics['tracks'],
}
data['metadata']['library']['artists'] = {
'total': statistics['artists'],
}
data['metadata']['library']['albums'] = {
'total': statistics['albums'],
}
data['metadata']['library']['music'] = {
'hours': statistics['music_duration']
}
data["usage"]["users"]["total"] = statistics["users"]
data["metadata"]["library"]["tracks"] = {"total": statistics["tracks"]}
data["metadata"]["library"]["artists"] = {"total": statistics["artists"]}
data["metadata"]["library"]["albums"] = {"total": statistics["albums"]}
data["metadata"]["library"]["music"] = {"hours": statistics["music_duration"]}
data['metadata']['usage'] = {
'favorites': {
'tracks': {
'total': statistics['track_favorites'],
}
},
'listenings': {
'total': statistics['listenings']
}
data["metadata"]["usage"] = {
"favorites": {"tracks": {"total": statistics["track_favorites"]}},
"listenings": {"total": statistics["listenings"]},
}
return data

View file

@ -8,13 +8,13 @@ from funkwhale_api.users.models import User
def get():
return {
'users': get_users(),
'tracks': get_tracks(),
'albums': get_albums(),
'artists': get_artists(),
'track_favorites': get_track_favorites(),
'listenings': get_listenings(),
'music_duration': get_music_duration(),
"users": get_users(),
"tracks": get_tracks(),
"albums": get_albums(),
"artists": get_artists(),
"track_favorites": get_track_favorites(),
"listenings": get_listenings(),
"music_duration": get_music_duration(),
}
@ -43,9 +43,7 @@ def get_artists():
def get_music_duration():
seconds = models.TrackFile.objects.aggregate(
d=Sum('duration'),
)['d']
seconds = models.TrackFile.objects.aggregate(d=Sum("duration"))["d"]
if seconds:
return seconds / 3600
return 0

View file

@ -2,10 +2,11 @@ from django.conf.urls import url
from rest_framework import routers
from . import views
admin_router = routers.SimpleRouter()
admin_router.register(r'admin/settings', views.AdminSettings, 'admin-settings')
admin_router.register(r"admin/settings", views.AdminSettings, "admin-settings")
urlpatterns = [
url(r'^nodeinfo/2.0/$', views.NodeInfo.as_view(), name='nodeinfo-2.0'),
url(r'^settings/$', views.InstanceSettings.as_view(), name='settings'),
url(r"^nodeinfo/2.0/$", views.NodeInfo.as_view(), name="nodeinfo-2.0"),
url(r"^settings/$", views.InstanceSettings.as_view(), name="settings"),
] + admin_router.urls

View file

@ -12,15 +12,14 @@ from . import nodeinfo
from . import stats
NODEINFO_2_CONTENT_TYPE = (
'application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8' # noqa
)
NODEINFO_2_CONTENT_TYPE = "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8" # noqa
class AdminSettings(preferences_viewsets.GlobalPreferencesViewSet):
pagination_class = None
permission_classes = (HasUserPermission,)
required_permissions = ['settings']
required_permissions = ["settings"]
class InstanceSettings(views.APIView):
permission_classes = []
@ -29,16 +28,11 @@ class InstanceSettings(views.APIView):
def get(self, request, *args, **kwargs):
manager = global_preferences_registry.manager()
manager.all()
all_preferences = manager.model.objects.all().order_by(
'section', 'name'
)
all_preferences = manager.model.objects.all().order_by("section", "name")
api_preferences = [
p
for p in all_preferences
if getattr(p.preference, 'show_in_api', False)
p for p in all_preferences if getattr(p.preference, "show_in_api", False)
]
data = serializers.GlobalPreferenceSerializer(
api_preferences, many=True).data
data = serializers.GlobalPreferenceSerializer(api_preferences, many=True).data
return Response(data, status=200)
@ -47,8 +41,7 @@ class NodeInfo(views.APIView):
authentication_classes = []
def get(self, request, *args, **kwargs):
if not preferences.get('instance__nodeinfo_enabled'):
if not preferences.get("instance__nodeinfo_enabled"):
return Response(status=404)
data = nodeinfo.get()
return Response(
data, status=200, content_type=NODEINFO_2_CONTENT_TYPE)
return Response(data, status=200, content_type=NODEINFO_2_CONTENT_TYPE)