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/dataset page #558

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file added clipping/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions clipping/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django import forms
from django.conf import settings
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.forms.models import ModelChoiceField

from .models import Clipping, ClippingRelation


class ClippingRelationAdminForm(forms.ModelForm):
contents = []
for app_name in settings.CONTENTS:
for model_name in settings.CONTENTS[app_name]:
contents.append(model_name)

content_type = ModelChoiceField(
ContentType.objects.filter(app_label__in=settings.CONTENTS, model__in=contents),
empty_label="--------",
label="Content",
)
object_id = forms.CharField(widget=forms.Select(choices=[("", "---------")]), label="Element")

class Meta:
model = ClippingRelation
fields = ["content_type", "object_id", "clipping"]


@admin.register(ClippingRelation)
class ClippingRelationAdmin(admin.ModelAdmin):
form = ClippingRelationAdminForm
list_display = ("get_clipping_relation", "clipping", "content_type")

def get_clipping_relation(self, obj):
return obj.content_object.name

get_clipping_relation.short_description = "Relation"


class ClippingAdminForm(forms.ModelForm):
category = forms.CharField(widget=forms.Select(choices=settings.CATEGORY_CHOICES), label="Category")

class Meta:
model = Clipping
exclude = ["added_by"]


@admin.register(Clipping)
class ClippingAdmin(admin.ModelAdmin):
form = ClippingAdminForm
list_display = ("date", "title", "author", "vehicle", "category", "url", "added_by", "published")

# Sets the current user as the adder
def save_model(self, request, obj, form, change):
if getattr(obj, "added_by", None) is None:
obj.added_by = request.user
obj.save()
5 changes: 5 additions & 0 deletions clipping/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class ClippingConfig(AppConfig):
name = "clipping"
17 changes: 17 additions & 0 deletions clipping/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django import forms
from django.conf import settings
from django.forms import ModelForm

from .models import Clipping


class ClippingForm(ModelForm):
category = forms.CharField(widget=forms.Select(choices=settings.CATEGORY_CHOICES), label="Categoria")
date = forms.CharField(widget=forms.TextInput(attrs={"class": "datepicker"}), label="Data")
author = forms.CharField(label="Autor")
title = forms.CharField(label="Título")

class Meta:
model = Clipping
fields = "__all__"
exclude = ["added_by", "published"]
38 changes: 38 additions & 0 deletions clipping/management/commands/import_clippings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import csv
from pathlib import Path

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand

from clipping.models import Clipping


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("csv", type=Path, help="CSV file to update the clippings JSON field")

def clean_args(self, **kwargs):
csv_file = kwargs["csv"]
if csv_file and not csv_file.exists():
raise Exception(f"The CSV {csv_file} does not exists")
return csv_file

def handle(self, *args, **kwargs):
csv_file = self.clean_args(**kwargs)
username = "turicas"
user = get_user_model().objects.get(username=username)
with open(csv_file, encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
p = Clipping(
date=row["data"],
vehicle=row["veiculo"],
author=row["autor"],
title=row["titulo"],
category=row["categoria"],
url=row["link"],
published=True,
added_by=user,
)
p.save()
self.stdout.write(self.style.SUCCESS("Successfully imported"))
45 changes: 45 additions & 0 deletions clipping/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 3.1.6 on 2021-04-09 13:04

import datetime
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('contenttypes', '0002_remove_content_type_name'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Clipping',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateField(default=datetime.date.today)),
('vehicle', models.CharField(blank=True, max_length=100, null=True)),
('author', models.CharField(blank=True, max_length=100, null=True)),
('title', models.CharField(blank=True, max_length=200, null=True)),
('category', models.CharField(max_length=100, null=True)),
('url', models.URLField(unique=True)),
('published', models.BooleanField(blank=True, default=False)),
('added_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='ClippingRelation',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('object_id', models.PositiveIntegerField(db_index=True)),
('clipping', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='clipping.clipping')),
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')),
],
options={
'unique_together': {('content_type', 'object_id', 'clipping')},
},
),
]
Empty file added clipping/migrations/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions clipping/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import datetime

from django.contrib.auth import get_user_model
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models


class Clipping(models.Model):
date = models.DateField(null=False, blank=False, default=datetime.date.today)
vehicle = models.CharField(max_length=100, null=True, blank=True)
author = models.CharField(max_length=100, null=True, blank=True)
title = models.CharField(max_length=200, null=True, blank=True)
category = models.CharField(max_length=100, null=True)
url = models.URLField(null=False, blank=False, unique=True)
published = models.BooleanField(default=False, blank=True)

added_by = models.ForeignKey(get_user_model(), null=True, blank=True, on_delete=models.SET_NULL)

def __str__(self):
return self.title


