Skip to content

Commit

Permalink
show recently viewed items on the index page
Browse files Browse the repository at this point in the history
fixes #417
  • Loading branch information
christianp committed Mar 20, 2019
1 parent 083df2b commit 9dc49ac
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 21 deletions.
27 changes: 27 additions & 0 deletions accounts/migrations/0018_editoritemviewed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 2.0.13 on 2019-03-20 15:02

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('editor', '0037_auto_20190320_1436'),
('accounts', '0017_auto_20171018_0843'),
]

operations = [
migrations.CreateModel(
name='EditorItemViewed',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateTimeField(auto_now=True)),
('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='views', to='editor.EditorItem')),
('userprofile', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='last_viewed_items', to='accounts.UserProfile')),
],
options={
'ordering': ('-date',),
},
),
]
17 changes: 16 additions & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from sanitizer.models import SanitizedTextField

from editor.models import NewQuestion, EditorTag, Project, TimelineItem, SiteBroadcast
from editor.models import NewQuestion, EditorTag, Project, TimelineItem, SiteBroadcast, EditorItem

class RegistrationManager(regmodels.RegistrationManager):
@transaction.atomic
Expand Down Expand Up @@ -92,6 +92,21 @@ def public_timeline(self):
def get_absolute_url(self):
return reverse('view_profile', args=(self.pk,))

class EditorItemViewed(models.Model):
userprofile = models.ForeignKey(UserProfile, related_name='last_viewed_items', on_delete=models.CASCADE)
item = models.ForeignKey(EditorItem,related_name='views', on_delete=models.CASCADE)
date = models.DateTimeField(auto_now=True)

class Meta:
ordering = ('-date',)

@receiver(signals.post_save, sender=EditorItemViewed)
def truncate_last_viewed_items(instance, created, **kwargs):
views = EditorItemViewed.objects.filter(userprofile=instance.userprofile)
old = views[5:].values_list('id',flat=True)
if old:
views.filter(pk__in=old).delete()

class BasketQuestion(models.Model):
class Meta:
ordering = ['qn_order']
Expand Down
48 changes: 32 additions & 16 deletions editor/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends "base.html" %}
{% load timeline %}
{% load sstatic %}
{% load links %}

{% block javascripts %}
{{block.super}}
Expand Down Expand Up @@ -75,25 +76,40 @@ <h3>Or just take a look around.</h3>
</div>
</div>
<div class="col-sm-4">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="panel-title"><span class="glyphicon glyphicon-plus"></span> Create</h2>
</div>
<div class="panel-body create">
<a href="{% url 'question_new' %}" class="btn btn-lg btn-link"><span class="glyphicon glyphicon-file"></span> Question</a>
<a href="{% url 'exam_new' %}" class="btn btn-lg btn-link"><span class="glyphicon glyphicon-book"></span> Exam</a>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="panel-title"><span class="glyphicon glyphicon-plus"></span> Create</h2>
</div>
<div class="panel-body create">
<a href="{% url 'question_new' %}" class="btn btn-lg btn-link"><span class="glyphicon glyphicon-file"></span> Question</a>
<a href="{% url 'exam_new' %}" class="btn btn-lg btn-link"><span class="glyphicon glyphicon-book"></span> Exam</a>
</div>
</div>

<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title"><a href="{% url 'search' %}"><span class="glyphicon glyphicon-search"></span> Browse the public database</a></h2>
</div>
<div class="panel-body browse">
<a href="{% url 'search' %}?item_types=questions" class="btn btn-lg btn-link"><span class="glyphicon glyphicon-file"></span> Questions</a>
<a href="{% url 'search' %}?item_types=exams" class="btn btn-lg btn-link"><span class="glyphicon glyphicon-book"></span> Exams</a>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title"><a href="{% url 'search' %}"><span class="glyphicon glyphicon-search"></span> Browse the public database</a></h2>
</div>
<div class="panel-body browse">
<a href="{% url 'search' %}?item_types=questions" class="btn btn-lg btn-link"><span class="glyphicon glyphicon-file"></span> Questions</a>
<a href="{% url 'search' %}?item_types=exams" class="btn btn-lg btn-link"><span class="glyphicon glyphicon-book"></span> Exams</a>
</div>
</div>

{% if request.user.userprofile.last_viewed_items.exists %}
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title"><span class="glyphicon glyphicon-eye-open"></span> Recently viewed items</h2>
</div>
<ul class="list-group">
{% for itemview in request.user.userprofile.last_viewed_items.all %}
<li class="list-group-item">
{% editoritem_link itemview.item.rel_obj show_icon=True %}
</li>
{% endfor %}
</ul>
</div>
{% endif %}

<div class="panel panel-default">
<div class="panel-heading">
Expand Down
5 changes: 4 additions & 1 deletion editor/templates/links/editoritem.html
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
<a href="{{item.get_absolute_url}}">{{item.editoritem.name}}</a>
<a href="{{item.get_absolute_url}}">
{% if show_icon %}<span class="glyphicon glyphicon-{{item.icon}}"></span>{% endif %}
{{item.editoritem.name}}
</a>
4 changes: 2 additions & 2 deletions editor/templatetags/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def exam_link(e):
return {'e': e}

@register.inclusion_tag('links/editoritem.html')
def editoritem_link(item):
return {'item': item}
def editoritem_link(item,show_icon=False):
return {'item': item, 'show_icon': show_icon}

@register.simple_tag
def editoritem_url(link, item):
Expand Down
6 changes: 5 additions & 1 deletion editor/views/editoritem.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from django_tables2.config import RequestConfig

from accounts.models import UserProfile
from accounts.models import UserProfile, EditorItemViewed

from editor.tables import EditorItemTable
from editor.models import EditorItem, Project, Access, Licence, PullRequest, Taxonomy, Contributor
Expand Down Expand Up @@ -151,6 +151,10 @@ def get(self, request, *args, **kwargs):
else:
if not self.user.is_anonymous:
self.user.notifications.filter(target_object_id=self.object.pk).mark_all_as_read()
item = self.object.editoritem
v, created = EditorItemViewed.objects.get_or_create(userprofile=self.user.userprofile,item=item)
if not created:
v.save()

return super(BaseUpdateView, self).get(request, *args, **kwargs)

Expand Down

0 comments on commit 9dc49ac

Please sign in to comment.