Skip to content

Commit

Permalink
tidy plugin settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jscott1989 committed Nov 29, 2016
1 parent a0e428d commit 9501f7b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 43 deletions.
63 changes: 24 additions & 39 deletions src/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from happening.configuration import attach_to_form
from happening.configuration import save_variables
from .forms import ConfigurationForm, ThemeForm, SocialAppForm
from happening import plugins as happening_plugins
from payments.models import PaymentHandler
from django.db import transaction
from .forms import PaymentHandlerForm
Expand All @@ -28,55 +27,41 @@ def index(request):
return render(request, "admin/index.html")


def format_plugin(plugin_id, plugin):
"""Return a single formatted plugin.
Format is (id, name, enabled)
"""
enabled = False

preference = PluginSetting.objects.filter(plugin_name=plugin_id).first()
if preference:
enabled = preference.enabled

return (plugin_id, plugin.Plugin.name, plugin.Plugin.__doc__, enabled)


def save_plugins(request, plugins):
"""Save plugin preferences to the database."""
for plugin_id in list(plugins.keys()):
preference, _ = PluginSetting.objects.get_or_create(
plugin_name=plugin_id)
preference.enabled = False
if plugin_id + "_plugin" in request.POST:
preference.enabled = True
preference.save()

# Then clear the cache
happening_plugins.enabled_plugins_cache = None
messages.success(request,
"Your plugin settings have been saved")
return redirect("plugins")


@admin_required
def plugins(request):
"""Plugin settings."""
if request.method == "POST":
# Save the plugins
for plugin in settings.PLUGINS:
preference, _ = PluginSetting.objects.get_or_create(
plugin_name=plugin)
preference.enabled = False
if plugin + "_plugin" in request.POST:
preference.enabled = True
preference.save()

messages.success(request,
"Your plugin settings have been saved")
return redirect("plugins")

plugins = {}

for plugin in settings.PLUGINS:
p = importlib.import_module(plugin)
plugins[plugin] = p
enabled = False

if request.method == "POST":
# Save the plugins
return save_plugins(request, plugins)
preference = PluginSetting.objects.filter(plugin_name=plugin).first()
if preference:
enabled = preference.enabled

formatted_plugins = [
format_plugin(p_id, plugin) for p_id, plugin in list(plugins.items())]
plugins[plugin] = {
"id": plugin,
"name": p.Plugin.name,
"description": p.Plugin.__doc__,
"enabled": enabled}

return render(request, "admin/plugins.html",
{"plugins": formatted_plugins})
{"plugins": plugins.values()})


@admin_required
Expand Down
2 changes: 2 additions & 0 deletions src/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,15 @@ def member_uncancelled_tickets(member):
"""List the number of tickets not cancelled."""
return member.tickets.filter(cancelled=False).count()


User.uncancelled_tickets = member_uncancelled_tickets


def member_attended_tickets(member):
"""List the number of attended events."""
return member.tickets.filter(cancelled=False, checked_in=True).count()


User.attended_tickets = member_attended_tickets


Expand Down
1 change: 1 addition & 0 deletions src/happening/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def follow_object_code(user, obj, role):
role,
user.pk))


User.follow = follow
User.unfollow = unfollow
User.is_following = is_following
Expand Down
1 change: 1 addition & 0 deletions src/happening/templatetags/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ def jsonify(object):
return mark_safe(serialize('json', object))
return mark_safe(json.dumps(object))


register.filter('jsonify', jsonify)
jsonify.is_safe = True
1 change: 1 addition & 0 deletions src/happening/templatetags/xextends.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ def do_xextends(parser, token):
# let the orginal do_extends parse the tag, and wrap the ExtendsNode
return XExtendsNode(do_extends(parser, token), kwargs)


register.tag('xextends', do_xextends)
2 changes: 2 additions & 0 deletions src/happening/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def add_site_to_all_models(*args, **kwargs):
del kwargs['site']
return mommy._make(*args, **kwargs)


mommy._make = mommy.make
mommy.make = add_site_to_all_models

Expand All @@ -44,6 +45,7 @@ def create_client(self):
"""Create a test client."""
return Client()


if 'travis' in os.environ:
# We don't use VCRPy on the CI server
# (for some reason it doesn't work anyway...)
Expand Down
7 changes: 7 additions & 0 deletions src/members/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def get_user_name(user):
if user.get_full_name():
return user.get_full_name()
return user.username


User.name = get_user_name


Expand Down Expand Up @@ -115,6 +117,8 @@ def close_account(user):
[e.delete() for e in user.socialaccount_set.all()]

user.save()


User.close_account = close_account


Expand All @@ -133,6 +137,8 @@ def __str__(self):
def member_can_add_tag(member):
"""Can a member add new tags."""
return member.tags.count() < Tag.objects.all().count()


User.can_add_tag = member_can_add_tag


Expand All @@ -145,6 +151,7 @@ def apply_session_tags(sender, user, request, **kwargs):
user.tags.add(t)
request.session["tags"] = []


user_logged_in.connect(apply_session_tags)


Expand Down
8 changes: 4 additions & 4 deletions src/templates/admin/plugins.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ <h2 class="block__header-text">Plugins</h2>
</tr>
</thead>
<tbody>
{% for plugin_id, plugin_name, plugin_description, enabled in plugins %}
{% for plugin in plugins %}
<tr>
<td>
<strong>{{plugin_name}}</strong>
<p>{{plugin_description}}</p>
<strong>{{plugin.name}}</strong>
<p>{{plugin.description}}</p>
</td>
<td>
<input name="{{plugin_id}}_plugin" type="checkbox" {% if enabled %}checked{% endif %}/>
<input name="{{plugin.id}}_plugin" type="checkbox" {% if plugin.enabled %}checked{% endif %}/>
</td>
</tr>
{% endfor %}
Expand Down

0 comments on commit 9501f7b

Please sign in to comment.