def waitlist_check(request):
    waitlists = Waitlist.objects.all().prefetch_related(
        'users').select_related('course')
    for waitlist in waitlists:
        if not waitlist.expired and waitlist.course.available > 0 or waitlist.expired and waitlist.course.available <= 0:
            if not waitlist.expired and waitlist.course.available > 0:
                message = 'open_course'
                waitlist.expired = True
            elif waitlist.expired and waitlist.course.available <= 0:
                message = 'closed_course'
                waitlist.expired = False
            else:
                # this should not happen but just to satisfy the logic checker
                message = 'none'
            for user in waitlist.users.all():
                notifications = json.loads(user.notifications.notifications)
                notif_id = 0
                if notifications:
                    notif_id = notifications[-1].get('id') + 1
                new_notification = create_notification(
                    notif_type='waitlist',
                    notif_id=notif_id,
                    data={
                        'message': message,
                        'course': waitlist.course.to_dict()
                    })
                notifications.append(new_notification)
                user.notifications.notifications = json.dumps(notifications)
                user.notifications.save()
            waitlist.save()
    [
        waitlist.delete() for waitlist in waitlists
        if waitlist.users.count() == 0
    ]
    return JsonResponse({})
    def post(self, request):
        if request.data.get('crn'):
            waitlist, created = Waitlist.objects.get_or_create(
                school='uc_merced',
                course=Course.objects.get(crn=request.data.get('crn')))
            waitlist.users.add(request.user)
            waitlist.save()
            notifications = json.loads(
                request.user.notifications.notifications)
            notif_id = 0
            if notifications:
                notif_id = notifications[-1].get('id') + 1

            notifications.append(
                create_notification(
                    notif_type='message',
                    notif_id=notif_id,
                    data={
                        'message':
                        'You will be notified when course {} is open'.format(
                            request.data.get('crn'))
                    }))
            request.user.notifications.notifications = json.dumps(
                notifications)
            request.user.notifications.save()
            return Response({'success': True})
        return Response({
            "error_title": "no_crn",
            "error_description": "CRN not provided",
            "error_code": 110
        })
Beispiel #3
0
def save_profile(sender, instance, raw, using, update_fields, **kwargs):
    if not hasattr(instance, 'scheduleuser') or not instance.scheduleuser:
        random_key = ''.join(choices(ascii_uppercase + digits, k=25))
        instance.scheduleuser = ScheduleUser.objects.create(user=instance, unique_id=random_key)
    if not hasattr(instance, 'notifications') or not instance.notifications:
        instance.notifications = Notifications.objects.create(
            user=instance,
            notifications=json_dumps([
                create_notification(notif_type='message', notif_id=0,
                                    data={'message': 'Welcome to BobcatCourses'})
            ])
        )