forked from jazzband/django-waffle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
601 additions
and
264 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Generated by Django 2.0.4 on 2018-04-26 07:42 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Company', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=100)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='CompanyAwareFlag', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(help_text='The human/computer readable name.', max_length=100, unique=True, verbose_name='Name')), | ||
('everyone', models.NullBooleanField(help_text='Flip this flag on (Yes) or off (No) for everyone, overriding all other settings. Leave as Unknown to use normally.', verbose_name='Everyone')), | ||
('percent', models.DecimalField(blank=True, decimal_places=1, help_text='A number between 0.0 and 99.9 to indicate a percentage of users for whom this flag will be active.', max_digits=3, null=True, verbose_name='Percent')), | ||
('testing', models.BooleanField(default=False, help_text='Allow this flag to be set for a session for user testing', verbose_name='Testing')), | ||
('superusers', models.BooleanField(default=True, help_text='Flag always active for superusers?', verbose_name='Superusers')), | ||
('staff', models.BooleanField(default=False, help_text='Flag always active for staff?', verbose_name='Staff')), | ||
('authenticated', models.BooleanField(default=False, help_text='Flag always active for authenticated users?', verbose_name='Authenticated')), | ||
('languages', models.TextField(blank=True, default='', help_text='Activate this flag for users with one of these languages (comma-separated list)', verbose_name='Languages')), | ||
('rollout', models.BooleanField(default=False, help_text='Activate roll-out mode?', verbose_name='Rollout')), | ||
('note', models.TextField(blank=True, help_text='Note where this Flag is used.', verbose_name='Note')), | ||
('created', models.DateTimeField(db_index=True, default=django.utils.timezone.now, help_text='Date when this Flag was created.', verbose_name='Created')), | ||
('modified', models.DateTimeField(default=django.utils.timezone.now, help_text='Date when this Flag was last modified.', verbose_name='Modified')), | ||
('companies', models.ManyToManyField(blank=True, help_text='Activate this flag for these companies.', to='test_app.Company')), | ||
('groups', models.ManyToManyField(blank=True, help_text='Activate this flag for these user groups.', to='auth.Group', verbose_name='Groups')), | ||
('users', models.ManyToManyField(blank=True, help_text='Activate this flag for these users.', to=settings.AUTH_USER_MODEL, verbose_name='Users')), | ||
], | ||
options={ | ||
'verbose_name': 'Flag', | ||
'verbose_name_plural': 'Flags', | ||
'abstract': False, | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='CompanyUser', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('password', models.CharField(max_length=128, verbose_name='password')), | ||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), | ||
('username', models.CharField(max_length=100)), | ||
('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.Company')), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
), | ||
] |
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,73 @@ | ||
from django.contrib.auth.base_user import AbstractBaseUser | ||
from django.db import models | ||
from django.db.models import CASCADE | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from waffle.models import AbstractUserFlag, CACHE_EMPTY | ||
from waffle.utils import get_setting, keyfmt, get_cache | ||
|
||
cache = get_cache() | ||
|
||
|
||
class Company(models.Model): | ||
name = models.CharField( | ||
max_length=100, | ||
) | ||
|
||
|
||
class CompanyUser(AbstractBaseUser): | ||
company = models.ForeignKey( | ||
Company, | ||
on_delete=CASCADE | ||
) | ||
|
||
username = models.CharField( | ||
max_length=100, | ||
) | ||
|
||
|
||
class CompanyAwareFlag(AbstractUserFlag): | ||
FLAG_COMPANIES_CACHE_KEY = 'FLAG_COMPANIES_CACHE_KEY' | ||
FLAG_COMPANIES_CACHE_KEY_DEFAULT = 'flag:%s:companies' | ||
|
||
companies = models.ManyToManyField( | ||
Company, | ||
blank=True, | ||
help_text=_('Activate this flag for these companies.'), | ||
) | ||
|
||
def get_flush_keys(self, flush_keys=None): | ||
flush_keys = super(CompanyAwareFlag, self).get_flush_keys(flush_keys) | ||
companies_cache_key = get_setting(CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY, | ||
CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY_DEFAULT) | ||
flush_keys.append(keyfmt(companies_cache_key, self.name)) | ||
return flush_keys | ||
|
||
def is_active_for_user(self, user): | ||
is_active = super(CompanyAwareFlag, self).is_active_for_user(user) | ||
if is_active: | ||
return is_active | ||
|
||
if getattr(user, 'company_id', None): | ||
company_ids = self._get_company_ids() | ||
if user.company_id in company_ids: | ||
return True | ||
|
||
def _get_company_ids(self): | ||
cache_key = keyfmt( | ||
get_setting(CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY, CompanyAwareFlag.FLAG_COMPANIES_CACHE_KEY_DEFAULT), | ||
self.name | ||
) | ||
cached = cache.get(cache_key) | ||
if cached == CACHE_EMPTY: | ||
return set() | ||
if cached: | ||
return cached | ||
|
||
company_ids = set(self.companies.all().values_list('pk', flat=True)) | ||
if not company_ids: | ||
cache.add(cache_key, CACHE_EMPTY) | ||
return set() | ||
|
||
cache.add(cache_key, company_ids) | ||
return company_ids |
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
Oops, something went wrong.