Skip to content

Commit

Permalink
Updated Group Views (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoegl committed Aug 18, 2017
1 parent 98111fd commit d2e5904
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 104 deletions.
29 changes: 15 additions & 14 deletions mygpo/administration/templates/admin/merge-grouping.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1>{% trans "Merge Podcasts and Episodes" %}</h1>
{% trans "Episodes that have the same number will be merged. Please verify all your changes by clicking on 'Renew Groups' before starting the Merge." %}
</div>

<form method="post" action="{% url "admin-merge-process" %}">
<form method="post" action="{% url "admin-merge-process" task.pk %}">
{% csrf_token %}

{% for podcast in podcasts %}
Expand All @@ -39,18 +39,21 @@ <h1>{% trans "Merge Podcasts and Episodes" %}</h1>
{% endfor %}
</tr>

{% for n, episodes in groups %}
{% for line in groups %}
<tr>
<th>{{ n }}</th>
<th>{{ forloop.counter }}</th>

{% for podcast in podcasts %}
{% for episode in line %}
<td>
{% for episode in episodes %}
{% if episode.podcast.get_id == podcast.get_id %}
<input type="text" name="episode_{{ episode.get_id }}" value="{{ n }}" size="2"/>
{% episode_link episode podcast %}<br />
{% endif %}
{% endfor %}
{% if episode %}
<input
type="text"
name="episode_{{ episode.get_id }}"
value="{{ forloop.parentloop.counter }}"
size="2"
/>
{% episode_link episode episode.podcast %}<br />
{% endif %}
</td>
{% endfor %}

Expand All @@ -59,14 +62,12 @@ <h1>{% trans "Merge Podcasts and Episodes" %}</h1>

<tr>
<td></td>
<td><input type="submit" name="renew" value="Renew Groups" /></td>
<td><input type="submit" name="merge" value="Merge!" /></td>
<td><button formaction="{% url "admin-merge-update" task.pk %}">Renew Groups</button></td>
<td><button>Merge</button></td>
</tr>

</table>

<input type="hidden" name="queue_id" value="{{ queue_id }}" />

</form>


Expand Down
10 changes: 5 additions & 5 deletions mygpo/administration/templates/admin/merge-select.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ <h1>{% trans "Merge Podcasts and Episodes" %}</h1>
{% endblock %}

{% block content %}
{% if queue_length > 0 %}
<div>
Queue Length: {{ queue_length }} -
<a href="{% url "admin-merge" %}?queue=1">Take from Queue</a>
Queue Length: {{ queue_length }}
{% if task %}
- <a href="{% url "admin-merge-verify" task.pk %}">Take from Queue</a>
{% endif %}
</div>
{% endif %}

<form method="post" action="{% url "admin-merge-verify" %}" role="form">
<form method="post" action="{% url "admin-merge-create" %}" role="form">
{% csrf_token %}
{% for url in urls %}
<div class="form-group">
Expand Down
10 changes: 0 additions & 10 deletions mygpo/administration/templates/admin/task-status.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ <h1>

{% if ready %}

<p>{% trans "The following actions were recorded:" %}
<ul>
{% for action, count in actions %}
<li>{{ action }}: {{ count }}</li>
{% empty %}
<li><em>{% trans "none" %}</em></li>
{% endfor %}
</ul>
</p>

<p>{% trans "Go to podcast" %} {% podcast_group_link podcast %}</p>
{% else %}
<p>{% trans "The operation is still ongoing..." %}</p>
Expand Down
12 changes: 10 additions & 2 deletions mygpo/administration/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@
views.MergeSelect.as_view(),
name='admin-merge'),

