Ejemplo n.º 1
0
    def trigger_assign(self, task, user_set, constituencies=None):
        from signup.signals import user_touch
        
        assigned = []
        already_assigned = []
        for user in user_set:
            user_touch.send(self,
                            user=user,
                            task_slug=task.slug,
                            constituencies=constituencies)
            assigned.append(user)

        return (assigned, already_assigned)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
    def handle(self, *args, **options):
        """
            The 'touch' command fires the user_touch signal on every
            user on the site. This allows tasks to :
            assign themselves
            to pre-existing users (in addition to new users, as handled
            by user_signup)
            """
        if not args:
            raise CommandError("Please specify a command, e.g. 'touch' or 'email'")
        if args[0] not in ('touch','email'):
            raise CommandError("Unknown command '%s'" % args[0])
        near_postcode = options.get('near_postcode', None)
        if not near_postcode:
            has_distance = options['max_distance']
            has_number = options['max_number']
            
            if has_distance and not has_number:
                raise CommandError("'--max-distance' option requires a "
                                   "postcode")
            if has_number and (not has_distance and not options['random']):
                raise CommandError("'--max-number' option requires a "
                                   "postcode or --random flag")
            
        if hasattr(options, 'max_distance') and not near_postcode:
            raise CommandError("'max-distance' option requires a postcode")
        max_distance = options['max_distance'] \
                       and int(options['max_distance']) or None
        max_number = options['max_number'] \
                     and int(options['max_number']) or None
        if near_postcode:
            constituencies = []
            constituency_name = twfy.getConstituency(near_postcode)
            constituency = Constituency.objects.all()\
                           .filter(name=constituency_name)\
                           .get(year=settings.CONSTITUENCY_YEAR)
            neighbours = constituency.neighbors(limit=max_number,
                                                within_km=max_distance)
            users = ConstituencyUserChain(constituency, neighbours)

        else:
            users = CustomUser.objects.all()
        if options['random']:
            users = users.order_by('?')
        queryfilter = options.get('queryfilter', '')
        if queryfilter:
            users = eval(queryfilter)
        emailfilter = options.get('emailfilter', '')
        if emailfilter:
            users = users.filter(email__icontains=emailfilter)
        task_slug = options.get('task_slug', None)
        dry_run = options.get('dry_run', False)

        count = 0
        for user in users:
            if max_number and count == max_number:
                break
            msg = "user %s" % user
            if args[0] == "touch":
                msg += "\n  touching:"
                responses = []
                if task_slug:
                    if dry_run:
                        print task_slug
                        count += 1
                    else:
                        if TaskUser.objects.filter(user=user,
                                                   task__slug=task_slug):
                            continue
                        else:
                            responses = user_touch.send(self,
                                                        user=user,
                                                        task_slug=task_slug)
                            count += 1
                else:
                    for task in Task.objects.all():
                        if dry_run:
                            print task.slug
                            count += 1
                        else:
                            if TaskUser.objects.filter(user=user,
                                                       task__slug=task_slug):
                                continue                            
                            result = user_touch.send(self,
                                                     user=user,
                                                     task_slug=task.slug)
                            responses.extend(result)
                            count += 1
                    msg += "\n  ".join([x[1] for x in responses\
                                        if x[1]])
            elif args[0] == "email":
                emailed_on_this_round = []
                for task in TaskUser.objects.filter(user=user):
                    if task_slug and task_slug != task.task.slug:
                        continue
                    if user.email not in emailed_on_this_round:
                        sent = False
                        if not dry_run:
                            try:
                                sent = task.send_email(force=True)
                            except RegistrationProfile.DoesNotExist:
                                msg + "\n  no profile found for %s" % user.email
                        if sent:
                            msg += "\n  emailing about %s" % task.task.slug
                        else:
                            msg += "\n  no emails assigned for %s" % task.task.slug
                        emailed_on_this_round.append(user.email)
                    else:
                        msg += "\n   already emailed today"
                    
            print msg