def handle(self, *args, **options): currently = datetime.datetime.now() limit_year, limit_month = logic.decrement_year_month( currently.year, currently.month) members = (Member.objects.filter( legal_id__isnull=False, category__fee__gt=0, shutdown_date__isnull=True).order_by('legal_id').all()) mail_data = [] for member in members: if not isinstance(member.entity, Person): continue debt = logic.get_debt_state(member, limit_year, limit_month) if not debt: continue debt_info = { 'debt': utils.build_debt_string(debt), 'member': member, 'annual_fee': member.category.fee * 12, 'on_purpose_missing_var': "ERROR", } text = render_to_string('members/mail_indebt.txt', debt_info) if 'ERROR' in text: # badly built template raise ValueError( "Problems building the mail text; info: {}".format( debt_info)) mail_data.append((member, text)) print("Found {} members in debt".format(len(mail_data))) for member, text in mail_data: print( f"Sending mail to {member.entity.full_name} <{member.entity.email}>" ) try: utils.send_email(member, MAIL_SUBJECT, text) except Exception as err: print(" problem:", repr(err)) else: print(" ok") print("Done")
def post(self, request): raw_sendmail = parse.parse_qs(request.body)[b'sendmail'] to_send_mail_ids = map(int, raw_sendmail) limit_year, limit_month = self._get_yearmonth(request) sent_error = 0 sent_ok = 0 tini = time.time() errors_code = str(uuid.uuid4()) for member_id in to_send_mail_ids: member = Member.objects.get(id=member_id) debt = logic.get_debt_state(member, limit_year, limit_month) debt_info = { 'debt': utils.build_debt_string(debt), 'member': member, 'annual_fee': member.category.fee * 12, 'on_purpose_missing_var': "ERROR", } text = render_to_string('members/mail_indebt.txt', debt_info) if 'ERROR' in text: # badly built template logger.error( "Error when building the report missing mail result, info: %s", debt_info) return HttpResponse("Error al armar la página") try: utils.send_email(member, self.MAIL_SUBJECT, text) except Exception as err: sent_error += 1 logger.exception( "Problems sending email [%s] to member %s: %r", errors_code, member, err) else: sent_ok += 1 deltat = time.time() - tini context = { 'sent_ok': sent_ok, 'sent_error': sent_error, 'errors_code': errors_code, 'deltamsec': int(deltat * 1000), } return render(request, 'members/mail_sent.html', context)
def get(self, request): """Produce the report with the given year/month limits.""" limit_year, limit_month = self._get_yearmonth(request) # get those already confirmed members members = Member.objects\ .filter(legal_id__isnull=False, category__fee__gt=0, shutdown_date__isnull=True)\ .order_by('legal_id').all() debts = [] for member in members: debt = logic.get_debt_state(member, limit_year, limit_month) if debt: debts.append({ 'member': member, 'debt': utils.build_debt_string(debt), }) context = { 'debts': debts, 'limit_year': limit_year, 'limit_month': limit_month, } return render(request, 'members/report_debts.html', context)
def test_exceeding(self): result = utils.build_debt_string([(2018, 8), (2018, 9), (2018, 10), (2018, 11)]) self.assertEqual(result, "4 (2018-08, 2018-09, 2018-10, ...)")
def test_3(self): result = utils.build_debt_string([(2018, 8), (2018, 9), (2018, 10)]) self.assertEqual(result, "3 (2018-08, 2018-09, 2018-10)")
def test_2(self): result = utils.build_debt_string([(2018, 8), (2018, 9)]) self.assertEqual(result, "2 (2018-08, 2018-09)")
def test_1(self): result = utils.build_debt_string([(2018, 8)]) self.assertEqual(result, "1 (2018-08)")
def test_empty(self): result = utils.build_debt_string([]) self.assertEqual(result, "-")