Пример #1
0
    def quarantine(self, page=1, direction='dsc', order_by='timestamp',
        section=None, format=None):
        "quarantined messages"
        filters = session.get('filter_by', None)
        num_items = session.get('msgs_num_items', 50)
        if direction == 'dsc':
            sort = desc(order_by)
        else:
            sort = order_by
        messages = self._get_messages().filter(
                    Message.isquarantined == 1).order_by(sort)
        msgcount = self._get_msg_count().filter(
                    Message.isquarantined == 1)
        query = UserFilter(Session, c.user, messages)
        countquery = UserFilter(Session, c.user, msgcount)
        messages = query.filter()
        msgcount = countquery.filter()
        if section:
            if section == 'spam':
                messages = messages.filter(Message.spam == 1)
                msgcount = messages.filter(Message.spam == 1)
            else:
                messages = messages.filter(Message.spam == 0)
                msgcount = messages.filter(Message.spam == 0)
        if filters:
            dynq = DynaQuery(Message, messages, filters)
            dynmsgq = DynaQuery(Message, msgcount, filters)
            messages = dynq.generate()
            msgcount = dynmsgq.generate()
        c.order_by = order_by
        c.direction = direction
        c.section = section
        msgcount = msgcount.count()
        c.form = BulkReleaseForm(request.POST, csrf_context=session)
        if request.POST:
            choices = session.get('bulk_items', [])
        else:
            pages = paginate.Page(messages, page=int(page),
                                items_per_page=num_items,
                                item_count=msgcount)
            choices = [(str(message.id), message.id)
                        for message in pages.items]
            session['bulk_items'] = choices
            session.save()
        c.form.message_id.choices = choices
        if request.POST and c.form.validate() and choices:
            msgs = Session.query(Message.id,
                    Message.messageid,
                    Message.from_address,
                    Message.date, Message.to_address,
                    Message.hostname)\
                    .filter(Message.id.in_(c.form.message_id.data))
            query = UserFilter(Session, c.user, msgs)
            msgs = query.filter()
            formvals = (dict(release=c.form.release.data,
                            learn=c.form.learn.data, 
                            salearn_as=c.form.learnas.data,
                            todelete=c.form.delete.data,
                            use_alt=c.form.usealt.data,
                            altrecipients=c.form.altrecipients.data,
                            message_id=msg.messageid,
                            from_address=msg.from_address,
                            date=str(msg.date), to_address=msg.to_address,
                            hostname=msg.hostname,
                            mid=msg.id)
                        for msg in msgs)

            taskset = TaskSet(tasks=(
                                process_quarantined_msg.subtask(
                                    args=[formval],
                                    options=dict(queue=formval['hostname'])
                                ) for formval in formvals
                                )
                            )
            if formvals:
                try:
                    task = taskset.apply_async()
                    task.save(dbbackend)
                    session['bulk_items'] = []
                    if not 'taskids' in session:
                        session['taskids'] = []
                    session['taskids'].append(task.taskset_id)
                    session['bulkprocess-count'] = 1
                    session.save()
                    redirect(url('messages-bulk-process',
                            taskid=task.taskset_id))
                except QueueNotFound:
                    flash_alert(_('The messages could not processed'
                                ', try again later'))
        elif request.POST and not c.form.validate():
            flash_alert(_(', '.join([c.form.errors[err][0]
                                    for err in c.form.errors])))
        pages = paginate.Page(messages, page=int(page),
                                items_per_page=num_items,
                                item_count=msgcount)
        if format == 'json':
            response.headers['Content-Type'] = 'application/json'
            data = convert_to_json(pages,
                                    direction=direction,
                                    order_by=order_by,
                                    section=section)
            return data
        c.page = pages
        return render('/messages/quarantine.html')
