Skip to content

Commit

Permalink
Merge branch 'next' into Feature-Tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanThomsen authored Apr 12, 2022
2 parents a7c8b46 + 2b8ac75 commit 3d741cb
Show file tree
Hide file tree
Showing 30 changed files with 1,075 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Black formatter check

on: [push]
on: [push, pull_request]

jobs:
black:
Expand Down
Binary file added media/stregsystem/background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/stregsystem/easter.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Django==2.2.24
Pillow==8.3.2
Coverage==4.4.1
pytz==2018.3
pytz==2021.3
regex==2017.07.28
freezegun==0.3.15
Django-Select2==5.11.1
Expand Down
2 changes: 1 addition & 1 deletion stregreport/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def ranks_for_year(request, year):
1859,
]
coffee = [32, 35, 36, 39]
vitamin = [1850, 1851, 1852, 1863]
vitamin = [1850, 1851, 1852, 1863, 1880]

FORMAT = '%d/%m/%Y kl. %H:%M'
last_year = year - 1
Expand Down
1 change: 1 addition & 0 deletions stregsystem/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class ProductAdmin(admin.ModelAdmin):
"categories",
"rooms",
"alcohol_content_ml",
"caffeine_content_mg",
)
readonly_fields = ("get_bought",)

Expand Down
56 changes: 56 additions & 0 deletions stregsystem/caffeine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from datetime import datetime, timedelta
from typing import List

from django.utils import timezone

CAFFEINE_IN_COFFEE = 70
CAFFEINE_DEGRADATION_PR_HOUR = 0.12945
CAFFEINE_TIME_INTERVAL = timedelta(days=1)


class Intake:
timestamp: datetime
mg: int

def __init__(self, timestamp: datetime, mg: int):
self.timestamp = timestamp
self.mg = mg


def caffeine_mg_to_coffee_cups(mg: int) -> int:
return int(mg / CAFFEINE_IN_COFFEE)


# calculate current caffeine in body, takes list of intakes, applies caffeine degradation by using compound interest
def current_caffeine_in_body_compound_interest(intakes: List[Intake]) -> float:
"""
Given a list of Intakes (timestamp, mg), calculate caffeine mg content in blood at current time.
Assumes a bioavailability of 100%, immediate absorption in body, and caffeine half-life of 5 hours denoted by
CAFFEINE_DEGRADATION_PR_HOUR.
"""
# if no intakes withing given time interval, return 0mg in blood
if len(intakes) == 0:
return 0

# init last intake time to 24 hours and one minute ago to keep degradation within scope of intakes
last_intake_time = timezone.now() - timedelta(days=1, minutes=1)
mg_blood = 0

# append intake of 0 mg at timezone.now to intakes to calculate caffeine degradation from the latest intake to now
intakes.append(Intake(timezone.now(), 0))

# do compound interest on list of intakes
for intake in intakes:
# first do degradation of current caffeine in blood using compound rule (kn = k0 * (1 + r)^n), maxing to 0
mg_blood = max(
mg_blood
* ((1 - CAFFEINE_DEGRADATION_PR_HOUR) ** ((intake.timestamp - last_intake_time) / timedelta(hours=1))),
0,
)
# swap curr timestamp with last intake time to calculate degradation timespan in next iteration
last_intake_time = intake.timestamp

# finally, add current intake of caffeine to mg_blood
mg_blood += intake.mg

