Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial development #1

Merged
merged 65 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
584727d
WIP: Initial commit
KalobTaulien Apr 22, 2020
a795153
Package support
KalobTaulien Apr 22, 2020
aaeee4a
Minor adjustments
KalobTaulien Apr 30, 2020
389fdeb
Adding debug statements for easier local development
KalobTaulien Apr 30, 2020
04d368e
Documentation updates
KalobTaulien Apr 30, 2020
b39d5e7
Local debugging support
KalobTaulien Apr 30, 2020
04c70f1
Example integrations
KalobTaulien Apr 30, 2020
0fd2315
Better mapping support
KalobTaulien Apr 30, 2020
780b199
Dev notes
KalobTaulien Apr 30, 2020
3685865
Page dictionary support for unique column_name to field_name mapping
KalobTaulien May 1, 2020
d4ee236
Moved logic to outside method
KalobTaulien May 1, 2020
9091280
Moved private method down as setup method is more important
KalobTaulien May 1, 2020
5fe5370
Added docs around the EXTRA_SUPPORTED_MODELS setting
KalobTaulien May 1, 2020
6352d19
Cleanup & Airtable best practices doc
KalobTaulien May 4, 2020
7ca0098
Added a known issue
KalobTaulien May 4, 2020
8c918f4
Somewhat handle multiple returned records
KalobTaulien May 4, 2020
69c4ebf
Auto-register airtable import admin url
KalobTaulien May 4, 2020
0fc0ccb
Support additional model import in the template
KalobTaulien May 4, 2020
b045538
ctrl+z; undoing last commit as it was a bad idea
KalobTaulien May 4, 2020
3303508
Optional setting: disable airtable importing on pecific models
KalobTaulien May 4, 2020
22b04ed
Adding api response memoization
KalobTaulien May 4, 2020
e88aa38
Docs and extra model support
KalobTaulien May 4, 2020
a415f22
Resets all airtable record ids from management command
KalobTaulien May 5, 2020
6e5f043
Small fixes
KalobTaulien May 5, 2020
b3c7e57
Moved testing const to mixin file
KalobTaulien May 5, 2020
36416e3
Try/catch a failed Airtable request
KalobTaulien May 6, 2020
f726dbb
Remove cache purge line
KalobTaulien May 6, 2020
4e3ab70
Add Airtable record to object when creating a new record
KalobTaulien May 6, 2020
d89ec15
Auto increment model pks when creating new model from an import
KalobTaulien May 6, 2020
33ffc17
Better json to dict evaluation
KalobTaulien May 7, 2020
57973dd
Properly create new models from Airtable withour throwing an Integrit…
KalobTaulien May 7, 2020
a641d80
Use POST instead of GET requests
KalobTaulien May 7, 2020
93e996f
Default message when no models are configured
KalobTaulien May 12, 2020
ce28580
Hide Import menu option when disabled
KalobTaulien May 12, 2020
d11bf42
Django messages when page is saved w/ or w/o airtable record update
KalobTaulien May 12, 2020
4a6e32a
Future Snippet hook support
KalobTaulien May 12, 2020
34be856
Mock Airtable Client
KalobTaulien May 14, 2020
57ab574
Ignore common files and dirs
KalobTaulien May 14, 2020
b1b118b
View, Model and Mixin tests
KalobTaulien May 14, 2020
c34b157
Refactoring import command as separate class; writing tests for the n…
KalobTaulien May 15, 2020
5bf118c
Import management command tests
KalobTaulien May 18, 2020
bbc16a4
Cleanup + Black formatting
KalobTaulien May 18, 2020
30cbb4e
Flake8 formatting (sans E501's from Black formatting)
KalobTaulien May 18, 2020
73f9362
Check is Mixin is enabled before creating ERROR/SUCCESS messages
KalobTaulien May 18, 2020
5332460
Animated preview and test docs
KalobTaulien May 18, 2020
a6fac29
API key documentation line
KalobTaulien May 19, 2020
d1f6f67
Reusable test dictionaries; fixed a test
KalobTaulien May 19, 2020
ed94594
Misc. updates from code review
KalobTaulien May 20, 2020
b434f79
Removed EXTRA_SUPPORTED_MODELS in favour of re-using dictionaries
KalobTaulien May 20, 2020
5a1d340
Moving common logic into util functions
KalobTaulien May 20, 2020
26286ce
Better way to save models
KalobTaulien May 20, 2020
7d710ef
Use Importer and util functions instead of the call call_command
KalobTaulien May 20, 2020
8223804
Util tests
KalobTaulien May 20, 2020
b1aacb8
ImproperlyConfigured exception
KalobTaulien May 20, 2020
0787f8a
'None' => None
KalobTaulien May 20, 2020
6b6f647
m2m support; renamed obj to instance
KalobTaulien May 20, 2020
6bde27d
Wording update
KalobTaulien May 20, 2020
57d52b4
Removed single record testing condition
KalobTaulien May 20, 2020
46b2dc6
Using empty str helps prevent default IntegrityErrors on non null tex…
KalobTaulien May 21, 2020
e8253d4
Default to False
KalobTaulien May 21, 2020
0d30697
Default AIRTABLE to False in hooks
KalobTaulien May 21, 2020
6b1ca24
fixup! Default AIRTABLE to False in hooks
KalobTaulien May 21, 2020
149859d
Group models with the same settings together
KalobTaulien May 21, 2020
ca98b9b
Review touchups and flake8
KalobTaulien May 22, 2020
9a795c4
Black formatting
KalobTaulien May 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,6 @@ def test_debug_message(self):
debug_message = importer.debug_message(text)
self.assertEqual(debug_message, None)

def test_get_validated_models_with_invalid_model(self):
models = ["fake.ModelName"]
importer = Importer(models=models)
with self.assertRaises(CommandError) as context:
importer.get_validated_models()
self.assertEqual("'fake.modelname' is not recognised as a model name.", str(context.exception))

def test_get_validated_models_with_single_valid_model(self):
models = ["tests.Advert"]
importer = Importer(models=models)
models = importer.get_validated_models()
# `SimilarToAdvert` is added because it's an EXTRA_SUPPORTED_MODEL for `Advert`
self.assertListEqual(models, [Advert])

def test_get_validated_models_with_multiple_valid_models(self):
models = ["tests.Advert", "tests.SimplePage", "tests.SimilarToAdvert"]
importer = Importer(models=models)
models = importer.get_validated_models()
# `SimilarToAdvert` is added because it's an EXTRA_SUPPORTED_MODEL for `Advert`
self.assertListEqual(models, [Advert, SimplePage, SimilarToAdvert])

def test_get_model_for_path(self):
importer = Importer(models=[])
advert_model = importer.get_model_for_path("tests.Advert")
self.assertEqual(advert_model, Advert)
simple_page = importer.get_model_for_path("tests.SimplePage")
self.assertEqual(simple_page, SimplePage)
with self.assertRaises(ObjectDoesNotExist) as context:
bad_model_path = importer.get_model_for_path("tests.BadModelPathName")
self.assertEqual("ContentType matching query does not exist.", str(context.exception))

def test_get_model_serializer(self):
importer = Importer(models=["tests.Advert"])
advert_serializer = importer.get_model_serializer("tests.serializers.AdvertSerializer")
Expand Down
41 changes: 41 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.core.exceptions import ObjectDoesNotExist
from django.core.management.base import CommandError
from django.test import TestCase

from wagtail_airtable.utils import get_model_for_path, get_all_models, get_validated_models

from tests.models import Advert, ModelNotUsed, SimilarToAdvert, SimplePage
from tests.serializers import AdvertSerializer



class TestUtilFunctions(TestCase):

def test_get_model_for_path(self):
advert_model = get_model_for_path("tests.Advert")
self.assertEqual(advert_model, Advert)
simple_page = get_model_for_path("tests.SimplePage")
self.assertEqual(simple_page, SimplePage)
with self.assertRaises(ObjectDoesNotExist) as context:
bad_model_path = get_model_for_path("tests.BadModelPathName")
self.assertEqual("ContentType matching query does not exist.", str(context.exception))

def test_get_validated_models_with_single_valid_model(self):
models = ["tests.Advert"]
models = get_validated_models(models=models)
self.assertListEqual(models, [Advert])

def test_get_validated_models_with_multiple_valid_models(self):
models = ["tests.Advert", "tests.SimplePage", "tests.SimilarToAdvert"]
models = get_validated_models(models=models)
self.assertListEqual(models, [Advert, SimplePage, SimilarToAdvert])

def test_get_validated_models_with_invalid_model(self):
models = ["fake.ModelName"]
with self.assertRaises(CommandError) as context:
get_validated_models(models=models)
self.assertEqual("'fake.ModelName' is not recognised as a model name.", str(context.exception))

def test_get_all_models(self):
available_models = get_all_models()
self.assertListEqual(available_models, [SimplePage, Advert, SimilarToAdvert])
11 changes: 6 additions & 5 deletions wagtail_airtable/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.core.management.base import CommandError


# TODO write tests for these
def get_model_for_path(model_path):
"""
Given an 'app_name.model_name' string, return the model class
Expand All @@ -15,21 +16,21 @@ def get_model_for_path(model_path):
app_label, model_name
).model_class()

def get_all_models() -> set:
def get_all_models() -> list:
"""
Get's all models from settings.AIRTABLE_IMPORT_SETTINGS.
KalobTaulien marked this conversation as resolved.
Show resolved Hide resolved

Returns a set (unique and unordered list of models)
Returns a list of models.
"""
airtable_settings = getattr(settings, "AIRTABLE_IMPORT_SETTINGS", {})
validated_models = set()
validated_models = []
for label, model_settings in airtable_settings.items():
if model_settings.get("AIRTABLE_IMPORT_ALLOWED", True):
label = label.lower()
if "." in label:
try:
model = get_model_for_path(label)
validated_models.add(model)
validated_models.append(model)
except ObjectDoesNotExist:
raise CommandError(
"%r is not recognised as a model name." % label
Expand Down