Skip to content

Commit

Permalink
make lint
Browse files Browse the repository at this point in the history
  • Loading branch information
turicas committed May 10, 2024
1 parent 1dadb91 commit 6fd3b8b
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 53 deletions.
31 changes: 17 additions & 14 deletions clipping/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from django import forms
from django.conf import settings
from django.contrib import admin
from django.forms.models import ModelChoiceField

from .models import ClippingRelation, Clipping
from django.contrib.contenttypes.models import ContentType
from django.forms.models import ModelChoiceField

from django.conf import settings
from .models import Clipping, ClippingRelation


class ClippingRelationAdminForm(forms.ModelForm):
Expand All @@ -14,40 +13,44 @@ class ClippingRelationAdminForm(forms.ModelForm):
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')
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']
fields = ["content_type", "object_id", "clipping"]


@admin.register(ClippingRelation)
class ClippingRelationAdmin(admin.ModelAdmin):
form = ClippingRelationAdminForm
list_display = ('get_clipping_relation', 'clipping', 'content_type')
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'

get_clipping_relation.short_description = "Relation"


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

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


@admin.register(Clipping)
class ClippingAdmin(admin.ModelAdmin):
form = ClippingAdminForm
list_display = ('date','title', 'author', 'vehicle', 'category', 'url', 'added_by', 'published')
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:
if getattr(obj, "added_by", None) is None:
obj.added_by = request.user
obj.save()
2 changes: 1 addition & 1 deletion clipping/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class ClippingConfig(AppConfig):
name = 'clipping'
name = "clipping"
17 changes: 9 additions & 8 deletions clipping/forms.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from django.forms import ModelForm
from .models import Clipping
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')
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']
fields = "__all__"
exclude = ["added_by", "published"]
22 changes: 14 additions & 8 deletions clipping/management/commands/import_clippings.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import os
import csv
import rows

from django.core.management.base import BaseCommand
from pathlib import Path

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

from clipping.models import Clipping


Expand All @@ -23,10 +21,18 @@ 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:
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 = 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'))
self.stdout.write(self.style.SUCCESS("Successfully imported"))
8 changes: 4 additions & 4 deletions clipping/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import datetime

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


class Clipping(models.Model):
Expand All @@ -14,8 +15,7 @@ class Clipping(models.Model):
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)
added_by = models.ForeignKey(get_user_model(), null=True, blank=True, on_delete=models.SET_NULL)

def __str__(self):
return self.title
Expand All @@ -32,4 +32,4 @@ def __str__(self):
return "Content: {} | Clipping: {}".format(self.content_object.name, self.clipping.title)

class Meta:
unique_together = ['content_type', 'object_id', 'clipping']
unique_together = ["content_type", "object_id", "clipping"]
2 changes: 0 additions & 2 deletions clipping/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from django.test import TestCase

# Create your tests here.
4 changes: 2 additions & 2 deletions clipping/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from . import views

urlpatterns = [
path('get/contentype_instances/', views.get_contenttype_instances),
path('get/selected_instance/', views.get_current_selected_instance),
path("get/contentype_instances/", views.get_contenttype_instances),
path("get/selected_instance/", views.get_current_selected_instance),
]
13 changes: 7 additions & 6 deletions clipping/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import json
from django.http import HttpResponse
from django.shortcuts import render # get_object_or_404, redirect

from django.contrib.contenttypes.models import ContentType
from .models import Clipping, ClippingRelation
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'))
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')
pk = request.GET.get("id")
result = ClippingRelation.objects.get(id=pk).object_id
return HttpResponse(json.dumps(result), content_type="application/json")
18 changes: 10 additions & 8 deletions core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import uuid

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.core.mail import EmailMessage
from django.core.paginator import Paginator
Expand All @@ -10,6 +11,8 @@
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse

from clipping.forms import ClippingForm
from clipping.models import ClippingRelation
from core.filters import parse_querystring
from core.forms import ContactForm, DatasetSearchForm, get_table_dynamic_form
from core.middlewares import disable_non_logged_user_cache
Expand All @@ -18,9 +21,6 @@
from core.util import cached_http_get_json
from data_activities_log.activites import recent_activities
from traffic_control.logging import log_blocked_request
from clipping.models import ClippingRelation
from clipping.forms import ClippingForm
from django.contrib.contenttypes.models import ContentType


class Echo:
Expand Down Expand Up @@ -208,15 +208,17 @@ def dataset_detail(request, slug, tablename=""):
del querystring[key]

clipping = []
clipping_type_dataset = ContentType.objects.get(app_label='core', model='dataset')
clipping_dataset = list(ClippingRelation.objects.filter(content_type=clipping_type_dataset.id, object_id=dataset.pk))
clipping_type_dataset = ContentType.objects.get(app_label="core", model="dataset")
clipping_dataset = list(
ClippingRelation.objects.filter(content_type=clipping_type_dataset.id, object_id=dataset.pk)
)
clipping.extend(clipping_dataset)
clipping_type_table = ContentType.objects.get(app_label='core', model='table')
clipping_type_table = ContentType.objects.get(app_label="core", model="table")
clipping_table = list(ClippingRelation.objects.filter(content_type=clipping_type_table.id, object_id=table.pk))
clipping.extend(clipping_table)

message = None
if request.method == 'POST':
if request.method == "POST":
clipping_form = ClippingForm(request.POST)
if clipping_form.is_valid():
clipping = clipping_form.save(commit=False)
Expand Down Expand Up @@ -244,7 +246,7 @@ def dataset_detail(request, slug, tablename=""):
"total_count": all_data.count(),
"version": version,
"form": clipping_form,
"message": message
"message": message,
}

status = 200
Expand Down

0 comments on commit 6fd3b8b

Please sign in to comment.