This repository was archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Release channels and M2M for country and locale #41
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,8 +1,17 @@ | ||
| import factory | ||
|
|
||
| from normandy.classifier.models import Bundle | ||
| from django.test import RequestFactory | ||
|
|
||
| from normandy.classifier.models import Bundle, Client | ||
|
|
||
|
|
||
| class BundleFactory(factory.Factory): | ||
| class Meta: | ||
| model = Bundle | ||
|
|
||
|
|
||
| class ClientFactory(factory.Factory): | ||
| class Meta: | ||
| model = Client | ||
|
|
||
| request = factory.LazyAttribute(lambda o: RequestFactory().get('/')) | ||
This file contains hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Generated by Django 1.9 on 2016-02-19 01:01 | ||
| from __future__ import unicode_literals | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('recipes', '0015_auto_20160217_1819'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='ReleaseChannel', | ||
| fields=[ | ||
| ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('slug', models.SlugField(max_length=255, unique=True)), | ||
| ('name', models.CharField(max_length=255)), | ||
| ], | ||
| options={ | ||
| 'ordering': ['slug'], | ||
| }, | ||
| ), | ||
| migrations.AddField( | ||
| model_name='recipe', | ||
| name='release_channels', | ||
| field=models.ManyToManyField(blank=True, to='recipes.ReleaseChannel'), | ||
| ), | ||
| ] |
This file contains hidden or 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,55 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Generated by Django 1.9 on 2016-02-18 20:24 | ||
| from __future__ import unicode_literals | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('recipes', '0016_auto_20160219_0101'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='Country', | ||
| fields=[ | ||
| ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('code', models.CharField(max_length=255, unique=True)), | ||
| ('name', models.CharField(max_length=255)), | ||
| ('order', models.IntegerField()), | ||
| ], | ||
| options={ | ||
| 'ordering': ['order', 'name'], | ||
| }, | ||
| ), | ||
| migrations.AddField( | ||
| model_name='locale', | ||
| name='order', | ||
| field=models.IntegerField(default=100), | ||
| preserve_default=False, | ||
| ), | ||
| migrations.RemoveField( | ||
| model_name='recipe', | ||
| name='country', | ||
| ), | ||
| migrations.RemoveField( | ||
| model_name='recipe', | ||
| name='locale', | ||
| ), | ||
| migrations.AddField( | ||
| model_name='recipe', | ||
| name='locales', | ||
| field=models.ManyToManyField(blank=True, to='recipes.Locale'), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='recipe', | ||
| name='countries', | ||
| field=models.ManyToManyField(blank=True, to='recipes.Country'), | ||
| ), | ||
| migrations.AlterModelOptions( | ||
| name='locale', | ||
| options={'ordering': ['order', 'code']}, | ||
| ), | ||
| ] |
This file contains hidden or 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,52 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Generated by Django 1.9 on 2016-02-18 19:03 | ||
| from __future__ import unicode_literals | ||
|
|
||
| from django.db import migrations | ||
|
|
||
| from django_countries import countries | ||
|
|
||
|
|
||
| def add_countries(apps, schema_editor): | ||
| Country = apps.get_model('recipes', 'Country') | ||
| for (code, name) in countries: | ||
| if code == 'US': | ||
| order = 0 | ||
| else: | ||
| order = 100 | ||
|
|
||
| Country.objects.update_or_create(code=code, defaults={ | ||
| 'name': name, | ||
| 'order': order, | ||
| }) | ||
|
|
||
|
|
||
| def remove_countries(apps, schema_editor): | ||
| Country = apps.get_model('recipes', 'Country') | ||
| Country.objects.all().delete() | ||
|
|
||
|
|
||
| def set_locale_sort_order(apps, schema_editor): | ||
| Locale = apps.get_model('recipes', 'Locale') | ||
| try: | ||
| english = Locale.objects.get(code='en-US') | ||
| english.order = 0 | ||
| english.save() | ||
| except Locale.DoesNotExist: | ||
| pass | ||
|
|
||
|
|
||
| def noop(apps, schema_editor): | ||
| pass | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('recipes', '0017_auto_20160218_2024'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(add_countries, remove_countries), | ||
| migrations.RunPython(set_locale_sort_order, noop), | ||
| ] |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ | |
| from django.db import models | ||
|
|
||
| from adminsortable.models import SortableMixin | ||
| from django_countries.fields import CountryField | ||
| from rest_framework.reverse import reverse | ||
|
|
||
| from normandy.recipes import utils | ||
|
|
@@ -21,13 +20,48 @@ class Locale(models.Model): | |
| code = models.CharField(max_length=255, unique=True) | ||
| english_name = models.CharField(max_length=255, blank=True) | ||
| native_name = models.CharField(max_length=255, blank=True) | ||
| order = models.IntegerField(default=100) | ||
|
|
||
| class Meta: | ||
| ordering = ['code'] | ||
| ordering = ['order', 'code'] | ||
|
|
||
| def __str__(self): | ||
| return '{self.code} ({self.english_name})'.format(self=self) | ||
|
|
||
| def matches(self, other): | ||
| return self.code.lower() == other.lower() | ||
|
|
||
|
|
||
| class ReleaseChannel(models.Model): | ||
| """Release channel of Firefox""" | ||
| slug = models.SlugField(max_length=255, unique=True) | ||
| name = models.CharField(max_length=255) | ||
|
|
||
| class Meta: | ||
| ordering = ['slug'] | ||
|
|
||
| def __str__(self): | ||
| return self.name | ||
|
|
||
| def matches(self, other): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit vague what this is matching against; perhaps we pass in the client and it extracts from the client the fields it's matching against?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to change this with the other matching refactors we talked about, since changing this sort of breaks |
||
| return self.slug == other or self.name == other | ||
|
|
||
|
|
||
| class Country(models.Model): | ||
| """Database table for countries from django_countries.""" | ||
| code = models.CharField(max_length=255, unique=True) | ||
| name = models.CharField(max_length=255) | ||
| order = models.IntegerField(default=100) | ||
|
|
||
| class Meta: | ||
| ordering = ['order', 'name'] | ||
|
|
||
| def __str__(self): | ||
| return '{self.name} ({self.code})'.format(self=self) | ||
|
|
||
| def matches(self, other): | ||
| return self.code == other or self.name == other | ||
|
|
||
|
|
||
| class Recipe(models.Model): | ||
| """A set of actions to be fetched and executed by users.""" | ||
|
|
@@ -36,33 +70,41 @@ class Recipe(models.Model): | |
|
|
||
| # Fields that determine who this recipe is sent to. | ||
| enabled = models.BooleanField(default=False) | ||
| locale = models.ForeignKey(Locale, blank=True, null=True) | ||
| country = CountryField(blank=True, null=True, default=None) | ||
| locales = models.ManyToManyField(Locale, blank=True) | ||
| countries = models.ManyToManyField(Country, blank=True) | ||
| start_time = models.DateTimeField(blank=True, null=True, default=None) | ||
| end_time = models.DateTimeField(blank=True, null=True, default=None) | ||
| sample_rate = PercentField(default=100) | ||
| release_channels = models.ManyToManyField(ReleaseChannel, blank=True) | ||
|
|
||
| def log_rejection(self, msg): | ||
| logger.debug('{} rejected: {}'.format(self, msg)) | ||
|
|
||
| def check_many(self, name, field_qs, client_val): | ||
| field_vals = list(field_qs.all()) | ||
|
|
||
| if field_vals == []: | ||
| return True | ||
|
|
||
| for val in field_vals: | ||
| if val.matches(client_val): | ||
| return True | ||
|
|
||
| field_vals_str = ', '.join(str(o) for o in field_vals) | ||
| self.log_rejection('client {name} ({client_val}) does not match recipe ' | ||
| '(choices are {field_vals})' | ||
| .format(name=name, client_val=client_val, field_vals=field_vals_str)) | ||
| return False | ||
|
|
||
| def matches(self, client): | ||
| """ | ||
| Return whether this Recipe should be sent to the given client. | ||
| """ | ||
| # This should be ordered roughly by performance cost | ||
| if not self.enabled: | ||
| self.log_rejection('not enabled') | ||
| return False | ||
|
|
||
| if self.locale and client.locale and self.locale.code.lower() != client.locale.lower(): | ||
| self.log_rejection('recipe locale ({self.locale!r}) != ' | ||
| 'client locale ({client.locale!r})') | ||
| return False | ||
|
|
||
| if self.country and self.country != client.country: | ||
| self.log_rejection('recipe country ({self.country!r}) != ' | ||
| 'client country ({client.country!r})') | ||
| return False | ||
|
|
||
| if self.start_time and self.start_time > client.request_time: | ||
| self.log_rejection('start time not met ({})'.format(self.start_time)) | ||
| return False | ||
|
|
@@ -77,6 +119,15 @@ def matches(self, client): | |
| self.log_rejection('did not match sample') | ||
| return False | ||
|
|
||
| if not self.check_many('locale', self.locales, client.locale): | ||
| return False | ||
|
|
||
| if not self.check_many('country', self.countries, client.country): | ||
| return False | ||
|
|
||
| if not self.check_many('channel', self.release_channels, client.release_channel): | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| def __repr__(self): | ||
|
|
||
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.
Oh man this is awesome