Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Commit

Permalink
refactor subjectpool template naming and locations
Browse files Browse the repository at this point in the history
Making template names consistent and moving subjectpool templates into
their own dedicated dir and namespace. Also refactoring decorator and
template tags usage.
  • Loading branch information
alee committed Mar 23, 2015
1 parent d76a1ef commit e0ddfcf
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 62 deletions.
5 changes: 2 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Django==1.7.6
Django==1.7.7
Fabric==1.10.1
Markdown==2.6.1
Pillow==2.7.0
Expand Down Expand Up @@ -35,7 +35,6 @@ wsgiref==0.1.2
xlrd==0.9.3
xlwt==0.7.5
dealer==2.0.4
uwsgi==2.0.9
uwsgi==2.0.10
# virtualcommons forked django-cas
git+http://git@github.com/virtualcommons/django-cas.git#egg=cas

25 changes: 11 additions & 14 deletions vcweb/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def is_anonymous(user):
return user is None or not user.is_authenticated()


def anonymous_required(view_function=None, redirect_to='core:dashboard'):
def anonymous_required(view_function=None, redirect_to='home'):
return create_user_decorator(view_function, is_anonymous, redirect_to=redirect_to)


Expand All @@ -47,29 +47,26 @@ def is_participant(user):
def group_required(*permission_groups, **kwargs):
"""Requires user membership in at least one of the groups passed in."""
def in_groups(u):
if u.is_authenticated() and (is_participant(u) or is_experimenter(u)):
group_names = [pgroup.value for pgroup in permission_groups]
if u.is_authenticated() and u.is_active:
group_names = [pg.value for pg in permission_groups]
return u.groups.filter(name__in=group_names).exists()
return user_passes_test(in_groups)


def create_user_decorator(view_function, is_valid_user, redirect_to='core:dashboard'):

def decorator(fn):
def create_user_decorator(view_function, is_valid_user, redirect_to='core:login'):
def _decorator(fn):
@wraps(fn)
def _decorated_view(request, *args, **kwargs):
if is_valid_user(request.user):
logger.debug('user was valid: %s', request.user)
user = request.user
if is_valid_user(user):
logger.debug('checked if user %s was valid with %s', user, is_valid_user)
return fn(request, *args, **kwargs)
else:
logger.debug('user was invalid, redirecting to %s', redirect_to)
logger.debug('user %s was deemed invalid with %s, redirecting to %s', user, is_valid_user, redirect_to)
return redirect(redirect_to)

_decorated_view.__name__ = fn.__name__
_decorated_view.__dict__ = fn.__dict__
_decorated_view.__doc__ = fn.__doc__
return _decorated_view

return decorator if view_function is None else decorator(view_function)
return _decorator if view_function is None else _decorator(view_function)


def retry(exceptiontocheck, tries=4, delay=3, backoff=2, logger=None):
Expand Down
53 changes: 30 additions & 23 deletions vcweb/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ class Meta:
ordering = ['title', 'namespace']


class ActivityLog(models.Model):
LogType = Choices('Experimenter', 'Scheduled', 'System')
log_message = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)
# log_type = models.CharField(max_length=64, choices=LogType, default=LogType.System)

def __unicode__(self):
return u"%s - %s" % (self.date_created.strftime("%m-%d-%Y %H:%M"), self.log_message)


class OstromlabFaqEntry(models.Model):
question = models.TextField(help_text=_("FAQ Question"))
answer = models.TextField(help_text=_("FAQ Answer"))
Expand Down Expand Up @@ -1160,7 +1170,7 @@ def initialize_data_values(self, group_parameters=None, participant_parameters=N
defaults=parameter_defaults[parameter])
logger.debug("prdv: %s (%s)", participant_data_value, created)

def log(self, log_message, *args, **kwargs):
def log(self, log_message, log_type=ActivityLog.LogType.System, *args, **kwargs):
if log_message:
message = "%s: %s" % (self, log_message)
logger.debug(message, *args)
Expand Down Expand Up @@ -1422,10 +1432,9 @@ def check_elapsed_time(self):