Пример #2
0
    def quarantine(self, page=1, direction='dsc', order_by='timestamp',
        section=None, format=None):
        "quarantined messages"
        filters = session.get('filter_by', None)
        num_items = session.get('msgs_num_items', 50)
        if direction == 'dsc':
            sort = desc(order_by)
        else:
            sort = order_by
        messages = self._get_messages().filter(
                    Message.isquarantined == 1).order_by(sort)
        msgcount = self._get_msg_count().filter(
                    Message.isquarantined == 1)
        query = UserFilter(Session, c.user, messages)
        countquery = UserFilter(Session, c.user, msgcount)
        messages = query.filter()
        msgcount = countquery.filter()
        if section:
            if section == 'spam':
                messages = messages.filter(Message.spam == 1)
                msgcount = messages.filter(Message.spam == 1)
            else:
                messages = messages.filter(Message.spam == 0)
                msgcount = messages.filter(Message.spam == 0)
        if filters:
            dynq = DynaQuery(Message, messages, filters)
            dynmsgq = DynaQuery(Message, msgcount, filters)
            messages = dynq.generate()
            msgcount = dynmsgq.generate()
        c.order_by = order_by
        c.direction = direction
        c.section = section
        msgcount = msgcount.count()
        c.form = BulkReleaseForm(request.POST, csrf_context=session)
        if request.POST:
            choices = session.get('bulk_items', [])
        else:
            pages = paginate.Page(messages, page=int(page),
                                items_per_page=num_items,
                                item_count=msgcount)
            choices = [(str(message.id), message.id)
                        for message in pages.items]
            session['bulk_items'] = choices
            session.save()
        c.form.message_id.choices = choices
        if request.POST and c.form.validate() and choices:
            msgs = Session.query(Message.id,
                    Message.messageid,
                    Message.from_address,
                    Message.timestamp, Message.to_address,
                    Message.hostname)\
                    .filter(Message.id.in_(c.form.message_id.data))
            query = UserFilter(Session, c.user, msgs)
            msgs = query.filter()
            localtmz = config.get('baruwa.timezone', 'Africa/Johannesburg')
            formvals = (dict(release=c.form.release.data,
                            learn=c.form.learn.data, 
                            salearn_as=c.form.learnas.data,
                            todelete=c.form.delete.data,
                            use_alt=c.form.usealt.data,
                            altrecipients=c.form.altrecipients.data,
                            message_id=msg.messageid,
                            from_address=msg.from_address,
                            date=convert_date(msg.timestamp, localtmz).strftime('%Y%m%d'),
                            to_address=msg.to_address,
                            hostname=msg.hostname,
                            mid=msg.id)
                        for msg in msgs)

            taskset = TaskSet(tasks=(
                                process_quarantined_msg.subtask(
                                    args=[formval],
                                    options=dict(queue=formval['hostname'])
                                ) for formval in formvals
                                )
                            )
            if formvals:
                try:
                    task = taskset.apply_async()
                    task.save(dbbackend)
                    session['bulk_items'] = []
                    if not 'taskids' in session:
                        session['taskids'] = []
                    session['taskids'].append(task.taskset_id)
                    session['bulkprocess-count'] = 1
                    session.save()
                    redirect(url('messages-bulk-process',
                            taskid=task.taskset_id))
                except QueueNotFound:
                    flash_alert(_('The messages could not processed'
                                ', try again later'))
        elif request.POST and not c.form.validate():
            flash_alert(_(u', '.join([unicode(c.form.errors[err][0])
                                    for err in c.form.errors])))
        pages = paginate.Page(messages, page=int(page),
                                items_per_page=num_items,
                                item_count=msgcount)
        if format == 'json':
            response.headers['Content-Type'] = 'application/json'
            data = convert_to_json(pages,
                                    direction=direction,
                                    order_by=order_by,
                                    section=section)
            return data
        c.page = pages
        return render('/messages/quarantine.html')
