Ejemplo n.º 1
0
    def run_query(self, alert):
        results = []
        error = False
        try:
            logger.info("Now running the query: %s\n" % alert.query)

            # Set up the data
            data = search_utils.get_string_to_dict(alert.query)
            try:
                del data['filed_before']
            except KeyError:
                pass
            data['order_by'] = 'score desc'
            logger.info("  Data sent to SearchForm is: %s\n" % data)
            search_form = SearchForm(data)
            if search_form.is_valid():
                cd = search_form.cleaned_data

                if self.rate == 'rt' and len(self.valid_ids[cd['type']]) == 0:
                    # Bail out. No results will be found if no valid_ids.
                    return error, cd['type'], results

                cut_off_date = get_cut_off_date(self.rate)
                if cd['type'] == 'o':
                    cd['filed_after'] = cut_off_date
                elif cd['type'] == 'oa':
                    cd['argued_after'] = cut_off_date
                main_params = search_utils.build_main_query(cd)
                main_params.update({
                    'rows': '20',
                    'start': '0',
                    'hl.tag.pre': '<em><strong>',
                    'hl.tag.post': '</strong></em>',
                    'caller': 'cl_send_alerts',
                })
                if self.rate == 'rt':
                    main_params['fq'].append(
                        'id:(%s)' % ' OR '.join(
                            [str(i) for i in self.valid_ids[cd['type']]]
                        ),
                    )
                results = self.connections[
                    cd['type']
                ].raw_query(
                    **main_params
                ).execute()
            else:
                logger.info("  Query for alert %s was invalid\n"
                            "  Errors from the SearchForm: %s\n" %
                            (alert.query, search_form.errors))
                error = True
        except:
            traceback.print_exc()
            logger.info("  Search for this alert failed: %s\n" %
                        alert.query)
            error = True

        logger.info("  There were %s results\n" % len(results))

        return error, cd['type'], results
Ejemplo n.º 2
0
    def run_query(self, alert, cut_off_date):
        results = None
        error = False
        try:
            if self.verbosity >= 1:
                print "Now running the query: %s" % alert.alertText

            # Set up the data
            data = search_utils.get_string_to_dict(alert.alertText)
            try:
                del data['filed_before']
            except KeyError:
                pass
            data['order_by'] = 'score desc'
            if self.verbosity >= 1:
                print "  Data sent to SearchForm is: %s" % data
            search_form = SearchForm(data)
            if search_form.is_valid():
                cd = search_form.cleaned_data
                if cd['type'] == 'o':
                    cd['filed_after'] = cut_off_date
                elif cd['type'] == 'oa':
                    cd['argued_after'] = cut_off_date
                main_params = search_utils.build_main_query(cd)
                main_params.update({
                    'rows': '20',
                    'start': '0',
                    'hl.tag.pre': '<em><strong>',
                    'hl.tag.post': '</strong></em>',
                    'caller': 'cl_send_alerts',
                })
                if cd['type'] == 'o':
                    conn = sunburnt.SolrInterface(
                        settings.SOLR_OPINION_URL, mode='r'
                    )
                elif cd['type'] == 'oa':
                    conn = sunburnt.SolrInterface(
                        settings.SOLR_AUDIO_URL, mode='r'
                    )
                results = conn.raw_query(**main_params).execute()
            else:
                print "  Query for alert %s was invalid" % alert.alertText
                print "  Errors from the SearchForm: %s" % search_form.errors
                error = True
        except:
            traceback.print_exc()
            print "  Search for this alert failed: %s" % alert.alertText
            error = True

        if self.verbosity >= 1:
            if results:
                print "  There were %s results" % len(results)
            else:
                print "  There were no results"
        if self.verbosity >= 2:
            print "  The value of results is: %s" % results

        return error, cd['type'], results,
Ejemplo n.º 3
0
    def run_query(self, alert, cut_off_date):
        results = None
        error = False
        try:
            if self.verbosity >= 1:
                print "Now running the query: %s" % alert.alertText

            # Set up the data
            data = search_utils.get_string_to_dict(alert.alertText)
            try:
                del data['filed_before']
            except KeyError:
                pass
            data['order_by'] = 'score desc'
            if self.verbosity >= 1:
                print "  Data sent to SearchForm is: %s" % data
            search_form = SearchForm(data)
            if search_form.is_valid():
                cd = search_form.cleaned_data
                if cd['type'] == 'o':
                    cd['filed_after'] = cut_off_date
                elif cd['type'] == 'oa':
                    cd['argued_after'] = cut_off_date
                main_params = search_utils.build_main_query(cd)
                main_params.update({
                    'rows': '20',
                    'start': '0',
                    'hl.tag.pre': '<em><strong>',
                    'hl.tag.post': '</strong></em>',
                    'caller': 'cl_send_alerts',
                })
                if cd['type'] == 'o':
                    conn = sunburnt.SolrInterface(settings.SOLR_OPINION_URL,
                                                  mode='r')
                elif cd['type'] == 'oa':
                    conn = sunburnt.SolrInterface(settings.SOLR_AUDIO_URL,
                                                  mode='r')
                results = conn.raw_query(**main_params).execute()
            else:
                print "  Query for alert %s was invalid" % alert.alertText
                print "  Errors from the SearchForm: %s" % search_form.errors
                error = True
        except:
            traceback.print_exc()
            print "  Search for this alert failed: %s" % alert.alertText
            error = True

        if self.verbosity >= 1:
            if results:
                print "  There were %s results" % len(results)
            else:
                print "  There were no results"
        if self.verbosity >= 2:
            print "  The value of results is: %s" % results

        return error, cd['type'], results,
Ejemplo n.º 4
0
def view_alerts(request):
    alerts = request.user.profile.alert.all()
    for a in alerts:
        alert_dict = search_utils.get_string_to_dict(a.alertText)
        if alert_dict.get('type') == 'oa':
            a.type = 'oa'
        else:
            a.type = 'o'
    return render_to_response('profile/alerts.html', {
        'alerts': alerts,
        'private': True
    }, RequestContext(request))
Ejemplo n.º 5
0
def view_alerts(request):
    alerts = request.user.profile.alert.all()
    for a in alerts:
        alert_dict = search_utils.get_string_to_dict(a.alertText)
        if alert_dict.get('type') == 'oa':
            a.type = 'oa'
        else:
            a.type = 'o'
    return render_to_response(
        'profile/alerts.html',
        {'alerts': alerts,
         'private': True},
        RequestContext(request)
    )
Ejemplo n.º 6
0
    def emailer(self, cut_off_date):
        """Send out an email to every user whose alert has a new hit for a rate.

        Look up all users that have alerts for a given period of time, and iterate
        over them. For each of their alerts that has a hit, build up an email that
        contains all the hits.

        It's tempting to lookup alerts and iterate over those instead of over the
        users. The problem with that is that it would send one email per *alert*,
        not per *user*.
        """

        # Query all users with alerts of the desired frequency
        # Use the distinct method to only return one instance of each person.
        if self.options.get('user_id'):
            userProfiles = UserProfile.objects.filter(alert__alertFrequency=self.options['rate']).\
                filter(user__pk=self.options['user_id']).distinct()
        else:
            userProfiles = UserProfile.objects.filter(alert__alertFrequency=self.options['rate']).distinct()

        # for each user with a daily, weekly or monthly alert...
        alerts_sent_count = 0
        for userProfile in userProfiles:
            #...get their alerts...
            alerts = userProfile.alert.filter(alertFrequency=self.options['rate'])
            if self.verbosity >= 1:
                print "\n\nAlerts for user '%s': %s" % (userProfile.user, alerts)
                print "*" * 40

            hits = []
            # ...and iterate over their alerts.
            for alert in alerts:
                try:
                    if self.verbosity >= 1:
                        print "Now running the query: %s" % alert.alertText

                    # Set up the data
                    data = search_utils.get_string_to_dict(alert.alertText)
                    try:
                        del data['filed_before']
                    except KeyError:
                        pass
                    data['filed_after'] = cut_off_date
                    data['order_by'] = 'score desc'
                    if self.verbosity >= 1:
                        print "Data sent to SearchForm is: %s" % data
                    search_form = SearchForm(data)
                    if search_form.is_valid():
                        cd = search_form.cleaned_data
                        main_params = search_utils.build_main_query(cd)
                        main_params.update({
                            'rows': '20',
                            'start': '0',
                            'hl.tag.pre': '<em><strong>',
                            'hl.tag.post': '</strong></em>',
                            'caller': 'cl_send_alerts',
                        })
                        results = self.conn.raw_query(**main_params).execute()
                    else:
                        print "Query for alert %s was invalid" % alert.alertText
                        print "Errors from the SearchForm: %s" % search_form.errors
                        continue
                except:
                    traceback.print_exc()
                    print "Search for this alert failed: %s" % alert.alertText
                    continue

                if self.verbosity >= 1:
                    print "There were %s results" % len(results)
                if self.verbosity >= 2:
                    print "The value of results is: %s" % results

                # hits is a multi-dimensional array. It consists of alerts,
                # paired with a list of document dicts, of the form:
                # [[alert1, [{hit1}, {hit2}, {hit3}]], [alert2, ...]]
                try:
                    if len(results) > 0:
                        hits.append([alert, results])
                        alert.lastHitDate = now()
                        alert.save()
                    elif alert.sendNegativeAlert:
                        # if they want an alert even when no hits.
                        hits.append([alert, None])
                        if self.verbosity >= 1:
                            print "Sending results for negative alert '%s'" % alert.alertName
                except Exception, e:
                    traceback.print_exc()
                    print "Search failed on this alert: %s" % alert.alertText
                    print e

            if len(hits) > 0:
                alerts_sent_count += 1
                self.send_alert(userProfile, hits)
            elif self.verbosity >= 1:
                print "No hits, thus not sending mail for this alert."