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

feature/55 first approach to campaigns #92

Closed
wants to merge 12 commits into from
Closed
Empty file added campaigns/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions campaigns/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.contrib import admin
from campaigns.models import Campaign
from drip.models import Drip


class DripInline(admin.TabularInline):
model = Drip


class CampaignAdmin(admin.ModelAdmin):
inlines = [
DripInline,
]


admin.site.register(Campaign, CampaignAdmin)
5 changes: 5 additions & 0 deletions campaigns/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CampaignsConfig(AppConfig):
name = 'campaigns'
22 changes: 22 additions & 0 deletions campaigns/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.0.7 on 2020-12-30 21:11

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Campaign',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(default='Unnamed Campaign', max_length=256)),
('delete_drips', models.BooleanField(default=True)),
],
),
]
Empty file.
11 changes: 11 additions & 0 deletions campaigns/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models


class Campaign(models.Model):
name = models.CharField(max_length=256, default='Unnamed Campaign')
delete_drips = models.BooleanField(default=True)

def delete(self, using=None, keep_parents=False):
if self.delete_drips:
self.drip_set.all().delete()
super().delete(using, keep_parents)
65 changes: 65 additions & 0 deletions campaigns/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from django.test import TestCase
from drip.models import Drip
from campaigns.models import Campaign

DRIP_AMOUNT = 10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use random.randint. Just a suggestion



def _drips_generator(amount, **drip_extra_args):
for i in range(amount):
yield Drip(
name='{}th drip'.format(i),
**drip_extra_args
)


class DripsTestCase(TestCase):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test also using a class different from Drip, to test the GenericForeignKey part


def test_campaings_creation(self):
campaign = Campaign()
campaign.save()

Drip.objects.bulk_create(
_drips_generator(
DRIP_AMOUNT,
campaign=campaign
)
)

assert len(campaign.drip_set.all()) == DRIP_AMOUNT

def test_remove_campaings(self):
campaign_delete_drips = Campaign(name='remove me!', delete_drips=True)
campaign_delete_drips.save()

Drip.objects.bulk_create(
_drips_generator(
DRIP_AMOUNT,
campaign=campaign_delete_drips
)
)

drips_before_delete = Drip.objects.count()

campaign_delete_drips.delete()

assert drips_before_delete > Drip.objects.count()

campaign_do_not_delete_drips = Campaign(
name='remove me, but keep my drips alive!',
delete_drips=False,
)
campaign_do_not_delete_drips.save()

Drip.objects.bulk_create(
_drips_generator(
DRIP_AMOUNT,
campaign=campaign_do_not_delete_drips
)
)

drips_before_delete = Drip.objects.count()

campaign_do_not_delete_drips.delete()

assert drips_before_delete == Drip.objects.count()
2 changes: 1 addition & 1 deletion drip/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.9.3'
__version__ = '1.10.3'
20 changes: 20 additions & 0 deletions drip/migrations/0003_drip_campaign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.0.7 on 2020-12-30 22:10

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


class Migration(migrations.Migration):

dependencies = [
('campaigns', '0001_initial'),
('drip', '0002_querysetrule_rule_type'),
]

operations = [
migrations.AddField(
model_name='drip',
name='campaign',
field=models.ForeignKey(blank=True, help_text='If set, this is the campaign to which this Drip belongs to.', null=True, on_delete=django.db.models.deletion.CASCADE, to='campaigns.Campaign'),
),
]
20 changes: 20 additions & 0 deletions drip/migrations/0004_auto_20201230_2223.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.0.7 on 2020-12-30 22:23

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


class Migration(migrations.Migration):

dependencies = [
('campaigns', '0001_initial'),
('drip', '0003_drip_campaign'),
]

operations = [
migrations.AlterField(
model_name='drip',
name='campaign',
field=models.ForeignKey(blank=True, default=None, help_text='If set, this is the campaign to which this Drip belongs to.', null=True, on_delete=django.db.models.deletion.SET_DEFAULT, to='campaigns.Campaign'),
),
]
9 changes: 9 additions & 0 deletions drip/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class AbstractDrip(models.Model):
max_length=120, blank=True, default='default'
)

campaign = models.ForeignKey(
'campaigns.Campaign',
null=True,
blank=True,
default=None,
on_delete=models.SET_DEFAULT,
help_text='If set, this is the campaign to which this Drip belongs to.'
)

class Meta:
abstract = True

Expand Down
3 changes: 3 additions & 0 deletions testsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'django.contrib.messages',

'drip',
'campaigns',

# testing only
'credits',
Expand Down Expand Up @@ -68,3 +69,5 @@
STATICFILES_DIRS = ()

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

DRIP_CLASS_NAME = 'drip.Drip'