Skip to content

Commit

Permalink
Merge pull request #285 from DemocracyClub/ruff
Browse files Browse the repository at this point in the history
Ruff
  • Loading branch information
symroe authored Nov 29, 2024
2 parents a9ee13e + 13111f0 commit 1ac6cdf
Show file tree
Hide file tree
Showing 76 changed files with 987 additions and 1,062 deletions.
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ jobs:
name: Set up virtual environment
command: |
pipenv sync --dev
- run: pipenv run playwright install --with-deps chromium
- run: pipenv run ruff check .
- run: ./scripts/lint.sh
- run: ./scripts/format.sh
- run: make lambda-layers/DependenciesLayer/requirements.txt
- run: npm install
- run: npm run build
Expand Down
3 changes: 1 addition & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ djangorestframework = ">=3.9.1"
durationpy = "==0.4"
enum34 = "==1.1.6"
fixtures = "==3.0.0"
flake8 = "==3.8.3"
funcsigs = "==1.0.2"
dj-pagination = "==2.4.0"
linecache2 = "==1.0.0"
Expand Down Expand Up @@ -64,7 +63,6 @@ django-static-jquery = "==2.1.4"
setuptools = "*"
faker = "*"
werkzeug = "*"
black = "==19.10b0"
django-s3file = "*"
psycopg2-binary = "*"
dc_design_system = {version = "0.5.0", git = "https://github.com/DemocracyClub/design-system.git" }
Expand All @@ -85,6 +83,7 @@ pytest = "*"
pytest-playwright = "*"
pytest-cov = "*"
moto = "*"
ruff = "*"