class ClippingRelation(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField(db_index=True)
content_object = GenericForeignKey()

clipping = models.ForeignKey(Clipping, on_delete=models.CASCADE)

def __str__(self):
return "Content: {} | Clipping: {}".format(self.content_object.name, self.clipping.title)

class Meta:
unique_together = ["content_type", "object_id", "clipping"]
43 changes: 43 additions & 0 deletions clipping/templates/admin/clipping/change_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% extends "admin/change_form.html" %} {% block extrahead %} {{ block.super }}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
const populateSelect = () =>
$.getJSON(
"/clipping/get/contentype_instances/",
{ id: $("select#id_content_type").val() },
function (j) {
var options = '<option value="">---------</option>';
for (var i = 0; i < j.length; i++) {
options +=
'<option value="' + j[i].id + '">' + j[i].name + "</option>";
}
// inspect html to check id of subcategory select
$("select#id_object_id").html(options);
}
);

// Populate content select then set element select
const setObjectIdSelect = async (j) => {
await populateSelect();
$("select#id_object_id").val(j);
};

/*
inspect html to check id of Content select, populate
select Element if change occurs
*/
$(document).on("change", "select#id_content_type", populateSelect);

// Document ready
$(function () {
const objPk = $(location).attr("href").match("\/([0-9_]+)\/");
if (objPk && $("select#id_content_type").val()) {
$.getJSON("/clipping/get/selected_instance/", { id: objPk[1] }, (j) =>
setObjectIdSelect(j)
);
} else if($("select#id_content_type").val()) {
populateSelect()
}
});
</script>
{% endblock %}
1 change: 1 addition & 0 deletions clipping/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
8 changes: 8 additions & 0 deletions clipping/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path

from . import views

urlpatterns = [
path("get/contentype_instances/", views.get_contenttype_instances),
path("get/selected_instance/", views.get_current_selected_instance),
]
18 changes: 18 additions & 0 deletions clipping/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json

from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponse

from .models import ClippingRelation


def get_contenttype_instances(request):
pk = request.GET.get("id")
result = list(ContentType.objects.get(id=pk).model_class().objects.filter().values("id", "name"))
return HttpResponse(json.dumps(result), content_type="application/json")


def get_current_selected_instance(request):
pk = request.GET.get("id")
result = ClippingRelation.objects.get(id=pk).object_id
return HttpResponse(json.dumps(result), content_type="application/json")
19 changes: 19 additions & 0 deletions core/migrations/0030_table_short_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.1.8 on 2021-09-15 19:02

from django.db import migrations
import markdownx.models


class Migration(migrations.Migration):

dependencies = [
('core', '0029_auto_20201206_2132'),
]

operations = [
migrations.AddField(
model_name='table',
name='short_description',
field=markdownx.models.MarkdownxField(blank=True, null=True),
),
]
18 changes: 18 additions & 0 deletions core/migrations/0031_auto_20210915_1605.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.8 on 2021-09-15 19:05

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('core', '0030_table_short_description'),
]

operations = [
migrations.RenameField(
model_name='dataset',
old_name='description',
new_name='short_description',
),
]
19 changes: 19 additions & 0 deletions core/migrations/0032_dataset_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.1.8 on 2021-09-15 20:26

from django.db import migrations
import markdownx.models


class Migration(migrations.Migration):

dependencies = [
('core', '0031_auto_20210915_1605'),
]

operations = [
migrations.AddField(
model_name='dataset',
name='description',
field=markdownx.models.MarkdownxField(blank=True, null=True),
),
]
19 changes: 19 additions & 0 deletions core/migrations/0033_auto_20210915_1731.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.1.8 on 2021-09-15 20:31

from django.db import migrations
import markdownx.models


class Migration(migrations.Migration):

dependencies = [
('core', '0032_dataset_description'),
]

operations = [
migrations.AlterField(
model_name='dataset',
name='short_description',
field=markdownx.models.MarkdownxField(blank=True, null=True),
),
]
4 changes: 3 additions & 1 deletion core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ class Dataset(models.Model):
author_name = models.CharField(max_length=255, null=False, blank=False)
author_url = models.URLField(max_length=2000, null=True, blank=True)
code_url = models.URLField(max_length=2000, null=False, blank=False)
description = models.TextField(null=False, blank=False)
short_description = MarkdownxField(null=True, blank=True)
description = MarkdownxField(null=True, blank=True)
icon = models.CharField(max_length=31, null=False, blank=False)
license_name = models.CharField(max_length=255, null=False, blank=False)
license_url = models.URLField(max_length=2000, null=False, blank=False)
Expand Down Expand Up @@ -311,6 +312,7 @@ class Table(models.Model):
version = models.ForeignKey(Version, on_delete=models.CASCADE, null=False, blank=False)
import_date = models.DateTimeField(null=True, blank=True)
description = MarkdownxField(null=True, blank=True)
short_description = MarkdownxField(null=True, blank=True)
hidden = models.BooleanField(default=False)
api_enabled = models.BooleanField(default=True)

Expand Down