Skip to content

Commit

Permalink
A few modifications to the BILL manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptypjeu committed Jan 13, 2024
1 parent 9195859 commit 91599bc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
40 changes: 20 additions & 20 deletions teknologr/api/bill.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ def __request(self, path):
if r.status_code != 200:
raise BILLException(f"BILL returned status code {r.status_code}")

number = 0
# Not a number, return as text
try:
number = int(r.text)
except ValueError:
# Not a number, return as text
return r.text

# A negative number means an error code
Expand All @@ -55,38 +54,39 @@ def create_bill_account(self, username):
raise BILLException(f"BILL returned error: {result}")

def delete_bill_account(self, bill_code):
info = self.get_bill_info(bill_code)
error = info.get('error')
if error:
raise BILLException(error)
info = self.get_account_by_code(bill_code)

# If the BILL account does not exist all is ok
if info.get('exists') is False:
if not info:
return

result = self.__request(f"del?type=user&acc={bill_code}")

if result != 0:
raise BILLException(f"BILL returned error: {result}")

def find_bill_code(self, username):
result = self.__request(f"get?type=user&id={username}")
return json.loads(result)["acc"]
def get_account_by_username(self, username):
'''
Get the info for a certain BILL account. Returns None if the account does not exist.
'''
try:
result = self.__request(f"get?type=user&id={username}")
return json.loads(result)
except BILLException as e:
s = str(e)
if s == BILLAccountManager.ERROR_ACCOUNT_DOES_NOT_EXIST:
return None
raise e

def get_bill_info(self, bill_code):
def get_account_by_code(self, bill_code):
'''
Get the info for a certain BILL account. Never throws.
Get the info for a certain BILL account. Returns None if the account does not exist.
'''
if not bill_code:
return {'acc': None, 'exists': False}
try:
result = self.__request(f"get?type=user&acc={bill_code}")
return {
**json.loads(result),
'exists': True,
}
return json.loads(result)
except BILLException as e:
s = str(e)
if s == BILLAccountManager.ERROR_ACCOUNT_DOES_NOT_EXIST:
return {'acc': bill_code, 'exists': False}
return {'acc': bill_code, 'error': s}
return None
raise e
10 changes: 8 additions & 2 deletions teknologr/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,13 @@ def change_ldap_password(request, member_id):
class BILLAccountView(APIView):
def get(self, request, member_id):
member = get_object_or_404(Member, id=member_id)
return Response(BILLAccountManager().get_bill_info(member.bill_code), status=200)
try:
account = BILLAccountManager().get_account_by_code(member.bill_code)
except Exception as e:
return Response({'detail': str(e)}, status=500)
if not account:
return Response({'detail': 'Could not find BILL account.'}, status=404)
return Response(account)

def post(self, request, member_id):
member = get_object_or_404(Member, id=member_id)
Expand All @@ -395,7 +401,7 @@ def post(self, request, member_id):

# Check if there already is a BILL account with this LDAP name
try:
bill_code = bm.find_bill_code(member.username)
bill_code = bm.get_account_by_username(member.username).get('acc')
except:
pass

Expand Down
8 changes: 4 additions & 4 deletions teknologr/members/templates/member.html
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ <h5>LDAP</h5>
class="btn btn-primary"
data-toggle="modal"
data-target="#changepw_modal"
{% if not LDAP.exists %}disabled title="LDAP-konto existerar inte"{% endif %}>
{% if not LDAP.username %}disabled title="LDAP-konto existerar inte"{% endif %}>
Ändra lösenord
</button>
{% include "modals/ldap_changepw.html" with modalname="changepw_modal" title="Ändra LDAP lösenord" member_id=member.id only %}
Expand Down Expand Up @@ -237,15 +237,15 @@ <h5>BILL</h5>

{% if BILL.error %}
<div class="alert alert-danger">{{ BILL.error }}</div>
{% elif not BILL.exists %}
{% elif not BILL.acc %}
<div class="alert alert-danger">BILL-konto "{{ member.bill_code }}" existerar inte</div>
{% endif %}

<b>Konto:</b>
<span class="monospace">{{ member.bill_code }}</span>
<br/>

{% if BILL.exists %}
{% if BILL.acc %}
<b>Saldo:</b> {{ BILL.balance }}€
<br/>
{% endif %}
Expand All @@ -259,7 +259,7 @@ <h5>BILL</h5>
</button>

{% else %}
<button id="add-bill-button" class="btn btn-primary" {% if not LDAP.exists %} disabled title="Skapa LDAP-konto först"{% endif %} data-id="{{ member.id }}">Skapa BILL-konto</button>
<button id="add-bill-button" class="btn btn-primary" {% if not LDAP.username %} disabled title="Skapa LDAP-konto först"{% endif %} data-id="{{ member.id }}">Skapa BILL-konto</button>
{% endif %}
</div>
</div>
Expand Down
14 changes: 8 additions & 6 deletions teknologr/members/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,21 @@ def member(request, member_id):
# Get user account info
if member.username:
try:
context['LDAP'] = {}
with LDAPAccountManager() as lm:
context['LDAP'] = lm.get_user_details(member.username)
except LDAPError as e:
context['LDAP']['error'] = LDAPError_to_string(e)
context['LDAP'] = {'error': LDAPError_to_string(e)}

if member.bill_code:
bm = BILLAccountManager()
context['bill_admin_url'] = bm.admin_url(member.bill_code)
context['BILL'] = bm.get_bill_info(member.bill_code)
username = context['BILL'].get('id')
if username and member.username != username:
context['BILL']['error'] = f'LDAP användarnamnen här ({member.username}) och i BILL ({username}) matchar inte'
try:
context['BILL'] = bm.get_account_by_code(member.bill_code) or {}
username = context['BILL'].get('id')
if username and member.username != username:
context['BILL']['error'] = f'LDAP användarnamnen här ({member.username}) och i BILL ({username}) matchar inte'
except Exception as e:
context['BILL'] = {'error': str(e)}

# load side list items
set_side_context(context, 'members', member)
Expand Down

0 comments on commit 91599bc

Please sign in to comment.