[requires]
python_version = "3.12"
Expand Down
465 changes: 142 additions & 323 deletions Pipfile.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[![Coverage Status](https://coveralls.io/repos/DemocracyClub/electionleaflets/badge.svg?branch=master)](https://coveralls.io/r/DemocracyClub/electionleaflets?branch=django_1_7)
[![CI](https://circleci.com/gh/DemocracyClub/electionleaflets.svg?style=shield)](https://app.circleci.com/pipelines/github/DemocracyClub/electionleaflets)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

### Welcome

Expand Down Expand Up @@ -70,4 +69,4 @@ The app is deployed to AWS using the [Serverless
Framework](https://serverless.com/). This manages resources in AWS Lambda and
AWS API Gateway.

The app is deployed through CircleCI. The staging deployment is triggered by temporarily adding the current branch to context in the sam-deploy job. The production deployment is triggered by merging to master.
The app is deployed through CircleCI. The staging deployment is triggered by temporarily adding the current branch to context in the sam-deploy job. The production deployment is triggered by merging to master.
3 changes: 1 addition & 2 deletions electionleaflets/apps/analysis/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
# Generated by Django 1.9.5 on 2016-04-03 14:56


from django.db import migrations, models
import django.utils.timezone
import django_extensions.db.fields
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
# Generated by Django 1.9.5 on 2016-04-03 14:56


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


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Generated by Django 1.11.23 on 2019-12-02 23:06

import requests

from django.db import migrations


Expand All @@ -18,7 +17,6 @@ def add_missing_people(apps, schema_editor):
remote_id=leaflet.publisher_person_id
)
except Person.DoesNotExist:

url = "https://candidates.democracyclub.org.uk/api/next/people/{}/".format(
leaflet.publisher_person_id
)
Expand All @@ -32,7 +30,6 @@ def add_missing_people(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [
("analysis", "0002_auto_20160403_1456"),
("people", "0001_initial"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Generated by Django 2.2.20 on 2021-04-24 12:21

from django.db import migrations
import django_extensions.db.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("analysis", "0003_add_missing_people"),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@


class Migration(migrations.Migration):

dependencies = [
('analysis', '0004_auto_20210424_1221'),
("analysis", "0004_auto_20210424_1221"),
]

operations = [
migrations.AlterModelOptions(
name='leafletproperties',
options={'get_latest_by': 'modified'},
name="leafletproperties",
options={"get_latest_by": "modified"},
),
]
4 changes: 1 addition & 3 deletions electionleaflets/apps/analysis/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from django.contrib.auth.models import User
from django.db import models
from django_extensions.db.models import TimeStampedModel

from django.contrib.auth.models import User

from leaflets.models import Leaflet


Expand Down
7 changes: 3 additions & 4 deletions electionleaflets/apps/api/feeds.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# -*- coding: utf-8 -*-
import mimetypes

# from parties.models import Party
from constituencies.models import Constituency
from django.contrib.syndication.views import Feed
from django.shortcuts import get_object_or_404
from leaflets.models import Leaflet

# from parties.models import Party
from constituencies.models import Constituency


class LatestLeafletsFeed(Feed):
title = "electionleaflets.org latest items"
Expand All @@ -28,6 +26,7 @@ def item_description(self, item):
d = "{0} – {1}".format(d, item.images.all()[0].image.url)
return d


class ConstituencyFeed(Feed):
def get_object(self, request, cons_slug):
obj = get_object_or_404(Constituency, slug=cons_slug)
Expand Down
16 changes: 8 additions & 8 deletions electionleaflets/apps/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from leaflets.models import Leaflet, LeafletImage
from people.models import Person
from rest_framework import serializers
from sorl.thumbnail import get_thumbnail

from leaflets.models import Leaflet, LeafletImage
from constituencies.models import Constituency
from uk_political_parties.models import Party
from people.models import Person


class PartySerializer(serializers.HyperlinkedModelSerializer):
Expand Down Expand Up @@ -58,8 +56,9 @@ def get_people(self, obj):

def get_first_page_thumb(self, obj):
image = obj.get_first_image()
if image:
return get_thumbnail(obj.get_first_image().image, "350").url
if not image:
return None
return get_thumbnail(obj.get_first_image().image, "350").url

def validate(self, data):
if not data.get("status") or not data.get("images"):
Expand Down Expand Up @@ -89,8 +88,9 @@ class LeafletMinSerializer(serializers.ModelSerializer):

def get_first_page_thumb(self, obj):
image = obj.get_first_image()
if image:
return get_thumbnail(obj.get_first_image().image, "350").url
if not image:
return None
return get_thumbnail(obj.get_first_image().image, "350").url

class Meta:
model = Leaflet
Expand Down
19 changes: 14 additions & 5 deletions electionleaflets/apps/api/tests/test_feeds.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import pytest
from django.contrib.auth.models import User

from api.feeds import ConstituencyFeed, LatestLeafletsFeed
from constituencies.models import Constituency
from leaflets.tests.model_factory import LeafletFactory


@pytest.fixture(autouse=True)
def create_leaflets():
for _ in range(10):
LeafletFactory()


@pytest.fixture
def create_constituency():
return Constituency.objects.create(name="test_name", slug="test_slug")


@pytest.mark.django_db
class TestLatestLeafletsFeed:
def test_items_order(self, create_leaflets):
Expand All @@ -33,8 +34,13 @@ def test_item_description(self, create_leaflets):
if item.description:
description = item.description
if item.images.all():
description = "{0} – {1}".format(description, item.images.all()[0].image.url)
assert LatestLeafletsFeed.item_description(self, item) == description
description = "{0} – {1}".format(
description, item.images.all()[0].image.url
)
assert (
LatestLeafletsFeed.item_description(self, item) == description
)


@pytest.mark.django_db
class TestConstituencyFeed:
Expand All @@ -43,7 +49,10 @@ def test_get_object(self, create_constituency):
obj = feed.get_object(None, create_constituency.slug)
assert obj.slug == create_constituency.slug
assert feed.link == "/constituencies/%s/" % obj.slug
assert feed.description == "The most recently uploaded leaflets for %s" % obj.name
assert (
feed.description
== "The most recently uploaded leaflets for %s" % obj.name
)

def test_items(self, create_leaflets, create_constituency):
LeafletFactory(constituency=create_constituency)
Expand Down
61 changes: 38 additions & 23 deletions electionleaflets/apps/api/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,61 @@
import pytest
from electionleaflets.apps.api.views import LeafletFilter
from leaflets.models import Leaflet
from django.utils import timezone
from leaflets.models import Leaflet
from uk_political_parties.models import Party

from electionleaflets.apps.api.views import LeafletFilter


@pytest.fixture
def all_leaflets():
return [
Leaflet.objects.create(
title=f"Test Leaflet {i}",
status="live",
date_uploaded=timezone.now(),
modified=timezone.now()
)
for i in range(10)
]

return [
Leaflet.objects.create(
title=f"Test Leaflet {i}",
status="live",
date_uploaded=timezone.now(),
modified=timezone.now(),
)
for i in range(10)
]


@pytest.fixture
def party():
return Party.objects.create(
party_name="Test Party",
party_id="party:1"
)

return Party.objects.create(party_name="Test Party", party_id="party:1")


@pytest.fixture
def leaflet_with_party(all_leaflets, party):
leaflet = all_leaflets[0]
leaflet.publisher_party = party
leaflet.save()
return leaflet


@pytest.mark.django_db
class TestLeafletFilter:
def test_ballot_filter(self, all_leaflets):
leaflet = all_leaflets[0]
leaflet.ballots = [{"ballot_paper_id": "test"}]
leaflet.save()
filter = LeafletFilter(data={"ballot": "test"}, queryset=Leaflet.objects.all())
assert filter.ballot_filter(Leaflet.objects.all(), "ballot", "test").count() == 1

filter = LeafletFilter(
data={"ballot": "test"}, queryset=Leaflet.objects.all()
)
assert (
filter.ballot_filter(
Leaflet.objects.all(), "ballot", "test"
).count()
== 1
)

def test_party_filter(self, leaflet_with_party):
party = Party.objects.get(party_id="party:1")
filter = LeafletFilter(data={"party": party}, queryset=Leaflet.objects.all())
assert filter.party_filter(Leaflet.objects.all(), party, party.party_id).count() == 1

filter = LeafletFilter(
data={"party": party}, queryset=Leaflet.objects.all()
)
assert (
filter.party_filter(
Leaflet.objects.all(), party, party.party_id
).count()
== 1
)
11 changes: 5 additions & 6 deletions electionleaflets/apps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from django.db.models import Q
from django_filters import rest_framework as filters
from leaflets.models import Leaflet
from rest_framework import viewsets
from rest_framework.pagination import LimitOffsetPagination

from leaflets.models import Leaflet
from .serializers import LeafletSerializer


Expand All @@ -17,10 +17,7 @@ class StandardResultsSetPagination(LimitOffsetPagination):
class LeafletFilter(filters.FilterSet):
class Meta:
model = Leaflet
fields = {
"date_uploaded": ["gt", "exact"],
"modified": ["gt", "exact"]
}
fields = {"date_uploaded": ["gt", "exact"], "modified": ["gt", "exact"]}

def ballot_filter(self, queryset, name, value):
return queryset.filter(ballots__contains=[{"ballot_paper_id": value}])
Expand All @@ -43,7 +40,9 @@ def party_filter(self, queryset, name, value):


class LeafletViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Leaflet.objects.all().filter(status="live").prefetch_related("images")
queryset = (
Leaflet.objects.all().filter(status="live").prefetch_related("images")
)
serializer_class = LeafletSerializer
pagination_class = StandardResultsSetPagination
filter_backends = (filters.DjangoFilterBackend,)
Expand Down
Loading

0 comments on commit 1ac6cdf

Please sign in to comment.