Fix errors introduced in same_user, apply standard formatting

This commit is contained in:
Jason McBrayer 2020-07-08 12:58:38 -04:00
parent b1a2c7f57d
commit fd5e4874c8
5 changed files with 522 additions and 40 deletions

File diff suppressed because one or more lines are too long

View File

@ -6,13 +6,15 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('brutaldon', '0024_auto_20200601_0945'), ("brutaldon", "0024_auto_20200601_0945"),
] ]
operations = [ operations = [
migrations.AddField( migrations.AddField(
model_name='preference', model_name="preference",
name='preview_sensitive', name="preview_sensitive",
field=models.BooleanField(default=False, help_text='Show preview for media marked as "sensitive"'), field=models.BooleanField(
default=False, help_text='Show preview for media marked as "sensitive"'
),
), ),
] ]

View File

@ -29,19 +29,26 @@ class Theme(models.Model):
def __str__(self): def __str__(self):
return self.name return self.name
from django.db.models.fields.related_descriptors import ForeignKeyDeferredAttribute from django.db.models.fields.related_descriptors import ForeignKeyDeferredAttribute
def set_fields(klass): def set_fields(klass):
fields = [] fields = []
for n in dir(klass): for n in dir(klass):
assert n != "_fields" assert n != "_fields"
v = getattr(klass, n) v = getattr(klass, n)
if not hasattr(v, 'field'): continue if not hasattr(v, "field"):
if not isinstance(v.field, models.Field): continue continue
if isinstance(v, ForeignKeyDeferredAttribute): continue if not isinstance(v.field, models.Field):
continue
if isinstance(v, ForeignKeyDeferredAttribute):
continue
fields.append(n) fields.append(n)
setattr(klass, '_fields', fields) setattr(klass, "_fields", fields)
return klass return klass
@set_fields @set_fields
class Preference(models.Model): class Preference(models.Model):
theme = models.ForeignKey(Theme, models.CASCADE, null=False, default=1) theme = models.ForeignKey(Theme, models.CASCADE, null=False, default=1)
@ -51,9 +58,8 @@ class Preference(models.Model):
max_length=80, blank=True, null=True, choices=timezones, default="UTC" max_length=80, blank=True, null=True, choices=timezones, default="UTC"
) )
preview_sensitive = models.BooleanField( preview_sensitive = models.BooleanField(
default=False, default=False, help_text=_('Show preview for media marked as "sensitive"')
help_text=_( )
'Show preview for media marked as "sensitive"'))
no_javascript = models.BooleanField( no_javascript = models.BooleanField(
default=False, default=False,

View File

@ -45,11 +45,11 @@ urlpatterns = [
path("tags/<tag>", views.tag, name="tag"), path("tags/<tag>", views.tag, name="tag"),
path("user/", views.home, name="user_bad"), path("user/", views.home, name="user_bad"),
path("user/<username>", views.user, name="user"), path("user/<username>", views.user, name="user"),
# next/prev are integers, but pleroma uses 128 bit integers # next/prev are integers, but pleroma uses 128 bit integers
# ...encoded in Base62. # ...encoded in Base62.
# aka a "flake_id" # aka a "flake_id"
# from baseconv import base62, but we don't need to decode it # from baseconv import base62, but we don't need to decode it
# just pass it along back to pleroma but it is NOT an <int:> # just pass it along back to pleroma but it is NOT an <int:>
path("user/<username>/next/<next>", views.user, name="user_next"), path("user/<username>/next/<next>", views.user, name="user_next"),
path("user/<username>/prev/<prev>", views.user, name="user_prev"), path("user/<username>/prev/<prev>", views.user, name="user_prev"),
path("toot/<mention>", views.toot, name="toot"), path("toot/<mention>", views.toot, name="toot"),

View File

@ -91,7 +91,7 @@ def get_usercontext(request, feature_set="mainline"):
api_base_url=client.api_base_id, api_base_url=client.api_base_id,
session=get_session(client.api_base_id), session=get_session(client.api_base_id),
ratelimit_method="throw", ratelimit_method="throw",
feature_set=feature_set feature_set=feature_set,
) )
return user, mastodon return user, mastodon
else: else:
@ -144,6 +144,7 @@ def br_login_required(function=None, home_url=None, redirect_field_name=None):
if not url: if not url:
url = "/" url = "/"
return HttpResponseRedirect(url) return HttpResponseRedirect(url)
if not is_logged_in(request): if not is_logged_in(request):
return not_logged_in() return not_logged_in()
else: else:
@ -616,7 +617,8 @@ def note(request, next=None, prev=None):
return redirect(about) return redirect(about)
try: try:
last_seen = mastodon.notifications(limit=1)[0] last_seen = mastodon.notifications(limit=1)[0]
except IndexError: pass except IndexError:
pass
else: else:
account.note_seen = last_seen.id account.note_seen = last_seen.id
account.save() account.save()
@ -652,6 +654,7 @@ def note(request, next=None, prev=None):
return str(note.status.id) + note.type return str(note.status.id) + note.type
except: except:
return str(note.id) + note.type return str(note.id) + note.type
def group_sort_key(group): def group_sort_key(group):
return max([k.id for k in group]) return max([k.id for k in group])
@ -716,13 +719,20 @@ def thread(request, id):
}, },
) )
def same_username(account, acct, username): def same_username(account, acct, username):
if acct == username: return True if acct == username:
user, host = username.split("@", 1) return True
myhost = account.username.split("@",1)[1] try:
if acct == user and host == myhost: return True user, host = username.split("@", 1)
except ValueError:
user, host = username, ""
myhost = account.username.split("@", 1)[1]
if acct == user and host == myhost:
return True
return False return False
@br_login_required @br_login_required
def user(request, username, prev=None, next=None): def user(request, username, prev=None, next=None):
try: try:
@ -735,13 +745,14 @@ def user(request, username, prev=None, next=None):
# but until then, we might have to fallback to a regular search, # but until then, we might have to fallback to a regular search,
# if the account search fails to return results. # if the account search fails to return results.
for dict in mastodon.account_search(username): for dict in mastodon.account_search(username):
if not same_username(account, dict.acct, username): continue if not same_username(account, dict.acct, username):
continue
user_dict = dict user_dict = dict
break break
else: else:
for dict in mastodon.search(username, for dict in mastodon.search(username, result_type="accounts").accounts:
result_type="accounts").accounts: if not same_username(account, dict.acct, username):
if not same_username(account, dict.acct, username): continue continue
user_dict = dict user_dict = dict
break break
else: else:
@ -790,8 +801,7 @@ def settings(request):
if form.is_valid(): if form.is_valid():
for field in account.preferences._fields: for field in account.preferences._fields:
if field in form.cleaned_data: if field in form.cleaned_data:
setattr(account.preferences, field, setattr(account.preferences, field, form.cleaned_data[field])
form.cleaned_data[field])
request.session["timezone"] = account.preferences.timezone request.session["timezone"] = account.preferences.timezone
account.preferences.save() account.preferences.save()
account.save() account.save()
@ -818,17 +828,17 @@ def settings(request):
{"form": form, "account": account, "preferences": account.preferences}, {"form": form, "account": account, "preferences": account.preferences},
) )
def status_post(account, request, mastodon, **kw): def status_post(account, request, mastodon, **kw):
while True: while True:
try: try:
mastodon.status_post(**kw) mastodon.status_post(**kw)
except MastodonIllegalArgumentError as e: except MastodonIllegalArgumentError as e:
if not 'is only available with feature set' in e.args[0]: if not "is only available with feature set" in e.args[0]:
raise raise
feature_set = e.args[0].rsplit(" ",1)[-1] feature_set = e.args[0].rsplit(" ", 1)[-1]
account, mastodon = get_usercontext(request, account, mastodon = get_usercontext(request, feature_set=feature_set)
feature_set=feature_set)
continue continue
except TypeError: except TypeError:
@ -840,6 +850,7 @@ def status_post(account, request, mastodon, **kw):
break break
return account, mastodon return account, mastodon
@never_cache @never_cache
@br_login_required @br_login_required
def toot(request, mention=None): def toot(request, mention=None):
@ -902,12 +913,15 @@ def toot(request, mention=None):
].source.privacy ].source.privacy
try: try:
status_post( status_post(
account, request, mastodon, account,
request,
mastodon,
status=form.cleaned_data["status"], status=form.cleaned_data["status"],
visibility=form.cleaned_data["visibility"], visibility=form.cleaned_data["visibility"],
spoiler_text=form.cleaned_data["spoiler_text"], spoiler_text=form.cleaned_data["spoiler_text"],
media_ids=media_objects, media_ids=media_objects,
content_type="text/markdown") content_type="text/markdown",
)
except MastodonAPIError as error: except MastodonAPIError as error:
form.add_error( form.add_error(
"", "",
@ -1002,7 +1016,9 @@ def redraft(request, id):
].source.privacy ].source.privacy
try: try:
status_post( status_post(
account, request, mastodon, account,
request,
mastodon,
status=form.cleaned_data["status"], status=form.cleaned_data["status"],
visibility=form.cleaned_data["visibility"], visibility=form.cleaned_data["visibility"],
spoiler_text=form.cleaned_data["spoiler_text"], spoiler_text=form.cleaned_data["spoiler_text"],
@ -1127,7 +1143,9 @@ def reply(request, id):
) )
try: try:
status_post( status_post(
account, request, mastodon, account,
request,
mastodon,
status=form.cleaned_data["status"], status=form.cleaned_data["status"],
visibility=form.cleaned_data["visibility"], visibility=form.cleaned_data["visibility"],
spoiler_text=form.cleaned_data["spoiler_text"], spoiler_text=form.cleaned_data["spoiler_text"],