def all_round_data(self):
# FIXME: figure out a better way to convert these to json that doesn't involve manual remapping of attribute
# names or be consistent so that things on the client side are named
# the same as the server side
# names or be consistent so that things on the client side are named the same as the server side
all_round_data = []
for round_data in self.round_data_set.select_related('round_configuration').reverse():
for round_data in self.round_data_set.select_related('round_configuration').order_by('-pk'):
rc = round_data.round_configuration
all_round_data.append({
'pk': round_data.pk,
Expand Down Expand Up @@ -2114,7 +2123,7 @@ def get_related_group(self):
else:
return None

def log(self, log_message):
def log(self, log_message, log_type=ActivityLog.LogType.System):
if log_message:
logger.debug(log_message)
self.activity_log_set.create(round_configuration=self.current_round, log_message=log_message)
Expand Down Expand Up @@ -2839,14 +2848,6 @@ def to_dict(self, cacheable=True, include_email=False):
return super(Like, self).to_dict(cacheable=cacheable)


class ActivityLog(models.Model):
log_message = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return u"%s - %s" % (self.date_created.strftime("%m-%d-%Y %H:%M"), self.log_message)


class GroupActivityLog(ActivityLog):
group = models.ForeignKey(Group, related_name='activity_log_set')
round_configuration = models.ForeignKey(RoundConfiguration)
Expand Down Expand Up @@ -2996,7 +2997,7 @@ def _experiment_metadata_criteria(self, criteria,
return criteria

def with_attendance(self, attendance, **kwargs):
attendance_key = 'attendance__in' if isinstance(attendance, (list, tuple)) else 'attendance'
attendance_key = 'attendance__in' if isinstance(attendance, (list, set, tuple)) else 'attendance'
criteria = self._experiment_metadata_criteria({attendance_key: attendance}, **kwargs)
return self.filter(**criteria)

Expand Down Expand Up @@ -3242,8 +3243,10 @@ def weekly_schedule_tasks(sender, start=None, **kwargs):
experiment status changes (run or archived), invitations sent
"""
# FIXME add information regarding experiment status changes
invalid_permission_participants = Participant.objects.active().exclude(user__groups__name=PermissionGroup.participant)
invalid_permission_experimenters = Experimenter.objects.filter(user__is_active=True).exclude(user__email__contains=('mailinator.com')).exclude(user__groups__name=PermissionGroup.experimenter)
invalid_permission_participants = Participant.objects.active().exclude(
user__groups__name=PermissionGroup.participant.value)
invalid_permission_experimenters = Experimenter.objects.filter(user__is_active=True).exclude(
user__email__contains=('mailinator.com')).exclude(user__groups__name=PermissionGroup.experimenter.value)

# invalid users who are experimenter as well as participant
participant_users = Participant.objects.active().values_list('user__id', flat=True)
Expand All @@ -3256,10 +3259,14 @@ def weekly_schedule_tasks(sender, start=None, **kwargs):
# Invitations sent in last week
invites_last_week = Invitation.objects.filter(date_created__gt=last_week_datetime).count()

email = create_markdown_email(template="email/audit-email.txt", context={
"invalid_users": invalid_users,
"participants": invalid_permission_participants,
"experimenters": invalid_permission_experimenters,
"signups": signup_last_week, "invites": invites_last_week
}, subject="VCWEB Audit", to_email=[settings.DEFAULT_EMAIL])
mail.get_connection().send_messages([email,])
email = create_markdown_email(template="email/audit-email.txt",
context={
"invalid_users": invalid_users,
"participants": invalid_permission_participants,
"experimenters": invalid_permission_experimenters,
"signups": signup_last_week,
"invites": invites_last_week
},
subject="VCWEB Audit",
to_email=[settings.DEFAULT_EMAIL])
email.send()
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
{{ session_detail.scheduled_end_date.time }}</p>
<p><strong>Location: </strong> {{ session_detail.location }}</p>
<p><strong>Capacity: </strong> {{ session_detail.capacity }}</p>
<p><strong>Waitlist enabled: </strong> {{ session_detail.waitlist }}</p>
</div>
<div class="alert alert-warning">
{% if formset.forms|length > 0 %}
Expand Down
9 changes: 5 additions & 4 deletions vcweb/core/subjectpool/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def experimenter_index(request):
'potentialParticipantsCount': potential_participants_count,
}
form = SessionInviteForm()
return render(request, "experimenter/subject-pool-index.html",
return render(request, "subjectpool/experimenter-index.html",
{"view_model_json": dumps(session_data), "form": form})


Expand Down Expand Up @@ -288,7 +288,7 @@ def manage_participant_attendance(request, pk=None):
formset = attendanceformset(queryset=ParticipantSignup.objects.select_related(
'invitation__participant__user').filter(invitation__in=invitations_sent))

return render(request, 'experimenter/session_detail.html',
return render(request, 'subjectpool/experiment-session-detail.html',
{'session_detail': es, 'formset': formset})


Expand Down Expand Up @@ -350,7 +350,8 @@ def submit_experiment_session_signup(request):

if registered or waitlist:
# Check for any already registered or waitlisted participant signups for the current user
ps = ParticipantSignup.objects.registered_or_waitlisted(invitation__participant=user.participant, invitation__experiment_session__experiment_metadata__pk=experiment_metadata_pk)
ps = ParticipantSignup.objects.registered_or_waitlisted(invitation__participant=user.participant,
experiment_metadata_pk=experiment_metadata_pk)

ps = ps.first() if len(ps) > 0 else ParticipantSignup()

Expand Down Expand Up @@ -428,4 +429,4 @@ def experiment_session_signup(request):
still eligible to participate in future experiments and may receive future invitations for this
experiment."""))

return render(request, "participant/experiment-session-signup.html", {"invitation_list": invitation_list})
return render(request, "subjectpool/experiment-session-signup.html", {"invitation_list": invitation_list})
15 changes: 8 additions & 7 deletions vcweb/core/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% load raven %}
{% url 'about' as about %}
{% url 'home' as home %}
{% url 'contact_form' as contact %}
{% url 'core:dashboard' as dashboard %}
{% url 'core:login' as login %}
{% url 'core:logout' as logout %}
Expand Down Expand Up @@ -42,37 +43,37 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class='navbar-brand {% active request home %}' href='{{ home }}'><img class='pull-left' src='{% static "images/vcweb-logo.png" %}' width='30' height='23' alt='the virtual commons'>virtual commons </a>
<a class='navbar-brand {{ request|active:home }}' href='{{ home }}'><img class='pull-left' src='{% static "images/vcweb-logo.png" %}' width='30' height='23' alt='the virtual commons'>virtual commons </a>
</div>
<div class="collapse navbar-collapse" role='navigation'>
<ul class='nav navbar-nav'>
{% if user.is_authenticated %}
{% if user.participant %}
<li class='{% active_re request participate %}'><a href='{{ participate }}'><i class='fa fa-users'></i> Participate</a></li>
<li class='{{ request|active_re:participate }}'><a href='{{ participate }}'><i class='fa fa-users'></i> Participate</a></li>
{% elif user.experimenter %}
<li class='{% active_re request dashboard %}'><a href='{{ dashboard }}'><i class='fa fa-tachometer'></i> Dashboard</a></li>
<li class='{{ request|active_re:dashboard }}'><a href='{{ dashboard }}'><i class='fa fa-tachometer'></i> Dashboard</a></li>
{% if not user.experimenter.is_demo_experimenter %}
<li class='{% active_re request subjectpool %}'><a href='{{ subjectpool }}'><i class='fa fa-users'></i> Subject recruitment</a></li>
<li class='{{ request|active_re:subjectpool }}'><a href='{{ subjectpool }}'><i class='fa fa-users'></i> Subject recruitment</a></li>
{% endif %}
{% endif %}
{% endif %}
<li class='{% active_re request "contact" %}'><a href='/contact'><i class='fa fa-bell-o'></i> Contact us</a></li>
<li class='{{ request|active:contact }}'><a href='{{ contact }}'><i class='fa fa-bell-o'></i> Contact us</a></li>
<li><a target='_blank' href='{{ issues_url }}'><i class='fa fa-bullhorn'></i> Report a bug</a></li>
{% comment %} <li><a href='#' id="report-issue" data-toggle="modal" data-target="#bugModal" ><i class='fa fa-bullhorn'></i> Report a bug</a></li> {% endcomment %}
</ul>
{% endblock headerlinks %}
{% block login %}
<ul class='nav navbar-nav navbar-right'>
{% if user.is_authenticated %}
<li class='{% active request login %} dropdown'>
<li class='{{ request|active:login %} dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' href='#'>{{ user.email }} <i class='fa fa-chevron-down'></i></a>
<ul class='dropdown-menu'>
<li><a href='{{ profile }}'><i class='fa fa-user'></i> Account</a></li>
<li><a href='{{ logout }}'><i class='fa fa-sign-out'></i> Sign out</a></li>
</ul>
</li>
{% else %}
<li class='{% active request login %}'>
<li class='{{ request|active:login %}'>
{% if DEBUG %}
<a href='{{ login }}'>
{% else %}
Expand Down
12 changes: 6 additions & 6 deletions vcweb/core/templates/includes/nav-menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{% url 'home' as home %}
{% url 'about' as about %}
{% url 'contact' as contact %}
{% url 'contact_form' as contact %}
{% url 'core:login' as login %}
{% url 'core:logout' as logout %}
{% url 'core:register' as register %}
Expand All @@ -15,14 +15,14 @@
<div class='container'>
<ul class='nav'>
{% block headerlinks %}
<li class='{% active request home %}' ><a href='{{ home }}' class='first'><i class='fa fa-home'></i> Home</a></li>
<li class='{% active_re request dashboard %}'><a href='{{ dashboard }}'><i class='fa fa-th-large'></i> Dashboard</a></li>
<li class='{% active request contact %}'><a href='{{ contact }}'><i class='fa fa-bell-o'></i> Contact us</a></li>
<li class='{{ request|active:home }}' ><a href='{{ home }}' class='first'><i class='fa fa-home'></i> Home</a></li>
<li class='{{ request|active_re:dashboard }}'><a href='{{ dashboard }}'><i class='fa fa-th-large'></i> Dashboard</a></li>
<li class='{{ request|active:contact }}'><a href='{{ contact }}'><i class='fa fa-bell-o'></i> Contact us</a></li>
<li><a href='https://bitbucket.org/virtualcommons/vcweb/issues'><i class='fa fa-bullhorn'></i> Report a Bug</a></li>
{% if request.user.is_authenticated %}
<li class='{% active request logout %}'><a href='{{ logout }}'>Logout</a></li>
<li class='{{ request|active:logout }}'><a href='{{ logout }}'>Logout</a></li>
{% else %}
<li class='{% active request login %} {% active request register %}'>
<li class='{{ request|active:login }}'>
{% if DEBUG %}
<a href='{{ login }}'>Login/Register</a>
{% else %}
Expand Down
6 changes: 3 additions & 3 deletions vcweb/core/templatetags/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
register = template.Library()


@register.simple_tag
@register.filter(is_safe=True)
def active(request, pattern):
return 'active' if pattern == request.path else ''


@register.simple_tag
@register.filter(is_safe=True)
def active_re(request, pattern):
return 'active' if re.search(pattern, request.path) else ''


@register.filter(name='has_group')
@register.filter(is_safe=True)
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
3 changes: 1 addition & 2 deletions vcweb/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ def is_development(self):
'vcweb.experiment.irrigation',
)

VCWEB_APPS = ('vcweb.core',) + VCWEB_EXPERIMENTS

VCWEB_APPS = ('vcweb.core', 'vcweb.core.subjectpool',) + VCWEB_EXPERIMENTS

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + VCWEB_APPS

Expand Down

0 comments on commit e0ddfcf

Please sign in to comment.