Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

import csv 

import io 

import logging 

 

import import_app.csv_parser as csv_parser 

from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator 

from django.forms import modelformset_factory 

from django.http import JsonResponse 

from django.shortcuts import render 

from import_app.forms_import import BankingImportForm 

from members.models import Member, EmailToMember 

from import_app.templatetags.currency import currency 

 

from .models import DuplicateBankingWarning, NegativeCreditError, Transaction 

 

logging.getLogger(__name__) 

logging.getLogger().setLevel(logging.INFO) 

 

 

def run_import_banking(request): 

# Handle file upload 

uploadedFile = '' 

errors = [] 

imported = [] 

context = {} 

latest_chaos_number = Member.objects.latest('chaos_number').chaos_number 

if request.method == 'POST': 

form = BankingImportForm(request.POST, request.FILES) 

if form.is_valid(): 

uploadedFile = request.FILES['docfile'] 

data = uploadedFile.read().decode("iso-8859-1") 

f = io.StringIO(data) 

reader = csv.reader(f, delimiter=';') 

duplicate_warnings = [] 

for row in reader: 

logging.debug("reading row: {}".format(row)) 

if len(row) == 8 and not row[0].startswith('Buchungstag'): 

new_transaction = csv_parser.TransactionReader(row) 

new_tr_entry = Transaction() 

try: 

new_tr_entry.add_transaction(new_transaction, max_chaos_nr=latest_chaos_number) 

except DuplicateBankingWarning: 

duplicate_warnings.append('Duplicated entry not added: {}'.format(new_tr_entry.string_in)) 

except NegativeCreditError: 

logging.info("Outgoing transaction skipped") 

if len(duplicate_warnings) > 0: 

context["warnings"] = duplicate_warnings 

context["message"] = "uploaded successfully" 

 

form = BankingImportForm() # A empty, unbound form 

 

last_transaction = Transaction.objects.all().order_by('booking_day').last() 

last_transaction_date = last_transaction.booking_day if last_transaction else None 

 

context.update({'form': form, 'uploadedFile': uploadedFile, 'errors': errors, 'imported': imported, 

'latest_import': last_transaction_date}) 

logging.info("context is {}".format(context)) 

return render(request, 'import_banking_csv.html', context) 

 

 

def manage_transactions_failed(request): 

"""Show and manage failed transactions""" 

TransactionFormSet = modelformset_factory(Transaction, fields=('chaos_number', 'status'), extra=0) 

logging.info("manage_transactions: TransactionFormset".format(TransactionFormSet)) 

if request.method == 'POST': 

formset = TransactionFormSet(request.POST, request.FILES) 

filteredformset = TransactionFormSet() 

if formset.is_valid(): 

formset.save() 

return render( 

request, 

"manage_transactions_confirm.html", 

context={ 

"formset": formset, 

"status_matching": Transaction.STATUS_MATCHED_CHAOS_NR, 

"status_erroneous": Transaction.STATUS_FAILED, 

}, 

) 

 

else: 

# pagination goes here 

qs = Transaction.objects.filter(status=Transaction.STATUS_FAILED) 

paginator = Paginator(qs, 10) 

page = request.GET.get('page') 

try: 

objects = paginator.page(page) 

except PageNotAnInteger: 

objects = paginator.page(1) 

except EmptyPage: 

objects = paginator.page(paginator.num_pages) 

 

page_query = qs.filter(id__in=[object.id for object in objects]) 

formset = TransactionFormSet(queryset=page_query) 

context = {'objects': objects, 'formset': formset, "status_matching": Transaction.STATUS_MATCHED_CHAOS_NR} 

return render(request, 'manage_transactions.html', context) 

 

 

def manage_transactions_ignored(request): 

"""Show and manage ignored transactions""" 

TransactionFormSet = modelformset_factory(Transaction, fields=('chaos_number', 'status'), extra=0) 

logging.info("manage_transactions: TransactionFormset".format(TransactionFormSet)) 

if request.method == 'POST': 

formset = TransactionFormSet(request.POST, request.FILES) 

filteredformset = TransactionFormSet() 

if formset.is_valid(): 

formset.save() 

return render( 

request, 

"manage_transactions_confirm.html", 

context={ 

"formset": formset, 

"status_matching": Transaction.STATUS_MATCHED_CHAOS_NR, 

"status_erroneous": Transaction.STATUS_IGNOREFOREVER, 

}, 

) 

 

else: 

# pagination goes here 

qs = Transaction.objects.filter(status=Transaction.STATUS_IGNOREFOREVER) 

paginator = Paginator(qs, 10) 

page = request.GET.get('page') 

try: 

objects = paginator.page(page) 

except PageNotAnInteger: 

objects = paginator.page(1) 

except EmptyPage: 

