Add unit tests
[pharos.git] / tools / pharos-dashboard / src / notification / models.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from django.db import models
12
13 class BookingNotification(models.Model):
14     id = models.AutoField(primary_key=True)
15     type = models.CharField(max_length=100)
16     booking = models.ForeignKey('booking.Booking', on_delete=models.CASCADE)
17     submit_time = models.DateTimeField()
18     submitted = models.BooleanField(default=False)
19
20     def get_content(self):
21         return {
22             'start': self.booking.start.isoformat(),
23             'end': self.booking.end.isoformat(),
24             'user': self.booking.user.username,
25             'purpose': self.booking.purpose
26         }
27
28     def save(self, *args, **kwargs):
29         notifications = self.booking.bookingnotification_set.filter(type=self.type).exclude(
30             id=self.id)
31         if notifications.count() > 0:
32             raise ValueError('Doubled Notification')
33         return super(BookingNotification, self).save(*args, **kwargs)