Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/item to collection admin enhancement #151

10 changes: 9 additions & 1 deletion giza/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser, Lesson, Topic, Collection
from .models import CustomUser, Lesson, Topic, Collection, ElasticsearchItem

class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
Expand All @@ -15,9 +15,17 @@ class CustomUserAdmin(UserAdmin):
(None, {'fields': ('full_name', 'bio', 'tagline', 'picture')}),
)


class ElasticsearchItemInline(admin.TabularInline):
model = ElasticsearchItem

class CollectionAdmin(admin.ModelAdmin):
readonly_fields=('slug',)

inlines = [
ElasticsearchItemInline,
]

class LessonAdmin(admin.ModelAdmin):
readonly_fields=('slug',)

Expand Down
8 changes: 7 additions & 1 deletion giza/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from tinymce.widgets import TinyMCE

from .models import CustomUser
from .models import CustomUser, Collection

class CustomUserCreationForm(UserCreationForm):
class Meta:
Expand All @@ -13,3 +13,9 @@ class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('full_name', 'username', 'email',)

class CollectionForm(forms.ModelForm):
title = forms.CharField(required=False, label="Name your new collection")
class Meta:
model = Collection
fields = ('title', )
27 changes: 27 additions & 0 deletions giza/migrations/0005_auto_20210314_0551.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 2.2.12 on 2021-03-14 05:51

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


class Migration(migrations.Migration):

dependencies = [
('giza', '0004_lesson_summary'),
]

operations = [
migrations.RemoveField(
model_name='collection',
name='items',
),
migrations.CreateModel(
name='ElasticsearchItem',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('type', models.CharField(max_length=20)),
('es_id', models.IntegerField()),
('collection', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='items', to='giza.Collection')),
],
),
]
19 changes: 19 additions & 0 deletions giza/migrations/0006_collection_public.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.12 on 2021-04-15 13:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('giza', '0005_auto_20210314_0551'),
]

operations = [
migrations.AddField(
model_name='collection',
name='public',
field=models.BooleanField(blank=True, default=False),
preserve_default=False,
),
]
21 changes: 11 additions & 10 deletions giza/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.contrib.postgres.fields import JSONField
from django.utils.text import slugify

from tinymce.models import HTMLField
Expand All @@ -13,12 +12,6 @@
(PRIVATE, 'Private'),
]

EDITION = 'EDITION'
TRANSLATION = 'TRANSLATION'
CONTENT_TYPE_CHOICES = [
(EDITION, 'Edition'),
(TRANSLATION, 'Translation'),
]


class CustomUser(AbstractUser):
Expand Down Expand Up @@ -79,18 +72,17 @@ def save(self, *args, **kwargs):
self.slug = self._get_unique_slug()
super().save(*args, **kwargs)


class Collection(models.Model):
title = models.CharField(max_length=256)
slug = models.SlugField(blank=True)
public = models.BooleanField(blank=True, default=False)
owners = models.ManyToManyField(
'CustomUser', related_name='owners', blank=True)
topics = models.ManyToManyField('Topic', related_name='collections_topics', blank=True)
picture = models.ImageField(
upload_to='images', blank=True)

# possibly consider json field for this in the future
items = models.TextField(blank=True)

def __str__(self):
return self.title

Expand All @@ -105,3 +97,12 @@ def _get_unique_slug(self):
def save(self, *args, **kwargs):
self.slug = self._get_unique_slug()
super().save(*args, **kwargs)


class ElasticsearchItem(models.Model):
collection = models.ForeignKey(Collection, related_name='items', on_delete=models.CASCADE)
type = models.CharField(max_length=20)
es_id = models.IntegerField()

def __str__(self):
return "{}-{}".format(self.es_id, self.type)
4 changes: 3 additions & 1 deletion giza/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
from django.conf.urls import url
from django.urls import path
from django.conf.urls import include
from django.contrib import admin
from tms import views as tms_views
from search import views as search_views
Expand All @@ -34,7 +35,7 @@
url(r'^collections/user$', views.collections_user, name="collections_user"),
url(r'^collections/create$', views.collections_create, name="collections_create"),
url(r'^collections/(?P<slug>[\w-]+)$', views.collection, name="collection"),
url(r'^collections/(?P<slug>[\w-]+)/edit$', views.collection, name="collection_edit"),
url(r'^collections/(?P<slug>[\w-]+)/edit$', views.collections_edit, name="collection_edit"),
url(r'^collections/$', views.collections, name="collections"),
url(r'^mygiza/$', views.mygiza, name="mygiza"),

Expand All @@ -47,6 +48,7 @@
url(r'^search-results/$', search_views.results, name='results'),

# auth
# TODO for password change/reset, implement django accounts auth
# path('accounts/', include('django.contrib.auth.urls')),
url('sign-up/', views.sign_up, name='sign_up'),
url('login/', views.user_login, name='login'),
Expand Down
128 changes: 110 additions & 18 deletions giza/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from utils.elastic_backend import es, ES_INDEX
from utils.views_utils import CATEGORIES, FACETS_PER_CATEGORY, FIELDS_PER_CATEGORY

