def get_base_email_context(email_set, profile_data): assert isinstance(email_set, (Session, Newsletter)) from regnskab.emailtemplate import format_price primary_title = profile_data.get('title') balance = profile_data.get('balance', Decimal()) profile = profile_data['profile'] initial_balance = profile_data.get('initial_balance', Decimal()) if primary_title: title = (tk.prefix(primary_title, email_set.period, type='unicode') if primary_title.period else primary_title.root) else: title = None context = { 'TITEL ': title + ' ' if title else '', 'NAVN': profile.name, 'GAELDFOER': format_price(initial_balance), 'GAELD': format_price(balance), 'MAXGAELD': format_price(email_set._max_debt), 'INKA': email_set._inka.name, } return context
def get_profiles_title_status(period=None, time=None): def profile_key(p): if p.status is None: return (3, p.name) elif not p.in_current: return (2, p.name) elif p.title is None: return (1, p.name) elif isinstance(p.title, Alias): return (1, '%s %s' % (p.title, p.name)) else: return (0, title_key(p.title)) titles = get_titles(period=period, time=time) status_qs = SheetStatus.objects.all().order_by('profile_id') if time is not None: status_qs = status_qs.exclude(start_time__gt=time) groups = itertools.groupby(status_qs, key=lambda s: s.profile_id) statuses = { pk: max(s, key=lambda s: (s.end_time is None, s.end_time)) for pk, s in groups } profiles = list(Profile.objects.all()) for p in profiles: p.status = statuses.get(p.id) p.titles = titles.get(p.id, []) p.title = p.titles[0] if p.titles else None if p.title: p.title_name = ( '%s %s' % (tk.prefix(p.title, period or config.GFYEAR, type='unicode') if p.title.period else p.title.root, p.name)) else: p.title_name = p.name p.in_current = (p.status and (p.status.end_time is None or (time is not None and p.status.end_time > time))) profiles.sort(key=profile_key) return profiles
def _init(self, profiles): self._profiles = [] for profile, amount, selected in profiles: p = 'profile%d_' % profile.id if profile.title: profile.display_name = ( '%s %s' % (tk.prefix(profile.title, type='unicode') if profile.title.period else profile.title.root, profile.name)) else: profile.display_name = profile.name self.fields[p + 'selected'] = forms.BooleanField( initial=selected, required=False, label='%s markeret' % profile.display_name) amount_str = '%g' % amount try: int(amount_str) except ValueError: amount_str = '%.2f' % amount self.fields[p + 'amount'] = forms.FloatField( initial=amount_str, label='%s beløb' % profile.display_name, widget=forms.TextInput()) self._profiles.append(profile)
def test_FUOEOE(self): self.assertEqual(tk.prefix(("FUØØ", 2016), 2016), "FUØØ")
def test_invalid_gfyear_string(self): with self.assertRaisesRegex(TypeError, "str is not a valid type for gfyear."): tk.prefix(("CERM", 2012), "2016")
def test_invalid_title_root(self): with self.assertRaisesRegex(ValueError, "int is not a valid type for root."): tk.prefix((1, 2001), 2016)
def test_year_minus_01(self): self.assertEqual(tk.prefix(("CERM", 2015), 2016), "GCERM")
def test_longstring(self): self.assertEqual(tk.prefix(("This is a long string", 2012), 2016), "TOThis is a long string")
def test_explicit_type_plus_2(self): self.assertEqual(tk.prefix(("CERM", 2018), 2016, type='normal'), "K2CERM")
def test_sameyear(self): self.assertEqual(tk.prefix(("CERM", 2016), 2016), "CERM")
def test_empty_minus_15(self): self.assertEqual(tk.prefix(("", 2001), 2016), "T12O")
def test_empty_minus_04(self): self.assertEqual(tk.prefix(("", 2012), 2016), "TO")
def test_empty(self): self.assertEqual(tk.prefix(("", 2016), 2016), "")
def test_FUAAAA(self): self.assertEqual(tk.prefix(("FUÅÅ", 2016), 2016), "FUÅÅ")
def test_funny(self): self.assertEqual(tk.prefix(('KASS', 2013), 2016, type='tex'), r'OKA\$\$')
def test_prefix(self): self.assertEqual(tk.prefix(self.form(2013), 2016), 'OFORM')
def test_invalid_type(self): with self.assertRaisesRegex( ValueError, "'somestring' is not a valid type-parameter"): tk.prefix(("CERM", 2001), 2016, type="somestring")
def test_current(self): self.assertEqual(tk.prefix(('FORM', 2016), 2016, type='tex'), 'FORM')
def test_invalid_title_period(self): with self.assertRaisesRegex(ValueError, "str is not a valid type for period."): tk.prefix(("CERM", "2001"), 2016)
def test_simple_old(self): self.assertEqual(tk.prefix(('FORM', 2012), 2016, type='tex'), 'TOFORM')
def test_invalid_title_period_len(self): with self.assertRaisesRegex(ValueError, "'20010' is not a valid period"): tk.prefix(("CERM", 20010), 2016)
def test_explicit_type_minus_15(self): self.assertEqual(tk.prefix(("CERM", 2001), 2016, type='normal'), "T12OCERM")
def tk_prefix(title, arg='unicode'): return tk.prefix(title, gfyear=config.GFYEAR, type=arg)
def test_simple_future(self): self.assertEqual(tk.prefix(('FORM', 2017), 2016, type='tex'), 'KFORM')
def rows(self): result = [] transactions = self.legacy_transactions() kinds = list(self.columns()) kind_dict = {kind.id: kind for kind in kinds} sheetrow_qs = self.sheetrow_set.all() sheetrow_qs = sheetrow_qs.select_related('profile') sheetrow_qs = sheetrow_qs.prefetch_related('purchase_set') for row in sheetrow_qs: purchases = {p.kind_id: p for p in row.purchase_set.all()} for purchase in purchases.values(): # Use cached kind purchase.kind = kind_dict[purchase.kind_id] purchase_list = [ purchases.get(kind.id, Purchase(row=row, kind=kind, count=0)) for kind in kinds ] for p in purchase_list: if p.count % 1 == 0: p.counter = range(int(p.count)) else: p.counter = None im_url, im_width, im_start, im_stop = row.image_data() if im_url: image = dict(url=im_url, width=im_width, start=im_start, stop=im_stop, height=im_stop - im_start) else: image = None row_empty = not any(p.count for p in purchase_list) result.append( dict( id=row.id, profile=row.profile, position=row.position, # needed? name=row.name, kinds=purchase_list, image=image, empty=row_empty, legacy_transactions=transactions.pop(row.profile_id, {}), )) profile_ids = set(row['profile'] for row in result) titles = get_primary_titles(profile_ids=profile_ids, period=self.period) for row in result: try: title = row['title'] = titles[row['profile'].id] except (KeyError, AttributeError): title = row['title'] = row['display_title'] = None row['title_name'] = (row['profile'].name if row['profile'] else '') else: row['display_title'] = (tk.prefix( title, self.period, type='unicode') if title.period else title.root) row['title_name'] = ' '.join( (row['display_title'], row['profile'].name)) if self.legacy_style(): # Sort rows by title, period def key(row): if isinstance(row['title'], Title): return (0, -row['title'].period, row['title'].kind, row['title'].root) return (1, ) result.sort(key=key) return result
def test_exponent_old(self): self.assertEqual(tk.prefix(('FORM', 2010), 2016, type='tex'), 'T$^{3}$OFORM')
def get_tex_source(self, threshold): period = self.regnskab_session.period purchase_qs = Purchase.objects.all().order_by().filter( row__sheet__period=period) purchase_qs = purchase_qs.annotate( name=F('kind__name'), sheet_id=F('row__sheet_id'), profile_id=F('row__profile_id'), session_id=F('row__sheet__session_id')) purchase_qs = purchase_qs.values_list( 'sheet_id', 'name', 'profile_id', 'session_id', 'count') kinds = { (sheet_id, o.name): o.unit_price for o in PurchaseKind.objects.all() for sheet_id in o.sheets.all().values_list('id', flat=True) } kind_last_sheet = {} for sheet_id, name in kinds.keys(): ex = kind_last_sheet.setdefault(name, sheet_id) if ex < sheet_id: kind_last_sheet[name] = sheet_id prices = {name: kinds[sheet_id, name] for name, sheet_id in kind_last_sheet.items()} if not prices: prices = get_default_prices() counts = defaultdict(Decimal) cur_counts = defaultdict(Decimal) for sheet_id, name, profile_id, session_id, count in purchase_qs: if name in ('guldølkasse', 'sodavandkasse'): real_name = 'ølkasse' real_count = count * (kinds[sheet_id, name] / kinds[sheet_id, 'ølkasse']) elif name in ('ølkasse', 'ølkasser'): real_name = 'ølkasse' real_count = count else: real_name, real_count = name, count counts[profile_id, real_name] += real_count if session_id == self.regnskab_session.id: cur_counts[profile_id, real_name] += real_count transaction_qs = Transaction.objects.all() period_start_date, = ( Sheet.objects.filter(period=period).aggregate(Min('start_date')).values()) period_start_time = timezone.get_current_timezone().localize( datetime.datetime.combine(period_start_date, datetime.time())) transaction_qs = transaction_qs.filter(time__gte=period_start_time) for o in transaction_qs: if o.kind == Transaction.PAYMENT: real_name = 'betalt' amount = -o.amount else: real_name = 'andet' amount = o.amount counts[o.profile_id, real_name] += amount if o.session_id == self.regnskab_session.id: cur_counts[o.profile_id, real_name] += amount context = {} for name, unit_price in prices.items(): context['price_%s' % name] = unit_price keys = 'ølkasse guldøl øl sodavand andet betalt'.split() for k in keys: context['total_%s' % k] = context['last_%s' % k] = Decimal() context['total_balance'] = Decimal() time = self.regnskab_session.send_time profiles = get_profiles_title_status(period=period, time=time) if period == 2016 and timezone.now().year == 2016: hængere = [i for i in range(len(profiles)) if not profiles[i].title] best = {profiles[i].title.root: i for i in range(len(profiles)) if profiles[i].title and profiles[i].title.period == 2016} assert best['FORM'] < best['NF'] nf = profiles[best['NF']] del profiles[best['NF']] nf.name = 'Taberen' nf.title = None profiles.insert(random.choice(hængere), nf) FORM = profiles[best['FORM']] del profiles[best['FORM']] profiles.insert(0, FORM) FORM.name = 'Vinderen' FORM.title = None balances = compute_balance() rows = [] for p in profiles: balance = balances.get(p.id, 0) context['total_balance'] += balance for k in keys: context['total_%s' % k] += counts[p.id, k] context['last_%s' % k] += cur_counts[p.id, k] p_context = {} if p.title: if p.title.period is None: title_str = title_to_tex(p.title.root) else: title_str = tk.prefix(p.title, period, type='tex') p_context['name'] = '%s %s' % (title_str, p.name) else: p_context['name'] = p.name FMT = dict(betalt='\\hfill \\num{%.2f}', andet='\\hfill \\num{%.2f}', ølkasse='\\hfill \\num{%.1f}') p_context['last'] = ' & '.join( FMT.get(k, '\\hfill \\num{%g}') % cur_counts.get((p.id, k), 0) for k in keys) p_context['total'] = ' & '.join( FMT.get(k, '\\hfill \\num{%g}') % counts.get((p.id, k), 0) for k in keys) p_context['balance'] = balance p_context['hl'] = '\\hl' if balance > threshold else '' if not p.status or p.status.end_time is not None: continue rows.append(BALANCE_ROW % p_context) context['personer'] = '\n'.join(rows) tex_source = BALANCE_PRINT_TEX % context return tex_source
def test_exponent_new(self): self.assertEqual(tk.prefix(('FORM', 2018), 2016, type='tex'), 'K$^{2}$FORM')
def __str__(self): return '%s %s' % (tk.prefix(self, type='unicode'), getattr(self, 'profile', ''))
def test_FUAEAE(self): self.assertEqual(tk.prefix(("FUÆÆ", 2016), 2016), "FUÆÆ")