Skip to content

Commit

Permalink
Merge pull request #90 from Teknologforeningen/feature/enhance-perfor…
Browse files Browse the repository at this point in the history
…mance

Performance enhancements, GitHub Action workflow, and other minor fixes
  • Loading branch information
filiptypjeu authored Aug 9, 2023
2 parents fd27520 + a2fe4f1 commit 5b39c1c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/django-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: FARS unittests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10.12"
- name: Install latest pip
run: python -m pip install --upgrade pip
- name: Install dependencies
run: |
sudo apt install libsasl2-dev python3-dev libldap2-dev libssl-dev
pip install -r requirements.txt
- name: Apply migrations
run: python fars/manage.py migrate
- name: Run tests
run: python fars/manage.py test fars
6 changes: 6 additions & 0 deletions fars/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ SECRET_KEY=SomeRandomSecretKey
# Should debug be enabled? True/False
DEBUG=True

# Log file path
LOG_FILE=/var/log/fars/info.log

# Directory where to collect all static files
STATIC_ROOT="/var/www/fars/static"

# Static collection for ajax-select
#AJAX_SELECT_INLINES = 'inline'
AJAX_SELECT_INLINES = 'staticfiles'
Expand Down
14 changes: 13 additions & 1 deletion fars/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setUp(self):
self.admin_group.user_set.add(self.admin)

# Public bookable
Bookable.objects.create(public=True, hidden=False, id_str='public')
self.bookable = Bookable.objects.create(public=True, hidden=False, id_str='public')

# Restricted public bookable
Bookable.objects.create(public=True, hidden=False, id_str='publicrestricted').booking_restriction_groups.set([self.restriction_group])
Expand Down Expand Up @@ -109,6 +109,12 @@ def test_for_superuser(self):
# Should see all bookables
self.assertEqual(M, len(self.get_bookables()))

def test_post(self):
# Should not be able to POST using the API
self.login_superuser()
response = self.client.post('/api/bookables', {'name': 'My Bookable'})
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED, response.data)


class BookingsAPITest(BaseAPITest):
def get_bookings(self, bookable=None, booking_group=''):
Expand Down Expand Up @@ -151,6 +157,12 @@ def test_for_superuser(self):
for bookable in Bookable.objects.all():
self.assertEqual(N, len(self.get_bookings(bookable)))

def test_post(self):
# Should not be able to POST using the API
self.login_superuser()
response = self.client.post('/api/bookings', {'bookable': self.bookable.id, 'user': self.user2.id, 'start': t2, 'end': plus_one_day(t2)})
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED, response.data)

def test_pagination(self):
self.login_superuser()
response = self.client.get('/api/bookings?limit=1')
Expand Down
11 changes: 6 additions & 5 deletions fars/api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils.translation import gettext as _
from rest_framework import viewsets, generics, pagination
from rest_framework.filters import SearchFilter, OrderingFilter
from django_filters import rest_framework as filters
Expand All @@ -9,12 +10,12 @@

class BookingFilter(filters.FilterSet):
# Custom filter fields
before = filters.IsoDateTimeFilter(field_name='start', lookup_expr='lte')
after = filters.IsoDateTimeFilter(field_name='end', lookup_expr='gte')
before = filters.IsoDateTimeFilter(field_name='start', lookup_expr='lte', label=_('Booking starts before'))
after = filters.IsoDateTimeFilter(field_name='end', lookup_expr='gte', label=_('Booking ends after'))
# Fields where the defualt filter type is overridden
bookable = filters.CharFilter(field_name='bookable__id_str')
username = filters.CharFilter(field_name='user__username')
booking_group = filters.CharFilter(field_name='booking_group__name')
bookable = filters.CharFilter(field_name='bookable__id_str', label=_('Bookable id_str is'))
username = filters.CharFilter(field_name='user__username', label=_('User username is'))
booking_group = filters.CharFilter(field_name='booking_group__name', label=_('Booking group name is'))

class Meta:
model = Booking
Expand Down
7 changes: 7 additions & 0 deletions fars/booking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ def delete_from_date_forward(self, date):


class Booking(models.Model):
class BookingsManager(models.Manager):
def get_queryset(self):
# Let's just always select the related objects as well
return super().get_queryset().select_related('bookable', 'user')

objects = BookingsManager()

bookable = models.ForeignKey(Bookable, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
start = models.DateTimeField(_("start"))
Expand Down
24 changes: 22 additions & 2 deletions fars/fars/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
'file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': '/var/log/fars/info.log',
'filename': env('LOG_FILE', '/var/log/fars/info.log'),
},
},
'loggers': {
Expand All @@ -103,6 +103,26 @@
},
},
}
'''
else:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
}
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
'level': 'DEBUG',
},
},
}
'''


WSGI_APPLICATION = 'fars.wsgi.application'

Expand Down Expand Up @@ -154,7 +174,7 @@

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR,'booking/static')
STATIC_ROOT = env('STATIC_ROOT', None)
STATIC_URL = '/static/'

REST_FRAMEWORK = {
Expand Down

0 comments on commit 5b39c1c

Please sign in to comment.