Пример #3
0
    def quarantine(self,
                   page=1,
                   direction='dsc',
                   order_by='timestamp',
                   section=None,
                   format=None):
        "quarantined messages"
        filters = session.get('filter_by', None)
        num_items = session.get('msgs_num_items', 50)
        if direction == 'dsc':
            sort = desc(order_by)
        else:
            sort = order_by
        messages = get_messages().filter(
            Message.isquarantined == 1).order_by(sort)
        msgcount = get_msg_count().filter(Message.isquarantined == 1)
        query = UserFilter(Session, c.user, messages)
        countquery = UserFilter(Session, c.user, msgcount)
        messages = query.filter()
        msgcount = countquery.filter()
        if section:
            if section == 'spam':
                messages = messages.filter(Message.spam == 1)
                msgcount = messages.filter(Message.spam == 1)
            else:
                messages = messages.filter(Message.spam == 0)
                msgcount = messages.filter(Message.spam == 0)
        if filters:
            dynq = DynaQuery(Message, messages, filters)
            dynmsgq = DynaQuery(Message, msgcount, filters)
            messages = dynq.generate()
            msgcount = dynmsgq.generate()
        c.order_by = order_by
        c.direction = direction
        c.section = section
        msgcount = msgcount.count()
        c.form = BulkReleaseForm(request.POST, csrf_context=session)
        if request.method == 'POST':
            choices = session.get('bulk_items', [])
        else:
            pages = paginate.Page(messages,
                                  page=int(page),
                                  items_per_page=num_items,
                                  item_count=msgcount)
            choices = [(str(message.id), message.id)
                       for message in pages.items]
            session['bulk_items'] = choices
            session.save()
        c.form.message_id.choices = choices
        if request.method == 'POST' and c.form.validate() and choices:
            msgs = Session.query(Message.id,
                    Message.messageid,
                    Message.from_address,
                    Message.timestamp, Message.to_address,
                    Message.hostname, Message.msgfiles)\
                    .filter(Message.id.in_(c.form.message_id.data))
            query = UserFilter(Session, c.user, msgs)
            msgs = query.filter()
            localtmz = config.get('baruwa.timezone', 'Africa/Johannesburg')
            formvals = (dict(release=c.form.release.data,
                             learn=c.form.learn.data,
                             salearn_as=c.form.learnas.data,
                             todelete=c.form.delete.data,
                             use_alt=c.form.usealt.data,
                             altrecipients=c.form.altrecipients.data,
                             message_id=msg.messageid,
                             from_address=msg.from_address,
                             date=convert_date(msg.timestamp,
                                               localtmz).strftime('%Y%m%d'),
                             msgfiles=msg.msgfiles,
                             to_address=msg.to_address,
                             hostname=msg.hostname,
                             mid=msg.id,
                             num=num_items) for msg in msgs)

            if formvals:
                try:
                    subtasks = [
                        process_quarantined_msg.subtask(
                            args=[formval],
                            options=dict(
                                routing_key=system_hostname() if asbool(
                                    config.get('ms.quarantine.shared', 'false')
                                ) else formval['hostname']))
                        for formval in formvals
                    ]
                    task = group(subtasks).apply_async()
                    task.save(backend=RBACKEND)
                    session['bulk_items'] = []
                    if 'taskids' not in session:
                        session['taskids'] = []
                    session['taskids'].append(task.id)
                    session['bulkprocess-count'] = 1
                    session.save()
                    redirect(url('messages-bulk-process', taskid=task.id))
                except (QueueNotFound, OperationalError, IndexError):
                    flash_alert(
                        _('The messages could not processed'
                          ', try again later'))
        elif request.method == 'POST' and not c.form.validate():
            try:
                flash_alert(
                    _(u', '.join([
                        unicode(c.form.errors[err][0]) for err in c.form.errors
                    ])))
            except IndexError:
                flash_alert(
                    _('The messages could not processed'
                      ', an Unknown error occured.'))
        pages = paginate.Page(messages,
                              page=int(page),
                              items_per_page=num_items,
                              item_count=msgcount)
        if format == 'json':
            response.headers['Content-Type'] = 'application/json'
            data = convert_to_json(pages,
                                   direction=direction,
                                   order_by=order_by,
                                   section=section)
            return data
        c.page = pages
        return self.render('/messages/quarantine.html')