from .forms import CustomUserCreationForm
from .models import Collection, Lesson
from .forms import CustomUserCreationForm, CollectionForm
from .models import Collection, Lesson, ElasticsearchItem

RESULTS_SIZE = 20

Expand Down Expand Up @@ -245,7 +245,7 @@ def mygiza(request):
return render(request, 'pages/mygiza-landing.html')

def collections(request):
collections = Collection.objects.all()
collections = Collection.objects.filter(public=True)

return render(request, 'pages/mygiza-allcollections.html', {
'collections': collections,
Expand All @@ -266,7 +266,40 @@ def collection(request, slug):
hits = []

search_term = request.GET.get('q', '')
item_ids = collection.items.split(",")
query = {}

if collection.items.all():
query = {
'bool': {
"should": [],
}
}
for elasticsearch_item in collection.items.all():
query['bool']['should'].append({
'bool': {
'must': [
{
'term': {
"_type": elasticsearch_item.type,
}
},
{
'term': {
"_id": elasticsearch_item.es_id,
}
},
]
}
})
else:
# pass a query that will get no values returned
query = {
'ids': {
'type': '_doc',
'values': []
}
}

categorystring = ""
current_category = request.GET.get('category', '')
current_subfacets = {}
Expand All @@ -291,11 +324,7 @@ def collection(request, slug):
body_query = {
"from": results_from,
"size": RESULTS_SIZE,
"query": {
"ids": {
"values": item_ids,
}
},
"query": query,
"aggregations": {
"aggregation": {
"terms": {
Expand Down Expand Up @@ -323,12 +352,8 @@ def collection(request, slug):

search_results = es.search(index=ES_INDEX, body={
"from": results_from,
"size": RESULTS_SIZE,
"query": {
"ids": {
"values": item_ids,
}
},
"size": 10000,
"query": query,
"aggregations": {
"aggregation": {
"terms": {
Expand Down Expand Up @@ -399,11 +424,78 @@ def collection(request, slug):

def collections_create(request):

return render(request, 'pages/mygiza-collection-edit.html')
# create a collection
if request.method == 'POST':
collection_form = CollectionForm(data=request.POST)

# save user
if collection_form.is_valid():
# create user
collection = collection_form.save()
collection.owners.add(request.user)
collection.save()

return redirect('/collections/{}'.format(collection.slug))

else:
messages.error(request, "Error creating collection.")

# show collection form
else:
collection_form = CollectionForm()

def collections_edit(request):
return render(request, 'pages/mygiza-collection-edit.html', {
'collection_form': collection_form,
})

def collections_edit(request, slug):

# create a collection
if request.method == 'POST':
collection_form = CollectionForm(data=request.POST)

return render(request, 'pages/mygiza-collection-edit.html')
# save user
if collection_form.is_valid():
# create user
collection = collection_form.save()
collection.save()

return redirect('/collections/{}'.format(collection.slug))

else:
messages.error(request, "Error creating collection.")

# show collection form
else:
collection = get_object_or_404(Collection, slug=slug)
collection_form = CollectionForm(collection)

# user does not own this collection, redirect to collections page
if not request.user in collection.owners.all():
return redirect('/collections/')

# handle adding new item id and type to collection
if request.GET.get('add_item_id') and request.GET.get('add_item_type'):
elasticsearch_item = ElasticsearchItem(
es_id=request.GET.get('add_item_id'),
type=request.GET.get('add_item_type'),
collection=collection
)
elasticsearch_item.save()
return redirect('/collections/{}'.format(collection.slug))

elif request.GET.get('remove_item_id') and request.GET.get('remove_item_type'):
elasticsearch_item = ElasticsearchItem.objects.filter(
es_id=request.GET.get('remove_item_id'),
type=request.GET.get('remove_item_type'),
collection=collection
)
elasticsearch_item.delete()
return redirect('/collections/{}'.format(collection.slug))

return render(request, 'pages/mygiza-collection-edit.html', {
'collection_form': collection_form,
})

def lessons(request):
lessons = Lesson.objects.all()
Expand Down
18 changes: 18 additions & 0 deletions templates/pages/full.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ <h1>{{ object.displaytext }}<a name="top"></a></h1>

<div class="feature-block secondary" id="jumpmenu">
<div class="feature-block__body">
<h5 class="heading-em">MyGiza:</h5>
<p>
{% if user.is_authenticated %}
<a data-open="add_collection" aria-controls="add_collection" aria-haspopup="true" tabindex="0">
<span class="icon-plus-circle icon-fw"></span> Add this to a collection
</a>
{% else %}
<a href="/login" tabindex="0">
<span class="icon-plus-circle icon-fw"></span> Add this to a collection
</a>
{% endif %}
</p>

<h5 class="heading-em">Jump to:</h5>
rsinghal marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -829,6 +841,12 @@ <h3 class="feature-block__title">

<!-- /Modal -->

<!--
TEMPORARY FIX:
Include modals and styles for Adding items to Collections
-->
{% include 'partials/modals.html' %}
<link rel="stylesheet" href="{% static 'css/app.css' %}">
{% endblock %}

{% block extra_js %}
Expand Down
Loading