Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from newsdev/move-donors
Browse files Browse the repository at this point in the history
remove donor from 2018 cycle app
  • Loading branch information
rshorey authored Jan 15, 2019
2 parents fdb8e6f + a1b07bb commit 1cb6e43
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 35 deletions.
1 change: 0 additions & 1 deletion cycle_2018/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,4 @@ class CandidateAdmin(admin.ModelAdmin):

admin.site.register(ScheduleA, ScheduleAAdmin)
admin.site.register(ScheduleE, ScheduleEAdmin)
admin.site.register(Donor, DonorAdmin)
admin.site.register(Candidate, CandidateAdmin)
22 changes: 22 additions & 0 deletions cycle_2018/migrations/0037_auto_20190115_1719.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.1.2 on 2019-01-15 17:19

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


class Migration(migrations.Migration):

dependencies = [
('cycle_2018', '0036_auto_20190108_1945'),
]

operations = [
migrations.AlterField(
model_name='schedulea',
name='donor',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='donor.Donor'),
),
migrations.DeleteModel(
name='Donor',
),
]
19 changes: 19 additions & 0 deletions cycle_2018/migrations/0038_auto_20190115_1828.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.1.2 on 2019-01-15 18:28

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


class Migration(migrations.Migration):

dependencies = [
('cycle_2018', '0037_auto_20190115_1719'),
]

operations = [
migrations.AlterField(
model_name='schedulea',
name='donor',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='contributions_2018', to='donor.Donor'),
),
]
20 changes: 1 addition & 19 deletions cycle_2018/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,6 @@ class Meta:
]


class Donor(BaseModel):
nyt_name = models.CharField(max_length=255, null=True, blank=True)
nyt_employer = models.CharField(max_length=255, null=True, blank=True)
nyt_occupation = models.CharField(max_length=255, null=True, blank=True)
nyt_note = models.TextField(null=True, blank=True)
city = models.CharField(max_length=255, null=True, blank=True)
state = models.CharField(max_length=255, null=True, blank=True)
contribution_total = models.DecimalField(max_digits=12,decimal_places=2, null=True, blank=True)

def save(self, *args, **kwargs):
self.contribution_total = self.schedulea_set.filter(active=True).aggregate(Sum('contribution_amount'))['contribution_amount__sum']
if not self.contribution_total:
self.contribution_total = 0
super().save(*args, **kwargs)

def __str__(self):
return self.nyt_name

class Transaction(BaseModel):
status = models.CharField(max_length=50, choices=STATUS_CHOICES, default='ACTIVE')
form_type = models.CharField(max_length=255, null=True, blank=True)
Expand Down Expand Up @@ -363,7 +345,7 @@ class ScheduleA(Transaction):
occupation_search = SearchVectorField(null=True)
address_search = SearchVectorField(null=True)
old_donor_id = models.CharField(max_length=255, null=True, blank=True, help_text="terrible hack for editing donor totals, do not manually edit!")
donor = models.ForeignKey(Donor, null=True, blank=True, on_delete=models.SET_NULL)
donor = models.ForeignKey('donor.Donor', null=True, blank=True, related_name='contributions_2018', on_delete=models.SET_NULL)

"""
these search fields require triggers. Please see migration 0015 which I hand-edited
Expand Down
3 changes: 2 additions & 1 deletion cycle_2018/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from cycle_2018.models import *
from cycle_2018.forms import *
from donor.models import Donor

class Echo:
"""An object that implements just the write method of the file-like
Expand Down Expand Up @@ -315,7 +316,7 @@ def races(request):
return render(request, '2018/races.html', {'races':races})

def top_donors(request):
donors = sorted(Donor.objects.all(), key=lambda d: d.contribution_total, reverse=True)
donors = sorted(Donor.objects.all(), key=lambda d: d.contribution_total_2018, reverse=True)
#this sort might be too slow for words once the database hits a reasonable size, but also maybe not bc we will never have that many named donors
paginator = Paginator(donors, 50)
page = request.GET.get('page')
Expand Down
18 changes: 18 additions & 0 deletions donor/migrations/0002_auto_20190115_1814.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.2 on 2019-01-15 18:14

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('donor', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='donor',
old_name='contribution_total',
new_name='contribution_total_2018',
),
]
18 changes: 18 additions & 0 deletions donor/migrations/0003_donor_contribution_total_2020.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.2 on 2019-01-15 18:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('donor', '0002_auto_20190115_1814'),
]

