Skip to content

Commit

Permalink
minor changes to documentation to fix errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
peteretep authored and clintonb committed Jul 1, 2019
1 parent 45a29bb commit e4a8c62
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 60 deletions.
3 changes: 2 additions & 1 deletion docs/about/roadmap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ potentially *gross*. (Then again, if it's gross underneath but exposes a
pleasant UI, that's not ideal, but it's OK.)

The other big sticking point is that this won't be a simple ``ALTER
TABLE wafle_flag ADD COLUMN`` upgrade; things will break.
TABLE wafle_flag ADD COLUMN`` upgrade; things will break.

I've been thinking what Waffle would be like if I designed it from
scratch today with slightly different goals, like extensibility. Beyond
Expand All @@ -126,3 +126,4 @@ kind of overhaul.
.. _0.12: https://github.com/django-waffle/django-waffle/milestones/0.12
.. _0.13: https://github.com/django-waffle/django-waffle/milestones/0.13
.. _Gargoyle: https://github.com/disqus/gargoyle
.. _django-jinja: https://niwinz.github.io/django-jinja/latest/
113 changes: 57 additions & 56 deletions docs/types/flag.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ are in the group *or* if they are in the 12%.
Users are assigned randomly when using Percentages, so in practice
the actual proportion of users for whom the Flag is active will
probably differ slightly from the Percentage value.


.. _types-flag-custom-model:

Custom Flag Models
======================

For many cases, the default Flag model provides all the necessary functionality. It allows
flagging individual ``User``s and ``Group``s. If you would like flags to be applied to
flagging individual Users and Groups. If you would like flags to be applied to
different things, such as companies a User belongs to, you can use a custom flag model.

The functionality uses the same concepts as Django's custom user models, and a lot of this will
Expand All @@ -118,64 +118,65 @@ been imported directly.

Example:

```python
# settings.py
WAFFLE_FLAG_MODEL = 'myapp.Flag'
# models.py
class Flag(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(Flag, self).get_flush_keys(flush_keys)
companies_cache_key = get_setting(Flag.FLAG_COMPANIES_CACHE_KEY, Flag.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(Flag, 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(Flag.FLAG_COMPANIES_CACHE_KEY, Flag.FLAG_COMPANIES_CACHE_KEY_DEFAULT),
self.name
)
cached = cache.get(cache_key)
if cached == CACHE_EMPTY:
return set()
if cached:
return cached
.. code-block:: python
company_ids = set(self.companies.all().values_list('pk', flat=True))
if not company_ids:
cache.add(cache_key, CACHE_EMPTY)
return set()
# settings.py
WAFFLE_FLAG_MODEL = 'myapp.Flag'
cache.add(cache_key, company_ids)
return company_ids
# models.py
class Flag(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.'),
)
# admin.py
from waffle.admin import FlagAdmin as WaffleFlagAdmin
def get_flush_keys(self, flush_keys=None):
flush_keys = super(Flag, self).get_flush_keys(flush_keys)
companies_cache_key = get_setting(Flag.FLAG_COMPANIES_CACHE_KEY, Flag.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(Flag, 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(Flag.FLAG_COMPANIES_CACHE_KEY, Flag.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
# admin.py
from waffle.admin import FlagAdmin as WaffleFlagAdmin
class FlagAdmin(WaffleFlagAdmin):
raw_id_fields = tuple(list(WaffleFlagAdmin.raw_id_fields) ['companies'])
admin.site.register(Flag, FlagAdmin)
class FlagAdmin(WaffleFlagAdmin):
raw_id_fields = tuple(list(WaffleFlagAdmin.raw_id_fields) ['companies'])
admin.site.register(Flag, FlagAdmin)
```
.. _types-flag-testing:
Expand Down
6 changes: 3 additions & 3 deletions docs/types/sample.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ sample returns the :ref:`WAFFLE_SAMPLE_DEFAULT <starting-configuring>`
value but does not create a sample in the database. If you'd like
waffle to create missing samples in the database whenever it
encounters a missing sample you can set
:ref:`WAFFLE_CREATE_MISSING_SSAMPLES <starting-configuring>` to
``True``. If :ref:`WAFFLE_SAMPLE_DEFAULT` is ``True`` then the
:ref:`WAFFLE_CREATE_MISSING_SAMPLES <starting-configuring>` to
``True``. If :ref:`WAFFLE_SAMPLE_DEFAULT <starting-configuring>` is ``True`` then the
``Percent`` attribute of the sample will be created as 100.0 (so that
when the sample is checked it always evalues to
when the sample is checked it always evaluates to
``True``). Otherwise the value will be set to 0.0 so that the sample
always evaluates to ``False``.

Expand Down

0 comments on commit e4a8c62

Please sign in to comment.