def create_partner(self, org, name, description, primary_contact, labels=(), restricted=True): return Partner.create(org, name, description, primary_contact, restricted, labels, None)
def create_orgs(self, superuser, org_names, group_names, field_specs, partner_specs, password): """ Creates the orgs """ self._log(f"Creating {len(org_names)} orgs... ") orgs = [] for o, org_name in enumerate(org_names): org = Org.objects.create( name=org_name, timezone=pytz.timezone(random.choice(pytz.all_timezones)), subdomain=org_name.lower(), created_on=timezone.now(), created_by=superuser, modified_by=superuser, ) orgs.append(org) # create administrator user for this org Profile.create_org_user( org, "Adam", f"admin{o+1}@unicef.org", password, change_password=False, must_use_faq=False ) for group_name in group_names: Group.objects.create(org=org, uuid=str(uuid4()), name=group_name) for field_spec in field_specs: Field.objects.create( org=org, label=field_spec["label"], key=field_spec["key"], value_type=field_spec["value_type"] ) label_names = set() for partner_spec in partner_specs: label_names.update(partner_spec["labels"]) for label_name in sorted(label_names): tests = [ContainsTest([label_name.lower()], Quantifier.ANY)] Label.create(org, label_name, f"Messages about {label_name}", tests, is_synced=False) for p, partner_spec in enumerate(partner_specs): labels = Label.objects.filter(org=org, name__in=partner_spec["labels"]) partner = Partner.create( org, partner_spec["name"], partner_spec["description"], primary_contact=None, restricted=True, labels=labels, ) for user_spec in partner_spec["users"]: email = f"{user_spec['name'].lower()}{o + 1}@{partner_spec['name'].lower()}.com" Profile.create_partner_user(org, partner, user_spec["role"], user_spec["name"], email, password) self._log(self.style.SUCCESS("OK") + "\n") return orgs
def save(self, obj): data = self.form.cleaned_data org = self.request.org if 'partner_id' in self.kwargs: partner = Partner.get_all(org).get(pk=self.kwargs['partner_id']) else: partner = data.get('partner', None) full_name = data['full_name'] role = data.get('role', None) password = data['password'] change_password = data['change_password'] self.object = User.create(org, partner, role, full_name, obj.email, password, change_password)
def save(self, obj): data = self.form.cleaned_data org = self.request.org if 'partner_id' in self.kwargs: partner = Partner.get_all(org).get( pk=self.kwargs['partner_id']) else: partner = data.get('partner', None) full_name = data['full_name'] role = data.get('role', None) password = data['password'] change_password = data['change_password'] self.object = User.create(org, partner, role, full_name, obj.email, password, change_password)
def render_book(self, book): if self.type == self.TYPE_LABEL: sheet = book.add_sheet(six.text_type(_("Incoming Messages"))) labels = list(Label.get_all(self.org).order_by('name')) # get each label's day counts and organise by label and day totals_by_label = {} for label in labels: totals = DailyCount.get_by_label([label], DailyCount.TYPE_INCOMING, self.since, self.until).day_totals() totals_by_label[label] = {t[0]: t[1] for t in totals} self.write_row(sheet, 0, ["Date"] + [l.name for l in labels]) row = 1 for day in date_range(self.since, self.until): totals = [totals_by_label.get(l, {}).get(day, 0) for l in labels] self.write_row(sheet, row, [day] + totals) row += 1 elif self.type == self.TYPE_PARTNER: sheet = book.add_sheet(six.text_type(_("Replies Sent"))) partners = list(Partner.get_all(self.org).order_by('name')) # get each partner's day counts and organise by partner and day totals_by_partner = {} for partner in partners: totals = DailyCount.get_by_partner([partner], DailyCount.TYPE_REPLIES, self.since, self.until).day_totals() totals_by_partner[partner] = {t[0]: t[1] for t in totals} self.write_row(sheet, 0, ["Date"] + [p.name for p in partners]) row = 1 for day in date_range(self.since, self.until): totals = [totals_by_partner.get(l, {}).get(day, 0) for l in partners] self.write_row(sheet, row, [day] + totals) row += 1
def create_partner(self, org, name, labels=(), restricted=True): return Partner.create(org, name, restricted, labels, None)
def __init__(self, *args, **kwargs): self.org = kwargs.pop('org') super(UserForm, self).__init__(*args, **kwargs) self.fields['partner'].queryset = Partner.get_all(self.org)
def get_partner(self): return Partner.get_all( self.request.org).get(pk=self.kwargs["partner_id"])
def render_book(self, book): if self.type == self.TYPE_LABEL: sheet = book.add_sheet(str(_("Incoming Messages"))) labels = list(Label.get_all(self.org).order_by("name")) # get each label's day counts and organise by label and day totals_by_label = {} for label in labels: totals = DailyCount.get_by_label([label], DailyCount.TYPE_INCOMING, self.since, self.until).day_totals() totals_by_label[label] = {t[0]: t[1] for t in totals} self.write_row(sheet, 0, ["Date"] + [l.name for l in labels]) row = 1 for day in date_range(self.since, self.until): totals = [ totals_by_label.get(l, {}).get(day, 0) for l in labels ] self.write_row(sheet, row, [day] + totals) row += 1 elif self.type == self.TYPE_USER: replies_sheet = book.add_sheet(str(_("Replies Sent"))) cases_opened_sheet = book.add_sheet(str(_("Cases Opened"))) cases_closed_sheet = book.add_sheet(str(_("Cases Closed"))) users = self.org.get_org_users().order_by("profile__full_name") replies_totals_by_user = {} cases_opened_by_user = {} cases_closed_by_user = {} for user in users: replies_totals = DailyCount.get_by_user( self.org, [user], DailyCount.TYPE_REPLIES, self.since, self.until).day_totals() cases_opened_totals = DailyCount.get_by_user( self.org, [user], DailyCount.TYPE_CASE_OPENED, self.since, self.until).day_totals() cases_closed_totals = DailyCount.get_by_user( self.org, [user], DailyCount.TYPE_CASE_CLOSED, self.since, self.until).day_totals() replies_totals_by_user[user] = { t[0]: t[1] for t in replies_totals } cases_opened_by_user[user] = { t[0]: t[1] for t in cases_opened_totals } cases_closed_by_user[user] = { t[0]: t[1] for t in cases_closed_totals } self.write_row(replies_sheet, 0, ["Date"] + [u.get_full_name() for u in users]) self.write_row(cases_opened_sheet, 0, ["Date"] + [u.get_full_name() for u in users]) self.write_row(cases_closed_sheet, 0, ["Date"] + [u.get_full_name() for u in users]) row = 1 for day in date_range(self.since, self.until): replies_totals = [ replies_totals_by_user.get(u, {}).get(day, 0) for u in users ] cases_opened_totals = [ cases_opened_by_user.get(u, {}).get(day, 0) for u in users ] cases_closed_totals = [ cases_closed_by_user.get(u, {}).get(day, 0) for u in users ] self.write_row(replies_sheet, row, [day] + replies_totals) self.write_row(cases_opened_sheet, row, [day] + cases_opened_totals) self.write_row(cases_closed_sheet, row, [day] + cases_closed_totals) row += 1 elif self.type == self.TYPE_PARTNER: replies_sheet = book.add_sheet(str(_("Replies Sent"))) ave_sheet = book.add_sheet(str(_("Average Reply Time"))) ave_closed_sheet = book.add_sheet(str(_("Average Closed Time"))) cases_opened_sheet = book.add_sheet(str(_("Cases Opened"))) cases_closed_sheet = book.add_sheet(str(_("Cases Closed"))) partners = list(Partner.get_all(self.org).order_by("name")) # get each partner's day counts and organise by partner and day replies_totals_by_partner = {} cases_opened_by_partner = {} cases_closed_by_partner = {} replied_averages_by_partner = {} closed_averages_by_partner = {} for partner in partners: replies_totals = DailyCount.get_by_partner( [partner], DailyCount.TYPE_REPLIES, self.since, self.until).day_totals() cases_opened_totals = DailyCount.get_by_partner( [partner], DailyCount.TYPE_CASE_OPENED, self.since, self.until).day_totals() cases_closed_totals = DailyCount.get_by_partner( [partner], DailyCount.TYPE_CASE_CLOSED, self.since, self.until).day_totals() replies_totals_by_partner[partner] = { t[0]: t[1] for t in replies_totals } cases_opened_by_partner[partner] = { t[0]: t[1] for t in cases_opened_totals } cases_closed_by_partner[partner] = { t[0]: t[1] for t in cases_closed_totals } replied_second_totals = DailySecondTotalCount.get_by_partner( [partner], DailySecondTotalCount.TYPE_TILL_REPLIED, self.since, self.until).day_totals() replied_averages_by_partner[partner] = { t[0]: (float(t[2]) / t[1]) for t in replied_second_totals } closed_second_totals = DailySecondTotalCount.get_by_partner( [partner], DailySecondTotalCount.TYPE_TILL_CLOSED, self.since, self.until).day_totals() closed_averages_by_partner[partner] = { t[0]: (float(t[2]) / t[1]) for t in closed_second_totals } self.write_row(replies_sheet, 0, ["Date"] + [p.name for p in partners]) self.write_row(cases_opened_sheet, 0, ["Date"] + [p.name for p in partners]) self.write_row(cases_closed_sheet, 0, ["Date"] + [p.name for p in partners]) self.write_row(ave_sheet, 0, ["Date"] + [p.name for p in partners]) self.write_row(ave_closed_sheet, 0, ["Date"] + [p.name for p in partners]) row = 1 for day in date_range(self.since, self.until): replies_totals = [ replies_totals_by_partner.get(l, {}).get(day, 0) for l in partners ] cases_opened_totals = [ cases_opened_by_partner.get(l, {}).get(day, 0) for l in partners ] cases_closed_totals = [ cases_closed_by_partner.get(l, {}).get(day, 0) for l in partners ] replied_averages = [ replied_averages_by_partner.get(l, {}).get(day, 0) for l in partners ] closed_averages = [ closed_averages_by_partner.get(l, {}).get(day, 0) for l in partners ] self.write_row(replies_sheet, row, [day] + replies_totals) self.write_row(cases_opened_sheet, row, [day] + cases_opened_totals) self.write_row(cases_closed_sheet, row, [day] + cases_closed_totals) self.write_row(ave_sheet, row, [day] + replied_averages) self.write_row(ave_closed_sheet, row, [day] + closed_averages) row += 1
def create_partner(self, org, name): return Partner.create(org, name, None)
def __init__(self, *args, **kwargs): super(OrgUserForm, self).__init__(*args, **kwargs) self.fields['partner'].queryset = Partner.get_all(self.org).order_by('name')
def render_book(self, book): if self.type == self.TYPE_LABEL: sheet = book.add_sheet(six.text_type(_("Incoming Messages"))) labels = list(Label.get_all(self.org).order_by('name')) # get each label's day counts and organise by label and day totals_by_label = {} for label in labels: totals = DailyCount.get_by_label([label], DailyCount.TYPE_INCOMING, self.since, self.until).day_totals() totals_by_label[label] = {t[0]: t[1] for t in totals} self.write_row(sheet, 0, ["Date"] + [l.name for l in labels]) row = 1 for day in date_range(self.since, self.until): totals = [totals_by_label.get(l, {}).get(day, 0) for l in labels] self.write_row(sheet, row, [day] + totals) row += 1 elif self.type == self.TYPE_USER: replies_sheet = book.add_sheet(six.text_type(_("Replies Sent"))) cases_opened_sheet = book.add_sheet(six.text_type(_("Cases Opened"))) cases_closed_sheet = book.add_sheet(six.text_type(_("Cases Closed"))) users = self.org.get_org_users().order_by('profile__full_name') replies_totals_by_user = {} cases_opened_by_user = {} cases_closed_by_user = {} for user in users: replies_totals = DailyCount.get_by_user( self.org, [user], DailyCount.TYPE_REPLIES, self.since, self.until).day_totals() cases_opened_totals = DailyCount.get_by_user( self.org, [user], DailyCount.TYPE_CASE_OPENED, self.since, self.until).day_totals() cases_closed_totals = DailyCount.get_by_user( self.org, [user], DailyCount.TYPE_CASE_CLOSED, self.since, self.until).day_totals() replies_totals_by_user[user] = {t[0]: t[1] for t in replies_totals} cases_opened_by_user[user] = {t[0]: t[1] for t in cases_opened_totals} cases_closed_by_user[user] = {t[0]: t[1] for t in cases_closed_totals} self.write_row(replies_sheet, 0, ["Date"] + [u.get_full_name() for u in users]) self.write_row(cases_opened_sheet, 0, ["Date"] + [u.get_full_name() for u in users]) self.write_row(cases_closed_sheet, 0, ["Date"] + [u.get_full_name() for u in users]) row = 1 for day in date_range(self.since, self.until): replies_totals = [replies_totals_by_user.get(u, {}).get(day, 0) for u in users] cases_opened_totals = [cases_opened_by_user.get(u, {}).get(day, 0) for u in users] cases_closed_totals = [cases_closed_by_user.get(u, {}).get(day, 0) for u in users] self.write_row(replies_sheet, row, [day] + replies_totals) self.write_row(cases_opened_sheet, row, [day] + cases_opened_totals) self.write_row(cases_closed_sheet, row, [day] + cases_closed_totals) row += 1 elif self.type == self.TYPE_PARTNER: replies_sheet = book.add_sheet(six.text_type(_("Replies Sent"))) ave_sheet = book.add_sheet(six.text_type(_("Average Reply Time"))) ave_closed_sheet = book.add_sheet(six.text_type(_("Average Closed Time"))) cases_opened_sheet = book.add_sheet(six.text_type(_("Cases Opened"))) cases_closed_sheet = book.add_sheet(six.text_type(_("Cases Closed"))) partners = list(Partner.get_all(self.org).order_by('name')) # get each partner's day counts and organise by partner and day replies_totals_by_partner = {} cases_opened_by_partner = {} cases_closed_by_partner = {} replied_averages_by_partner = {} closed_averages_by_partner = {} for partner in partners: replies_totals = DailyCount.get_by_partner([partner], DailyCount.TYPE_REPLIES, self.since, self.until).day_totals() cases_opened_totals = DailyCount.get_by_partner([partner], DailyCount.TYPE_CASE_OPENED, self.since, self.until).day_totals() cases_closed_totals = DailyCount.get_by_partner([partner], DailyCount.TYPE_CASE_CLOSED, self.since, self.until).day_totals() replies_totals_by_partner[partner] = {t[0]: t[1] for t in replies_totals} cases_opened_by_partner[partner] = {t[0]: t[1] for t in cases_opened_totals} cases_closed_by_partner[partner] = {t[0]: t[1] for t in cases_closed_totals} replied_second_totals = DailySecondTotalCount.get_by_partner([partner], DailySecondTotalCount.TYPE_TILL_REPLIED, self.since, self.until).day_totals() replied_averages_by_partner[partner] = {t[0]: (float(t[2]) / t[1]) for t in replied_second_totals} closed_second_totals = DailySecondTotalCount.get_by_partner([partner], DailySecondTotalCount.TYPE_TILL_CLOSED, self.since, self.until).day_totals() closed_averages_by_partner[partner] = {t[0]: (float(t[2]) / t[1]) for t in closed_second_totals} self.write_row(replies_sheet, 0, ["Date"] + [p.name for p in partners]) self.write_row(cases_opened_sheet, 0, ["Date"] + [p.name for p in partners]) self.write_row(cases_closed_sheet, 0, ["Date"] + [p.name for p in partners]) self.write_row(ave_sheet, 0, ["Date"] + [p.name for p in partners]) self.write_row(ave_closed_sheet, 0, ["Date"] + [p.name for p in partners]) row = 1 for day in date_range(self.since, self.until): replies_totals = [replies_totals_by_partner.get(l, {}).get(day, 0) for l in partners] cases_opened_totals = [cases_opened_by_partner.get(l, {}).get(day, 0) for l in partners] cases_closed_totals = [cases_closed_by_partner.get(l, {}).get(day, 0) for l in partners] replied_averages = [replied_averages_by_partner.get(l, {}).get(day, 0) for l in partners] closed_averages = [closed_averages_by_partner.get(l, {}).get(day, 0) for l in partners] self.write_row(replies_sheet, row, [day] + replies_totals) self.write_row(cases_opened_sheet, row, [day] + cases_opened_totals) self.write_row(cases_closed_sheet, row, [day] + cases_closed_totals) self.write_row(ave_sheet, row, [day] + replied_averages) self.write_row(ave_closed_sheet, row, [day] + closed_averages) row += 1
def __init__(self, *args, **kwargs): super(OrgUserForm, self).__init__(*args, **kwargs) self.fields["partner"].queryset = Partner.get_all( self.org).order_by("name")
def create_partner(self, org, name, labels=()): return Partner.create(org, name, labels, None)