return mg_blood
19 changes: 14 additions & 5 deletions stregsystem/fixtures/testdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
"notes": "This is a test user."
}
},
{
"fields": {
"name": "kaffe"
},
"model": "stregsystem.category",
"pk": "6"
},
{
"model": "stregsystem.product",
"pk": 1,
Expand All @@ -28,12 +35,14 @@
},
{
"model": "stregsystem.product",
"pk": 2,
"pk": 32,
"fields": {
"name": "Kold Kaffe",
"price": 200,
"active": true,
"deactivate_date": null
"deactivate_date": null,
"caffeine_content_mg": 70,
"categories": [6]
}
},
{
Expand Down Expand Up @@ -72,7 +81,7 @@
"model": "stregsystem.oldprice",
"pk": 2,
"fields": {
"product": 2,
"product": 32,
"price": 200,
"changed_on": "2017-03-13T12:43:40.784+00:00"
}
Expand All @@ -81,7 +90,7 @@
"model": "stregsystem.oldprice",
"pk": 3,
"fields": {
"product": 2,
"product": 32,
"price": 200,
"changed_on": "2017-03-13T12:43:42.151+00:00"
}
Expand Down Expand Up @@ -118,7 +127,7 @@
"pk": 2,
"fields": {
"member": 1,
"product": 2,
"product": 32,
"room": 1,
"timestamp": "2017-03-13T12:54:12.423+00:00",
"price": 200
Expand Down
17 changes: 17 additions & 0 deletions stregsystem/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime

from django import forms

from stregsystem.models import MobilePayment, Member
Expand Down Expand Up @@ -31,3 +33,18 @@ class QRPaymentForm(forms.Form):

class PurchaseForm(forms.Form):
product_id = forms.IntegerField()


class RankingDateForm(forms.Form):
from_date = forms.DateField(widget=forms.SelectDateWidget(years=range(2000, datetime.date.today().year + 1)))
to_date = forms.DateField(
initial=datetime.date.today(), widget=forms.SelectDateWidget(years=range(2000, datetime.date.today().year + 1))
)

# validate form. make sure that from_date is before to_date
def clean(self):
cleaned_data = super().clean()
if cleaned_data['from_date'] > cleaned_data['to_date']:
# raise forms.ValidationError('Fra dato skal være før eller lig til dato')
self.add_error('to_date', 'Fra dato skal være før eller lig til dato')
return cleaned_data
18 changes: 18 additions & 0 deletions stregsystem/migrations/0015_product_caffeine_content_mg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.24 on 2021-11-30 12:45

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('stregsystem', '0014_mobilepayment_nullable_customername_20210908_1522'),
]

operations = [
migrations.AddField(
model_name='product',
name='caffeine_content_mg',
field=models.IntegerField(default=0),
),
]
39 changes: 36 additions & 3 deletions stregsystem/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
from collections import Counter
from email.utils import parseaddr

Expand All @@ -8,6 +9,7 @@
from django.db.models import Count
from django.utils import timezone

from stregsystem.caffeine import Intake, CAFFEINE_TIME_INTERVAL, current_caffeine_in_body_compound_interest
from stregsystem.deprecated import deprecated
from stregsystem.templatetags.stregsystem_extras import money
from stregsystem.utils import (
Expand Down Expand Up @@ -287,6 +289,35 @@ def calculate_alcohol_promille(self):

return bac

def calculate_caffeine_in_body(self) -> float:
# get list of last 24h caffeine intakes and calculate current body caffeine content
return current_caffeine_in_body_compound_interest(
[
Intake(x.timestamp, x.product.caffeine_content_mg)
for x in self.sale_set.filter(
timestamp__gt=timezone.now() - CAFFEINE_TIME_INTERVAL, product__caffeine_content_mg__gt=0
).order_by('timestamp')
]
)

def is_leading_coffee_addict(self):
coffee_category = [6]

now = timezone.now()
start_of_week = now - datetime.timedelta(days=now.weekday()) - datetime.timedelta(hours=now.hour)
user_with_most_coffees_bought = (
Member.objects.filter(
sale__timestamp__gt=start_of_week,
sale__timestamp__lte=now,
sale__product__categories__in=coffee_category,
)
.annotate(Count('sale'))
.order_by('-sale__count', 'username')
.first()
)

return user_with_most_coffees_bought == self


class Payment(models.Model): # id automatisk...
class Meta:
Expand All @@ -311,7 +342,8 @@ def __unicode__(self):
def __str__(self):
return self.member.username + " " + str(self.timestamp) + ": " + money(self.amount)

def save(self, *args, **kwargs):
@transaction.atomic
def save(self, mbpayment=None, *args, **kwargs):
if self.id:
return # update -- should not be allowed
else:
Expand All @@ -321,7 +353,7 @@ def save(self, *args, **kwargs):
self.member.save()
if self.member.email != "" and self.amount != 0:
if '@' in parseaddr(self.member.email)[1] and self.member.want_spam:
send_payment_mail(self.member, self.amount)
send_payment_mail(self.member, self.amount, mbpayment.comment if mbpayment else None)

def log_from_mobile_payment(self, processed_mobile_payment, admin_user: User):
LogEntry.objects.log_action(
Expand Down Expand Up @@ -447,7 +479,7 @@ def process_submitted_mobile_payments(submitted_data, admin_user: User):

payment = Payment(member=member, amount=payment_amount)
payment.log_from_mobile_payment(processed_mobile_payment, admin_user)
payment.save()
payment.save(mbpayment=processed_mobile_payment)

processed_mobile_payment.payment = payment
processed_mobile_payment.member = member
Expand Down Expand Up @@ -517,6 +549,7 @@ class Product(models.Model): # id automatisk...
categories = models.ManyToManyField(Category, blank=True)
rooms = models.ManyToManyField(Room, blank=True)
alcohol_content_ml = models.FloatField(default=0.0, null=True)
caffeine_content_mg = models.IntegerField(default=0)

@deprecated
def __unicode__(self):
Expand Down
Binary file added stregsystem/static/stregsystem/beerflake.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3d741cb

Please sign in to comment.