-
Notifications
You must be signed in to change notification settings - Fork 17
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 #152
Merged
+361
−22
Merged
Feature/55 #152
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1c154c8
feature/55 first approach to campaigns
juan-ignacio-sanchez 025636c
feature/55 fix flake8 style
juan-ignacio-sanchez 5586d28
feature/55 add python3.5 compat
juan-ignacio-sanchez cc2d5a1
remove previous migrations and refactor campaings/drip models
juan-ignacio-sanchez 601f666
refactor drip/campaign models
juan-ignacio-sanchez 451c07b
fix campaign creation tests
juan-ignacio-sanchez 8a62cfd
remove unused variable
juan-ignacio-sanchez 22189ac
allow the user to control wheter to remove or not the drips attached …
juan-ignacio-sanchez 765716f
add unittest and missing migration
juan-ignacio-sanchez ab549ec
add DripInline to CampaignAdmin
juan-ignacio-sanchez c5a2a99
fix style to match pep8
juan-ignacio-sanchez d15bcbe
Fix/upgrade migrations in models
jumcorredorro 6b3ddbb
Change tests to pytest
jumcorredorro 43d7a3e
Add timeline for campaign
jumcorredorro 7d00ce3
Change version library
jumcorredorro aa598e1
Fix factory Drip to sentences, making easy the uniqueness
jumcorredorro 8a54e40
Remove unused default in campaign name
jumcorredorro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
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,82 @@ | ||
import json | ||
from typing import Any, Callable, Dict, List, Optional, Set, Union | ||
|
||
from django.contrib import admin | ||
from django.core.handlers.wsgi import WSGIRequest | ||
from django.http import HttpResponse | ||
from django.shortcuts import get_object_or_404, render | ||
from django.urls import URLPattern, path | ||
|
||
from campaigns.models import Campaign | ||
from drip.models import Drip | ||
from drip.utils import get_simple_fields, get_user_model | ||
|
||
User = get_user_model() | ||
|
||
|
||
class DripInline(admin.TabularInline): | ||
model = Drip | ||
|
||
|
||
class CampaignAdmin(admin.ModelAdmin): | ||
inlines = [ | ||
DripInline, | ||
] | ||
users_fields: Union[str, List[str]] = [] | ||
|
||
def av(self, view: Callable) -> Callable: | ||
return self.admin_site.admin_view(view) | ||
|
||
def timeline( | ||
self, | ||
request: WSGIRequest, | ||
drip_id: int, | ||
into_past: int, | ||
into_future: int, | ||
) -> HttpResponse: | ||
""" | ||
Return a list of people who should get emails. | ||
""" | ||
|
||
campaign = get_object_or_404(Campaign, id=drip_id) | ||
|
||
shifted_drips = [] | ||
for drip in campaign.drip_set.all(): | ||
seen_users: Set[int] = set() | ||
for shifted_drip in drip.drip.walk(into_past=int(into_past), into_future=int(into_future) + 1): | ||
shifted_drip.prune() | ||
shifted_drips.append( | ||
{ | ||
"drip_model": drip, | ||
"drip": shifted_drip, | ||
"qs": shifted_drip.get_queryset().exclude( | ||
id__in=seen_users, | ||
), | ||
}, | ||
) | ||
seen_users.update(shifted_drip.get_queryset().values_list("id", flat=True)) | ||
|
||
return render(request, "campaign/timeline.html", locals()) | ||
|
||
def build_extra_context(self, extra_context: Optional[Dict[str, Any]]) -> Dict[str, Any]: | ||
extra_context = extra_context or {} | ||
User = get_user_model() | ||
if not self.users_fields: | ||
self.users_fields = json.dumps(get_simple_fields(User)) | ||
extra_context["field_data"] = self.users_fields | ||
return extra_context | ||
|
||
def get_urls(self) -> List[URLPattern]: | ||
urls = super(CampaignAdmin, self).get_urls() | ||
my_urls = [ | ||
path( | ||
"<int:drip_id>/timeline/<int:into_past>/<int:into_future>/", | ||
self.av(self.timeline), | ||
name="campaign_drip_timeline", | ||
), | ||
] | ||
|
||
return my_urls + urls | ||
|
||
|
||
admin.site.register(Campaign, CampaignAdmin) |
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,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CampaignsConfig(AppConfig): | ||
name = "campaigns" |
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,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)), | ||
], | ||
), | ||
] |
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,18 @@ | ||
# Generated by Django 3.2.14 on 2022-07-07 08:03 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('campaigns', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='campaign', | ||
name='name', | ||
field=models.CharField(max_length=256), | ||
), | ||
] |
Empty file.
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,11 @@ | ||
from django.db import models | ||
|
||
|
||
class Campaign(models.Model): | ||
name = models.CharField(max_length=256) | ||
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) |
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 @@ | ||
{% extends "admin/change_form.html" %} {% load i18n static admin_list admin_urls %} {% block object-tools-items %} | ||
<li> | ||
<a href="{% url 'admin:campaign_drip_timeline' original.id 4 7 %}" class="" | ||
>View Timeline</a | ||
> | ||
</li> | ||
<li> | ||
<a href="{% url 'admin:campaigns_campaign_history' original.pk %}" class="historylink">{% translate "History" %}</a> | ||
</li> | ||
{% if has_absolute_url %} | ||
<li> | ||
<a | ||
href="../../../r/{{ content_type_id }}/{{ object_id }}/" | ||
class="viewsitelink" | ||
>{% trans "View on site" %}</a | ||
> | ||
</li> | ||
{% endif%} | ||
{% endblock %} |
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,21 @@ | ||
{% extends "admin/base_site.html" %} | ||
{% load i18n static admin_modify %} | ||
{% load admin_urls %} | ||
|
||
{% block title %}Viewing Timeline for {{ drip.name }}{% endblock title %} | ||
|
||
{% block breadcrumbs %}{% endblock %} | ||
|
||
{% block content %} | ||
<h1>{{ drip.name }} Schedule:</h1> | ||
|
||
<div class="content-main"> | ||
<ul>{% for pack in shifted_drips %} | ||
<li><strong>{% if pack.drip.now_shift_kwargs.days != 0 %}{{ pack.drip.now }}{% else %}today!{% endif %}</strong>{% if pack.qs %} | ||
<ul>{% for user in pack.qs %}{% if user.email %} | ||
<li><a href="{% url 'admin:drip_drip_change' pack.drip_model.pk %}">Drip ({{pack.drip_model}})</a> {{ user.email }} - {{ user.id }} - <a href="{% url 'admin:view_drip_email' drip_id into_past into_future user.id %}">view email</a></li> | ||
{% endif %}{% endfor %}</ul> | ||
{% endif %}</li> | ||
{% endfor %}</ul> | ||
</div> | ||
{% endblock content %} |
Empty file.
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,22 @@ | ||
from factory import Faker, SubFactory | ||
from factory.django import DjangoModelFactory | ||
|
||
from campaigns.models import Campaign | ||
from drip.models import Drip | ||
|
||
|
||
class CampaignFactory(DjangoModelFactory): | ||
name = Faker("word") | ||
delete_drips = Faker("pybool") | ||
|
||
class Meta: | ||
model = Campaign | ||
|
||
|
||
class DripFactory(DjangoModelFactory): | ||
name = Faker("sentences", nb=3) | ||
campaign = SubFactory(CampaignFactory) | ||
|
||
class Meta: | ||
model = Drip | ||
django_get_or_create = ("name",) |
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,43 @@ | ||
import random | ||
from typing import Any, List | ||
|
||
import pytest | ||
|
||
from campaigns.models import Campaign | ||
from campaigns.tests.factories import CampaignFactory, DripFactory | ||
from drip.models import Drip | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def _drips_generator(amount: int, **drip_extra_args: Any) -> List[Campaign]: | ||
return DripFactory.create_batch(amount, **drip_extra_args) | ||
|
||
|
||
class TestCaseCampaign: | ||
def test_campaings_creation(self): | ||
campaign = CampaignFactory() | ||
drip_count = random.randint(5, 15) | ||
_drips_generator(drip_count, campaign=campaign) | ||
|
||
assert len(campaign.drip_set.all()) == drip_count | ||
|
||
@pytest.mark.parametrize( | ||
("delete_drips, drip_count, drip_count_after_delete"), | ||
( | ||
(True, 10, 0), | ||
(False, 10, 10), | ||
), | ||
) | ||
def test_remove_campaings( | ||
self, | ||
delete_drips: bool, | ||
drip_count: int, | ||
drip_count_after_delete: int, | ||
): | ||
campaign = CampaignFactory(delete_drips=delete_drips) | ||
_drips_generator(drip_count, campaign=campaign) | ||
|
||
campaign.delete() | ||
|
||
assert Drip.objects.count() == drip_count_after_delete |
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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "1.13.8" | ||
__version__ = "1.14.0" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We aren't indicating the type of the variables, you can remove this if you want. Not mandatory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I leave like this because of mypy.