operations = [
migrations.AddField(
model_name='donor',
name='contribution_total_2020',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=12, null=True),
),
]
16 changes: 6 additions & 10 deletions donor/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models
from django.utils import timezone
from django.db.models import Sum
from cycle_2018.models import ScheduleA
#from cycle_2018.models import ScheduleA

import datetime

Expand All @@ -23,17 +23,13 @@ class Donor(BaseModel):
nyt_note = models.TextField(null=True, blank=True)
city = models.CharField(max_length=255, null=True, blank=True)
state = models.CharField(max_length=255, null=True, blank=True)
contribution_total = models.DecimalField(max_digits=12,decimal_places=2, null=True, blank=True)

@property
def schedulea_set(self):
return ScheduleA.objects.filter(donor_id=self.id)

contribution_total_2018 = models.DecimalField(max_digits=12,decimal_places=2, null=True, blank=True)
contribution_total_2020 = models.DecimalField(max_digits=12,decimal_places=2, null=True, blank=True)

def save(self, *args, **kwargs):
self.contribution_total = self.schedulea_set.filter(active=True).aggregate(Sum('contribution_amount'))['contribution_amount__sum']
if not self.contribution_total:
self.contribution_total = 0
self.contribution_total_2018 = self.contributions_2018.filter(active=True).aggregate(Sum('contribution_amount'))['contribution_amount__sum']
if not self.contribution_total_2018:
self.contribution_total_2018 = 0
super().save(*args, **kwargs)

def __str__(self):
Expand Down
2 changes: 1 addition & 1 deletion donor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ def donor_details(request, donor_id):
context = {}
donor = Donor.objects.get(id=donor_id)
context['donor'] = donor
context['contribs'] = donor.schedulea_set.filter(active=True).order_by('-contribution_amount')
context['contribs'] = donor.contributions_2018.filter(active=True).order_by('-contribution_amount')
return render(request, 'donor/donor_details.html', context)
2 changes: 2 additions & 0 deletions new_cycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ So why not template or macro this switch out? One reason we're doing this whole
* In import statements in almost all files
* In `dependencies ` in all migrations (even if sqashed)
* In any url reverses in `views.py`
* In any reverse reference to a donor's contributions
* In the donor model, add fields for the next cycle contributions and make sure they are updated in `save()`
1. In `settings.py` add the new app in `INSTALLED_APPS`
1. In `templates`, copy over the previous cycle's folder and rename for the new cycle
1. Inside each template file, update the date prefix for any forms/actions and the namespace for url reverses
Expand Down
4 changes: 2 additions & 2 deletions templates/2018/top_donors.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ <h5> Includes only contributions that have been manually matched to a donor. Bef
<td> <a href="{%url 'donor:donor_details' result.id %}">{{result.nyt_name}}</a> </td>
<td> {{result.nyt_employer}} </td>
<td>
{%if result.contribution_total > 0 %}
${{result.contribution_total|floatformat:2|intcomma}}
{%if result.contribution_total_2018 > 0 %}
${{result.contribution_total_2018|floatformat:2|intcomma}}
{%else%}
No matched contributions
{%endif%}
Expand Down
3 changes: 2 additions & 1 deletion templates/donor/donor_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ <h2> {{donor.nyt_name}} </h2>
<dl class="dl-horizontal">
<dt> Employer </dt> <dd> {{donor.employer}}</dd>
<dt> Occupation </dt> <dd> {{donor.occupation}}</dd>
<dt> Total contributed </dt> <dd> ${{donor.contribution_total|floatformat:2|intcomma}}</dd>
<dt> Total contributed 2018 </dt> <dd> ${{donor.contribution_total_2018|floatformat:2|intcomma}}</dd>
<dt> Total contributed 2020 </dt> <dd> ${{donor.contribution_total_2020|floatformat:2|intcomma}}</dd>
</dl>
</div>

Expand Down

0 comments on commit 1cb6e43

Please sign in to comment.