url(r'^merge/verify$',
url(r'^merge/create$',
views.CreateMergeTask.as_view(),
name='admin-merge-create'),

url(r'^merge/verify/(?P<task_id>[^/]+)$',
views.MergeVerify.as_view(),
name='admin-merge-verify'),

url(r'^merge/process$',
url(r'^merge/update/(?P<task_id>[^/]+)$',
views.UpdateMergeTask.as_view(),
name='admin-merge-update'),

url(r'^merge/process/(?P<task_id>[^/]+)$',
views.MergeProcess.as_view(),
name='admin-merge-process'),

Expand Down
124 changes: 52 additions & 72 deletions mygpo/administration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
from django.template import RequestContext
from django.utils.translation import ugettext as _
from django.contrib.sites.requests import RequestSite
from django.views.generic import TemplateView
from django.views.generic import TemplateView, View
from django.utils.decorators import method_decorator
from django.conf import settings
from django.contrib.auth import get_user_model

from mygpo.podcasts.models import Podcast, Episode
from mygpo.administration.auth import require_staff
from mygpo.administration.group import PodcastGrouper
from mygpo.maintenance.merge import PodcastMerger, IncorrectMergeException
from mygpo.maintenance.models import MergeTask
from mygpo.administration.clients import UserAgentStats, ClientStats
from mygpo.administration.tasks import merge_podcasts
Expand Down Expand Up @@ -92,29 +90,32 @@ class MergeSelect(AdminView):

def get(self, request):
queue_length = MergeTask.objects.count()
task = MergeTask.objects.first()

use_queue = bool(request.GET.get('queue', False))
if use_queue:
queue = MergeTask.objects.first()
urls = [podcast.url for podcast in queue.podcasts]
queue_id = queue.id.hex

else:
num = int(request.GET.get('podcasts', 2))
urls = [''] * num
queue_id = ''
num = int(request.GET.get('podcasts', 2))
urls = [''] * num
queue_id = ''

return self.render_to_response({
'queue_length': queue_length,
'urls': urls,
'queue_id': queue_id,
'task': task,
})


class MergeBase(AdminView):
class CreateMergeTask(AdminView):

def post(self, request):
podcasts = self._get_podcasts(request)

task = MergeTask.objects.create_from_podcasts(podcasts)

return HttpResponseRedirect(
reverse('admin-merge-verify', args=[task.id])
)

def _get_podcasts(self, request):
podcasts = []

for n in count():
podcast_url = request.POST.get('feed%d' % n, None)
if podcast_url is None:
Expand All @@ -124,84 +125,68 @@ def _get_podcasts(self, request):
continue

p = Podcast.objects.get(urls__url=podcast_url)
podcasts.append(p)
yield p

return podcasts

class MergeBase(AdminView):
pass


class MergeVerify(MergeBase):

template_name = 'admin/merge-grouping.html'

def post(self, request):

try:
podcasts = self._get_podcasts(request)

grouper = PodcastGrouper(podcasts)

get_features = lambda episode: (episode.url, episode.title)

num_groups = grouper.group(get_features)

except InvalidPodcast as ip:
messages.error(request,
_('No podcast with URL {url}').format(url=str(ip)))

podcasts = []
num_groups = []

def get(self, request, task_id):
task = MergeTask.objects.get(id=uuid.UUID(task_id))
podcasts = list(sorted(task.podcasts, key=lambda p: p.subscribers))
groups = task.episode_groups()
return self.render_to_response({
'queue_id': request.POST.get('queue_id', ''),
'podcasts': podcasts,
'groups': num_groups,
})
'podcasts': podcasts,
'groups': groups,
'task': task,
})


class MergeProcess(MergeBase):
class UpdateMergeTask(View):

RE_EPISODE = re.compile(r'episode_([0-9a-fA-F]{32})')

def post(self, request):
def post(self, request, task_id):
task = MergeTask.objects.get(id=uuid.UUID(task_id))
podcasts = task.podcasts

try:
podcasts = self._get_podcasts(request)
features = self._features_from_post(request.POST)
get_features = lambda episode: features[episode.id]

except InvalidPodcast as ip:
messages.error(request,
_('No podcast with URL {url}').format(url=str(ip)))
# update groups within MergeTask
task.set_groups(get_features)
task.save()

grouper = PodcastGrouper(podcasts)
return HttpResponseRedirect(
reverse('admin-merge-verify', args=[task.id])
)

def _features_from_post(self, post):
features = {}
for key, feature in request.POST.items():
for key, feature in post.items():
m = self.RE_EPISODE.match(key)
if m:
episode_id = uuid.UUID(m.group(1))
features[episode_id] = feature

get_features = lambda episode: features[episode.id]

num_groups = grouper.group(get_features)
queue_id = request.POST.get('queue_id', '')
return features

if 'renew' in request.POST:
return render(request, 'admin/merge-grouping.html', {
'queue_id': queue_id,
'podcasts': podcasts,
'groups': num_groups,
})

class MergeProcess(MergeBase):

elif 'merge' in request.POST:
def post(self, request, task_id):

podcast_ids = [p.get_id() for p in podcasts]
num_groups = list(num_groups)
task = MergeTask.objects.get(id=uuid.UUID(task_id))

res = merge_podcasts.delay(podcast_ids, num_groups, queue_id)
res = merge_podcasts.delay(task.pk)

return HttpResponseRedirect(reverse('admin-merge-status',
args=[res.task_id]))
return HttpResponseRedirect(reverse('admin-merge-status',
args=[res.task_id]))


class MergeStatus(AdminView):
Expand All @@ -221,16 +206,11 @@ def get(self, request, task_id):
# TODO: what to do with multiple frontends?
cache.clear()

try:
actions, podcast = result.get()

except IncorrectMergeException as ime:
messages.error(request, str(ime))
return HttpResponseRedirect(reverse('admin-merge'))
podcast_id = result.get()
podcast = Podcast.objects.get(id=podcast_id)

return self.render_to_response({
'ready': True,
'actions': actions.items(),
'podcast': podcast,
})

Expand Down
21 changes: 21 additions & 0 deletions mygpo/maintenance/migrations/0006_mergetask_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-08-13 09:01
from __future__ import unicode_literals

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('maintenance', '0005_task'),
]

operations = [
migrations.AddField(
model_name='mergetask',
name='groups',
field=django.contrib.postgres.fields.jsonb.JSONField(default=dict),
),
]
Loading

0 comments on commit d2e5904

Please sign in to comment.