-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move url storage from Feedback to Notifcation
To help troubleshooting, we create an abstract model for sharing analysis methods among models and move the storage of urls from the Feedback model to Notifications.
- Loading branch information
Showing
7 changed files
with
184 additions
and
78 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
paying_for_college/migrations/0013_add_notification_url.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,19 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('paying_for_college', '0012_add_feedback_url'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='notification', | ||
name='url', | ||
field=models.TextField(null=True, blank=True), | ||
), | ||
] |
57 changes: 57 additions & 0 deletions
57
paying_for_college/migrations/0014_migrate_url_field_to_notifications.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,57 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations | ||
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned | ||
|
||
|
||
def parse_url(feedback): | ||
""" | ||
Replicates the Feedback model method 'parsed_url' because model methods | ||
are not available in migrations (grumble). | ||
""" | ||
data = {} | ||
if 'feedback' in feedback.url or '?' not in feedback.url: | ||
return data | ||
split_fields = feedback.url.split('?')[1].split('&') | ||
for field in split_fields: | ||
pair = field.split('=') | ||
data[pair[0]] = pair[1] | ||
return data | ||
|
||
|
||
def forward(apps, schema_editor): | ||
Notification = apps.get_model('paying_for_college', 'Notification') | ||
Feedback = apps.get_model('paying_for_college', 'Feedback') | ||
db_alias = schema_editor.connection.alias | ||
|
||
for feedback in Feedback.objects.using(db_alias).exclude(url=None): | ||
try: | ||
notification = Notification.objects.using( | ||
db_alias).get(oid=parse_url(feedback).get('oid')) | ||
notification.url = feedback.url | ||
notification.save() | ||
except ObjectDoesNotExist: | ||
continue | ||
except MultipleObjectsReturned: | ||
continue | ||
|
||
|
||
def backward(apps, schema_editor): | ||
Notification = apps.get_model('paying_for_college', 'Notification') | ||
db_alias = schema_editor.connection.alias | ||
|
||
for notification in Notification.objects.using(db_alias).all(): | ||
notification.url = None | ||
notification.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('paying_for_college', '0013_add_notification_url'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(forward, backward), | ||
] |
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
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