Allow editing filters

This commit is contained in:
Jason McBrayer 2019-02-15 17:42:36 -05:00
parent 73459106e8
commit 0007547a7c
4 changed files with 132 additions and 5 deletions

View File

@ -0,0 +1,79 @@
{% extends "base.html" %}
{% load widget_tweaks %}
{% block content %}
<div class="container">
<h1 class="title">Edit Filter</h1>
<form method="post" id="edit-filter-form"
action="{% url "edit_filter" filter.id %}">
{% csrf_token %}
<div>
{{ form.non_field_errors }}
</div>
<div class="field">
<label class="label"> {{ form.phrase.label }}</label>
<div class="control">
{% render_field form.phrase class+="input" %}
</div>
</div>
<div class="columns">
<div class="column field">
<label class="label checkbox">
{{ form.context_home.label }}
{% render_field form.context_home %}
</label>
</div>
<div class="column field">
<label class="label checkbox">
{{ form.context_public.label }}
{% render_field form.context_public %}
</label>
</div>
<div class="column field">
<label class="label checkbox">
{{ form.context_notes.label }}
{% render_field form.context_notes %}
</label>
</div>
<div class="column field">
<label class="label checkbox">
{{ form.context_thread.label }}
{% render_field form.context_thread %}
</label>
</div>
</div>
<div class="field">
<label class="label checkbox">
{{ form.whole_word.label }}
{% render_field form.whole_word %}
</label>
</div>
<div class="field">
<label class="label" for="expires_in">{{ form.expires_in.label }}</label>
<div class="control has-icons-left">
<div class="select">
{% render_field form.expires_in class+="select" %}
<span class="icon is-small is-left">
<span class="fa fa-clock-o"></span>
</span>
</div>
</div>
</div>
<div class="field">
<input type="submit" name="submit" value="Save"
class="button is-primary">
</div>
</form>
</div>
{% endblock %}

View File

@ -23,7 +23,12 @@
{{ context }} {{ context }}
{% endfor %} {% endfor %}
</td> </td>
<td>Edit filter</td> <td>
<a href="{% url "edit_filter" filter.id %}">
<span class="fa fa-pencil"></span>
Edit filter
</a>
</td>
<td> <td>
<a href="{% url "delete_filter" filter.id %}" <a href="{% url "delete_filter" filter.id %}"
ic-delete-from="{% url "delete_filter" filter.id %}", ic-delete-from="{% url "delete_filter" filter.id %}",

View File

@ -63,5 +63,6 @@ urlpatterns = [
path('filters/list', views.list_filters, name='list_filters'), path('filters/list', views.list_filters, name='list_filters'),
path('filters/create', views.create_filter, name='create_filter'), path('filters/create', views.create_filter, name='create_filter'),
path('filters/delete/<id>', views.delete_filter, name='delete_filter'), path('filters/delete/<id>', views.delete_filter, name='delete_filter'),
path('filters/edit/<id>', views.edit_filter, name='edit_filter'),
path('', views.home, name=''), path('', views.home, name=''),
] ]

View File

@ -975,10 +975,7 @@ def list_filters(request):
@br_login_required @br_login_required
def create_filter(request): def create_filter(request):
try: account, mastodon = get_usercontext(request)
account, mastodon = get_usercontext(request)
except NotLoggedInException:
return redirect(about)
if request.method == 'POST': if request.method == 'POST':
form = FilterForm(request.POST) form = FilterForm(request.POST)
if form.is_valid(): if form.is_valid():
@ -1028,3 +1025,48 @@ def delete_filter(request, id):
"own_acct": request.session["user"], "own_acct": request.session["user"],
"confirm_page": True, "confirm_page": True,
"preferences": account.preferences}) "preferences": account.preferences})
@br_login_required
def edit_filter(request, id):
account, mastodon = get_usercontext(request)
filter = mastodon.filter(id)
contexts = []
if request.method == 'POST':
form = FilterForm(request.POST)
if form.is_valid():
if form.cleaned_data['context_home']:
contexts.append('home')
if form.cleaned_data['context_public']:
contexts.append('public')
if form.cleaned_data['context_notes']:
contexts.append('notifications')
if form.cleaned_data['context_thread']:
contexts.append('thread')
expires = form.cleaned_data['expires_in']
if expires == "":
expires = None
mastodon.filter_update(id, form.cleaned_data['phrase'],
contexts,
whole_word=form.cleaned_data['whole_word'],
expires_in=expires)
return redirect(list_filters)
else:
return render(request, 'filters/edit.html',
{ 'form': form,
'account': account,
'filter': filter,
'preferences': account.preferences})
else:
contexts = []
form = FilterForm({'phrase': filter.phrase,
'context_home': "home" in filter.context,
'context_public': "public" in filter.context,
'context_notes': "notifications" in filter.context,
'context_thread': "thread" in filter.context,
'whole_word': filter.whole_word})
return render(request, 'filters/edit.html',
{ 'form': form,
'account': account,
'filter': filter,
'preferences': account.preferences})