This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.core.management.base import (BaseCommand) | ||
from django.db import transaction | ||
|
||
from api.models import FacilityList, FacilityListItem, Source | ||
|
||
|
||
@transaction.atomic | ||
def list_to_source(facility_list): | ||
source_qs = Source.objects.filter( | ||
source_type=Source.LIST, facility_list=facility_list) | ||
if source_qs.exists(): | ||
source = source_qs.first() | ||
else: | ||
source = Source(source_type=Source.LIST, | ||
facility_list=facility_list) | ||
|
||
source.contributor = facility_list.contributor | ||
source.is_public = facility_list.is_public | ||
source.is_active = facility_list.is_active | ||
source.save() | ||
|
||
# Override auto_now values by using a queryset update | ||
Source.objects.filter(pk=source.pk).update( | ||
created_at=facility_list.created_at, | ||
updated_at=facility_list.updated_at) | ||
|
||
FacilityListItem.objects \ | ||
.filter(facility_list=facility_list) \ | ||
.update(source=source) | ||
|
||
|
||
def lists_to_sources(): | ||
for facility_list in FacilityList.objects.all(): | ||
list_to_source(facility_list) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = ('Create/update Source records from FacilityList records and ' | ||
'connect the Source records to FacilityListItem records.') | ||
|
||
def handle(self, *args, **options): | ||
lists_to_sources() |
33 changes: 33 additions & 0 deletions
33
src/django/api/migrations/0036_add_source_20190926_2048.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Generated by Django 2.2.3 on 2019-10-01 15:31 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('api', '0035_add_facility_history_switch'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Source', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('source_type', models.CharField(choices=[('LIST', 'LIST'), ('SINGLE', 'SINGLE')], help_text='Did the the facility data arrive in a list or a single item', max_length=6)), | ||
('is_active', models.BooleanField(default=True, help_text='True if items from the source should be shown as being associated with the contributor')), | ||
('is_public', models.BooleanField(default=True, help_text='True if the public can see factories from this list are associated with the contributor.')), | ||
('create', models.BooleanField(default=True, help_text='Should a facility or facility match be created from the facility data')), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
('contributor', models.ForeignKey(help_text='The contributor who submitted the facility data', null=True, on_delete=django.db.models.deletion.SET_NULL, to='api.Contributor')), | ||
('facility_list', models.OneToOneField(help_text='The related list if the type of the source is LIST.', null=True, on_delete=django.db.models.deletion.PROTECT, to='api.FacilityList')), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='facilitylistitem', | ||
name='source', | ||
field=models.ForeignKey(blank=True, help_text='The source from which this item was created.', null=True, on_delete=django.db.models.deletion.PROTECT, to='api.Source'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- Count the number of facility list CSV rows uploaded each month. | ||
|
||
SELECT | ||
CAST (date_part('month', i.created_at) AS int) AS month, | ||
CASE WHEN u.email LIKE '%openapparel.org%' THEN 'y' ELSE 'n' END AS is_public_list, | ||
COUNT(*) AS item_count | ||
FROM api_facilitylistitem i | ||
JOIN api_facilitylist l on i.facility_list_id = l.id | ||
JOIN api_contributor c ON l.contributor_id = c.id | ||
JOIN api_user u ON u.id = c.admin_id | ||
WHERE date_part('month', i.created_at) != date_part('month', now()) | ||
GROUP BY date_part('month', i.created_at), is_public_list | ||
ORDER BY date_part('month', i.created_at), is_public_list; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters