Пример #1
0
 def __init__(self, domain):
     self.form_data = []
     self.chw_data = []
     self.domain = domain
     domain_meta = Metadata.objects.filter(formdefmodel__domain=domain)
     domain_submits = Submission.objects.filter(domain=domain)
     
     self.name = domain.name
     self.submissions = domain_submits.count()
     self.attachments = Attachment.objects.filter(submission__domain=domain).count()
     if self.submissions:
         self.first_submission = domain_submits.order_by("submit_time")[0].submit_time
         self.last_submission = domain_submits.order_by("-submit_time")[0].submit_time
     else:
         self.first_submission = None
         self.last_submission = None
     
     self.full_count = domain_meta.count()
     chws = domain_meta.values_list('username', flat=True).distinct()
     forms = FormDefModel.objects.filter(domain=domain)
     blacklist = BlacklistedUser.for_domain(domain)
     for form in forms:
         form_metas = domain_meta.filter(formdefmodel=form)
         self.form_data.append({"form": form,
                                "count": form_metas.count(),
                                "first": _get_first_object(form_metas, "timeend", True),
                                "last": _get_first_object(form_metas, "timeend", False)
                               })
     self.blacklist_count = 0
     self.chw_blacklist_count = 0
     self.chw_count = 0
     for chw in chws:
         chw_forms = domain_meta.filter(username=chw)
         in_blacklist = chw in blacklist
         self.chw_data.append({"name": chw,
                               "count": chw_forms.count(),
                               "in_blacklist": in_blacklist,
                               "first": _get_first_object(chw_forms, "timeend", True),
                               "last": _get_first_object(chw_forms, "timeend", False)
                               })
         if in_blacklist:
             self.chw_blacklist_count += 1
             self.blacklist_count += chw_forms.count()
         else:
             self.chw_count += 1
     self.count = self.full_count - self.blacklist_count
Пример #2
0
def blacklist(request, domain=None):
    """Report of who is submitting as blacklisted users"""
    if not domain:
        domain = request.user.selected_domain
    startdate, enddate = get_dates(request, 7)
    if enddate < startdate:
        return """<p><b><font color="red">The date range you selected is not valid.  
                  End date of %s must fall after start date of %s.</font></b></p>""" % (
            enddate,
            startdate,
        )

    # our final structure called "all_data" will be:
    # { blacklist_username1:
    #    { device_id1:
    #        { # counts of submissions from the blacklisted user by date
    #          "date_counts": {date1: count1, date2: count2...}
    #          # other users who have also submitted from that device
    #          "users": [user1, user2]
    #        }
    #      device_id2: { ... }
    #      ...
    #    }
    #   blacklist_username2: { ... }
    #   ...
    # }
    # yikes.  we'll build it from the outside in, starting with the blacklist

    all_data = {}
    domain_blacklist = BlacklistedUser.for_domain(domain)

    # we need this dbhelper to do our inner queries, but might as well only
    # initialize it once.
    # TODO: we must be able to get the tablename, description from the model meta
    dbhelper = DbHelper("xformmanager_metadata", "XForm Metadata", "timeend")

    for blacklisted_user in domain_blacklist:
        # get metas, in the domain, within the timespan, matching this user
        all_metas = (
            Metadata.objects.filter(timeend__gte=startdate)
            .filter(timeend__lte=enddate)
            .filter(attachment__submission__domain=domain)
            .filter(username=blacklisted_user)
        )

        # list of devices submitting from this user in the period
        devices_to_check = all_metas.values_list("deviceid", flat=True).distinct()
        this_user_device_data = {}
        for device in devices_to_check:
            counts = dbhelper.get_filtered_date_count(
                startdate, enddate, {"username": blacklisted_user, "deviceid": device}
            )
            # flip key, value around and turn into a dictionary
            dict_counts = dict([(val, key) for key, val in counts])
            users = (
                Metadata.objects.filter(deviceid=device)
                .exclude(username=blacklisted_user)
                .values_list("username", flat=True)
                .distinct()
            )
            clean_users = []
            for user in users:
                if user:
                    clean_users.append(user)
                else:
                    clean_users.append("EMPTY USERNAME")
            this_device_data = {"date_counts": dict_counts, "users": ",".join(clean_users)}
            this_user_device_data[device] = this_device_data
        all_data[blacklisted_user] = this_user_device_data

    totalspan = enddate - startdate
    date_headings = []
    for day in range(0, totalspan.days + 1):
        target_date = startdate + timedelta(days=day)
        date_headings.append(target_date)
    return render_to_string(
        "custom/all/blacklist_submissions.html",
        {
            "all_dates": date_headings,
            "all_data": all_data,
            "startdate": startdate,
            "enddate": enddate,
            "domain": domain,
        },
    )