objects = paginator.page(paginator.num_pages) 

 

page_query = qs.filter(id__in=[object.id for object in objects]) 

formset = TransactionFormSet(queryset=page_query) 

context = {'objects': objects, 'formset': formset, "status_matching": Transaction.STATUS_MATCHED_CHAOS_NR} 

return render(request, 'manage_transactions.html', context) 

 

 

def manage_transactions(request): 

TransactionFormSet = modelformset_factory(Transaction, fields=('chaos_number', 'status'), extra=0) 

logging.info("manage_transactions: TransactionFormset".format(TransactionFormSet)) 

if request.method == 'POST': 

formset = TransactionFormSet(request.POST, request.FILES) 

if formset.is_valid(): 

formset.save() 

return render(template_name="manage_transactions_confirm.html", 

context={"formset": formset, 

"status_matching": Transaction.STATUS_MATCHED_CHAOS_NR, 

"status_erroneous": Transaction.STATUS_UNKNOWN_CHAOS_NR, 

}, request=request) 

 

# pagination goes here 

qs = Transaction.objects.filter(status=Transaction.STATUS_UNKNOWN_CHAOS_NR)\ 

| Transaction.objects.filter(status=Transaction.STATUS_MATCHED_CHAOS_NR) 

paginator = Paginator(qs, 10) 

page = request.GET.get('page') 

try: 

objects = paginator.page(page) 

except PageNotAnInteger: 

objects = paginator.page(1) 

except EmptyPage: 

objects = paginator.page(paginator.num_pages) 

 

page_query = qs.filter(id__in=[obj.id for obj in objects]) 

formset = TransactionFormSet(queryset=page_query) 

context = {'objects': objects, 'formset': formset, "status_matching": Transaction.STATUS_MATCHED_CHAOS_NR} 

return render(request, 'manage_transactions.html', context) 

 

 

def manage_transactions_confirm(request): 

TransactionFormSet = modelformset_factory(Transaction, fields=('chaos_number', 'status'), extra=0) 

if request.method == 'POST': 

formset = TransactionFormSet(request.POST, request.FILES) 

if formset.is_valid(): 

formset.save() 

return render( 

template_name="manage_transactions_confirm.html", 

context={"formset": formset}, request=request 

) 

 

return render(template_name="base.html", context={}, request=request) 

 

 

def manage_transactions_book(request): 

logging.basicConfig(level=logging.DEBUG) 

logging.warning("book") 

TransactionFormSet = modelformset_factory(Transaction, fields=('chaos_number', 'status'), extra=0) 

184 ↛ 211line 184 didn't jump to line 211, because the condition on line 184 was never false if request.method == 'POST': 

context = { 

"formset": [], # formset because that's what they've been called since import 

"status_failed": Transaction.STATUS_FAILED, 

"n_new_failed": 0, 

"n_all_failed": 0, 

"n_new_completed": 0 

} 

formset = TransactionFormSet(request.POST, request.FILES) 

logging.debug("Booking {}".format(formset)) 

for form in formset: 

logging.debug(form.instance) 

196 ↛ 194line 196 didn't jump to line 194, because the condition on line 196 was never false if form.instance.status == Transaction.STATUS_MATCHED_CHAOS_NR: 

print(form) 

form.instance.book() 

context["formset"].append(form) 

200 ↛ 201line 200 didn't jump to line 201, because the condition on line 200 was never true if form.instance.status == Transaction.STATUS_FAILED: 

context["n_new_failed"] += 1 

202 ↛ 194line 202 didn't jump to line 194, because the condition on line 202 was never false elif form.instance.status == Transaction.STATUS_COMPLETED: 

context["n_new_completed"] += 1 

EmailToMember(email_type=EmailToMember.SEND_TYPE_HAND_MATCHED, 

member=Member.objects.get(chaos_number=form.instance.chaos_number)).save() 

 

context["n_all_failed"] = len(Transaction.objects.filter(status=Transaction.STATUS_FAILED)) 

return render(template_name="success.html", context=context, request=request) 

 

else: 

return render(template_name="base.html", context={}, request=request) 

 

 

def member_lookup(request, chaosnr): 

logging.info("member_lookup called for {}".format(chaosnr)) 

try: 

member = Member.objects.get(chaos_number=chaosnr) 

return JsonResponse({ 

'member_first_name': member.first_name, 

'member_last_name': member.last_name, 

'chaosnr_asked': chaosnr, 

'member_chaos_number': member.chaos_number, 

'member_account_balance': currency(member.account_balance) 

}) 

 

except Member.DoesNotExist: 

 

return JsonResponse({ 

'member_first_name': None, 

'member_last_name': None, 

'chaosnr_asked': chaosnr, 

'member_chaos_number': None, 

'member_account_balance': None 

})