def add(request, constituency_slug=None): """ Report the addition of a leaflet """ context = {} leaflet_urls = request.GET.getlist('v1') \ or request.POST.getlist('v1') c = None if constituency_slug: year = settings.CONSTITUENCY_YEAR c = Constituency.objects.get(slug=constituency_slug, year=year) else: c = request.user.home_constituency leaflet = None for url in leaflet_urls: leaflet = UploadedLeaflet.objects.create(url=url, user=request.user, constituency=c) signals.leaflet_added.send(None, user=request.user, constituency=c) if leaflet: # return HttpResponseRedirect(reverse('tasks')) context['constituency'] = c context['leaflet'] = leaflet return render_with_context(request, 'tsc/thanks.html', context) else: return render_with_context(request, 'tsc/add.html', context)
def home(request): context = _get_statistics_context() if request.user.hassling: # Send them off a-hasslin' request.user.hassling = False request.user.save() return HttpResponseRedirect(reverse('constituency', args=[request.user.home_constituency.slug]) + "#candidates") tasks = TaskUser.objects.filter( user=request.user).order_by('state', 'date_assigned') open_tasks = tasks.filter(state__in=[TaskUser.States.started, TaskUser.States.assigned, TaskUser.States.ignored]) completed_tasks = tasks.filter(state=TaskUser.States.completed) ignored_tasks = tasks.filter(state=TaskUser.States.ignored) context['all_tasks'] = tasks context['open_tasks'] = open_tasks context['completed_tasks'] = completed_tasks context['ignored_tasks'] = ignored_tasks context['badges'] = Badge.objects.filter(user=request.user) context['new_signups'] = CustomUser.objects\ .order_by('-date_joined')[:5] context['first_time'] = request.user.login_count < 3 constituencies = request.user.constituencies.all() context['activity'] = generate_activity(constituencies) return render_with_context(request, 'tasks/tasks.html', context)
def manage_tasks(request): tasks = Task.objects.filter(archived=False) context= {} context['tasks'] = tasks return render_with_context(request, 'tasks/manage_tasks.html', context)
def admin_assign_constituency(request): from forms import AssignConstituency from signup.models import CustomUser context = {} if request.method == "POST": form = AssignConstituency(request.POST, request.FILES) if form.is_valid(): constituencies = form.cleaned_data['constituencies'] all_users = CustomUser.objects.filter( is_active=True, constituencies__in=constituencies) context['message'] = [] for task in form.cleaned_data['tasks']: (assigned, already_assigned) = TaskUser.objects.trigger_assign(task, all_users, constituencies) task_string = "%s in %s" % (task, constituencies) context['message'].append((task_string, assigned, already_assigned)) context['form'] = form else: context['form'] = form else: context['form'] = AssignConstituency() return render_with_context(request, 'tasks/task_admin.html', context)
def constituencies_with_more_than_rss(request, volunteers=1): current_site = Site.objects.get_current() constituencies = models.filter_where_customuser_more_than(volunteers) context = {'constituencies': constituencies} context['site'] = current_site return render_with_context(request, 'geo.rss', context, mimetype="application/atom+xml")
def manage_assign_email(request, task_pk): """ Assign an email to be sent for particular users. Creates TaskEmailUser objects to represent assignment """ context = {} matched_users = [] count = 0 skip = 0 task = get_object_or_404(Task, pk=task_pk) dry_run = request.POST.get('dry_run', False) selected_email = request.POST.get('email', None) email = None if selected_email: email = TaskEmail.objects.get(pk=selected_email) if request.method == "POST": queryfilter = request.POST['queryfilter'].strip() users = CustomUser.objects.all() if queryfilter: users = eval(queryfilter.strip()) users = users.exclude(taskuser__task__pk=task_pk, taskuser__state=TaskUser.States.ignored) users = users.filter(is_active=True, unsubscribed=False) # enforce for user in users: matched_users.append(user.email) taskusers = TaskUser.objects.filter(user=user, task=task) for task_user in taskusers: try: task_email_user = TaskEmailUser.objects.get(task_email=email, task_user=task_user) except TaskEmailUser.DoesNotExist: if not dry_run: task_email_user = TaskEmailUser.objects.create(task_email=email, task_user=task_user) count += 1 context['posted'] = True context['matched_users'] = matched_users context['queryfilter'] = queryfilter context['task'] = task context['emails'] = TaskEmail.objects.filter(task=task) \ .order_by("-date_created") context['dry_run'] = dry_run context['count'] = count context['skip'] = skip context['selected_email'] = email return render_with_context(request, 'tasks/manage_assign_email.html', context)
def express_interest(request, constituency_slug=None): """ Report the addition of a leaflet """ context = {} interest = None if request.method == "POST": if request.POST.get('interest', False): interest = MeetingInterest.objects\ .create(user=request.user, postcode=request.POST['postcode']) signals.interest_expressed.send(None, user=request.user) elif request.POST.get('organise', False): interest = MeetingInterest.objects\ .create(user=request.user, organiser=True, postcode=request.POST['postcode']) signals.interest_expressed.send(None, user=request.user) context['interest'] = interest return render_with_context(request, 'thanks.html', context) return render_with_context(request, 'express_interest.html', context)
def statistics(request): context = {} year = settings.CONSTITUENCY_YEAR context['histogram'] = models.date_joined_histogram() num_rows = context['histogram'].rowcount context['categorystep'] = int(num_rows / 40.0 * 4) + 1 context['const_volunteers'] = \ models.constituency_volunteers_histogram(Constituency.objects.filter(year=year)) return render_with_context(request, 'statistics.html', context)
def index(request): vars = {} if request.method == "POST": invite_form = InviteForm(request.POST, request.FILES) if invite_form.is_valid(): invite_form.save(request.user) context = {"notice": "Thanks for inviting more people to join!"} return HttpResponseRedirect(addToQueryString(reverse("tasks"), context)) else: vars["invite_form"] = invite_form else: vars["invite_form"] = InviteForm() vars["siteurl"] = quote("http://%s" % Site.objects.get_current().domain) request.user.seen_invite = True request.user.save() return render_with_context(request, "invite/invite_page.html", vars)
def manage_assign_tasks(request, task_pk): context = {} task = get_object_or_404(Task, pk=task_pk) context['task'] = task dry_run = False count = 0 skip = 0 matched_users = [] email = None if request.method == "POST": dry_run = request.POST.get('dry_run', False) queryfilter = request.POST['queryfilter'].strip() users = CustomUser.objects.all() if queryfilter: users = eval(queryfilter.strip()) users = users.filter(is_active=True, unsubscribed=False) # enforce for user in users: matched_users.append(user.email) if TaskUser.objects.filter(user=user, task__pk=task_pk): # Already assigned skip += 1 continue else: if not dry_run: # Trigger assignment responses = user_touch.send(None, user=user, task_slug=task.slug) count += 1 context['posted'] = True context['queryfilter'] = queryfilter context['matched_users'] = matched_users context['emails'] = TaskEmail.objects\ .distinct()\ .order_by("-date_created") context['dry_run'] = dry_run context['count'] = count context['skip'] = skip context['selected_email'] = email return render_with_context(request, 'tasks/manage_assign_tasks.html', context)
def index(request): vars = {} if request.method == "POST": invite_form = InviteForm(request.POST, request.FILES) if invite_form.is_valid(): invite_form.save(request.user) return HttpResponseRedirect(addToQueryString(reverse("inviteindex"), {'notice': strings.INVITE_NOTICE_SUCCESS})) else: vars['invite_form'] = invite_form else: vars['invite_form'] = InviteForm() vars['siteurl'] = quote("http://%s" % Site.objects.get_current().domain) return render_with_context(request, "invite/invite_page.html", vars)
def task(request, slug, constituency=None): """ Check out a task. Needs to also optionally take a login token to allow direct access to the task by email. """ context = {} context['task'] = Task.objects.get(slug=slug) if constituency is not None: year = settings.CONSTITUENCY_YEAR c = Constituency.objects.get(slug=constituency, year=year) context['constituency'] = Constituency.objects\ .get(slug=constituency, year=year) else: context['constituency'] = None; if request.user.is_authenticated(): items = TaskUser.objects if constituency is not None: try: context['usertasks'] = [items.get(task=context['task'], user=request.user, constituency=context['constituency'])] except TaskUser.DoesNotExist: pass else: # For the root task page, list all constituencies context['usertasks'] = items.filter(task=context['task'], user=request.user) else: context['usertasks'] = None started_tu = TaskUser.objects.filter(task=context['task'],\ state=TaskUser.States.started) if constituency is not None: started_tu = started_tu.filter(constituency=context['constituency']) context['started_users'] = \ CustomUser.objects.filter(taskuser__in=started_tu).distinct() return render_with_context(request, 'tasks/task_page.html', context)
def admin_assign_all(request): from forms import AssignForm from signup.models import CustomUser context = {} if request.method == "POST": form = AssignForm(request.POST, request.FILES) if form.is_valid(): all_users = CustomUser.objects.filter( is_active=True) context['message'] = [] for task in form.cleaned_data['tasks']: (assigned, already_assigned) = TaskUser.objects.trigger_assign(task, all_users) context['message'].append((task, assigned, already_assigned)) context['form'] = form else: context['form'] = form else: context['form'] = AssignForm() return render_with_context(request, 'tasks/task_admin.html', context)
def scan_queue(request, dry_run=False): # Look at the email queue, determine if there are any emails which can be sent users = CustomUser.objects.filter(is_active=True, unsubscribed=False, taskuser__taskemailuser__isnull=False) context = {} sent = [] for user in users: emails = TaskEmailUser.objects.filter(task_user__user=user).distinct() # Find out what the date was for the last sending last_sent = emails.exclude(date_sent=None).order_by('-date_added') date_last_sent = None if last_sent: date_last_sent = last_sent[0].date_sent # Only email once every two days if date_last_sent and date_last_sent > datetime.datetime.now() - datetime.timedelta(2): continue # Send unsent once-off messages with priority, then look for reminders # that are more than a week since they were last sent oldest_unsent = emails.filter(date_sent=None, task_email__email_type__in=[TaskEmail.EmailTypes.welcome, TaskEmail.EmailTypes.oneoff,], ).order_by('date_added') send_task_email_user = None if oldest_unsent: send_task_email_user = oldest_unsent[0] else: week_ago = datetime.datetime.now() - datetime.timedelta(7) # Find unsent reminders first reminders = emails.filter(date_sent=None, task_email__email_type=TaskEmail.EmailTypes.reminder ).order_by('date_sent') # If we cant find unsent reminders, find the oldest reminder sent more than a week ago if not reminders: reminders = emails.filter(date_sent__lte=week_ago, task_email__email_type=TaskEmail.EmailTypes.reminder ).order_by('date_sent') if reminders: send_task_email_user = reminders[0] if send_task_email_user: if not dry_run: try: success = send_task_email_user.send() if success: sent.append(send_task_email_user) except RegistrationProfile.DoesNotExist: pass else: sent.append(send_task_email_user) context['sent'] = sent context['dry_run'] = dry_run return render_with_context(request, 'tasks/emails_sent.html', context)