Skip to content

Commit

Permalink
Merge pull request #3 from Pancham1603/production
Browse files Browse the repository at this point in the history
Production merge
  • Loading branch information
Pancham1603 authored Mar 12, 2024
2 parents 6d92243 + 02519a6 commit cc25539
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 14 deletions.
19 changes: 12 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM python:3.8.16

# Install cron
# RUN apt-get update && apt-get install -y cron
RUN apt-get update && apt-get install -y cron

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
Expand All @@ -17,14 +17,19 @@ RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app

# # Copy cron job file
# COPY cronjob /etc/cron.d/cronjob
# Copy cron job file
COPY cronjob /etc/cron.d/cronjob

# # Give execution rights on the cron job
# RUN chmod 0644 /etc/cron.d/cronjob
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cronjob

# # Create the log file to be able to run tail
# RUN touch /var/log/cron.log
# Create the log file to be able to run tail
RUN touch /var/log/cron.log

RUN crontab /etc/cron.d/cronjob

# Run cron in the foreground
CMD ["cron", "-f"]

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 4.2.7 on 2024-02-03 22:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('global_settings', '0003_alter_settings_options_settings_max_violation_count'),
]

operations = [
migrations.AddField(
model_name='settings',
name='enable_hostel_limits',
field=models.BooleanField(default=False, help_text='Limit the number of students eligible for a particular hostel. Limit can be set from Hostel profile.'),
),
migrations.AddField(
model_name='settings',
name='enable_yearwise_limits',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='settings',
name='first_year',
field=models.IntegerField(blank=True, default=0, null=True),
),
migrations.AddField(
model_name='settings',
name='fourth_year',
field=models.IntegerField(blank=True, default=0, null=True),
),
migrations.AddField(
model_name='settings',
name='second_year',
field=models.IntegerField(blank=True, default=0, null=True),
),
migrations.AddField(
model_name='settings',
name='third_year',
field=models.IntegerField(blank=True, default=0, null=True),
),
migrations.AlterField(
model_name='settings',
name='enable_hostel_timers',
field=models.BooleanField(default=False, help_text='Enabling activates the time limits specified in the Hostel profile. The timers below, however, will not be operational.'),
),
]
10 changes: 9 additions & 1 deletion apps/global_settings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

class Settings(models.Model):

enable_hostel_timers = models.BooleanField(default=False)
enable_hostel_limits = models.BooleanField(default=False, help_text="Limit the number of students eligible for a particular hostel. Limit can be set from Hostel profile.")
enable_hostel_timers = models.BooleanField(default=False, help_text="Enabling activates the time limits specified in the Hostel profile. The timers below, however, will not be operational.")
frontend_checkin_timer = models.IntegerField(blank=True, null=True, default=0)
backend_checkin_timer = models.IntegerField(blank=True, null=True, default=0)

Expand All @@ -14,6 +15,13 @@ class Settings(models.Model):
female_ratio = models.FloatField(blank=True, null=True, default=0.5)

max_violation_count = models.IntegerField(blank=True, null=True, default=3)

enable_yearwise_limits = models.BooleanField(default=False)
first_year = models.IntegerField(blank=True, null=True, default=0)
second_year = models.IntegerField(blank=True, null=True, default=0)
third_year = models.IntegerField(blank=True, null=True, default=0)
fourth_year = models.IntegerField(blank=True, null=True, default=0)


class Meta:
verbose_name_plural = 'Settings'
10 changes: 10 additions & 0 deletions apps/nightpass/management/commands/start_booking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.core.management.base import BaseCommand
from ...models import CampusResource

class Command(BaseCommand):
help = 'Start booking for all campus resources'

def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('Running your cron job...'))
CampusResource.objects.all().update(is_booking=True, booking_complete=False)
self.stdout.write(self.style.SUCCESS('Cron job completed successfully'))
18 changes: 18 additions & 0 deletions apps/nightpass/migrations/0004_hostel_max_students_allowed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-02-03 22:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('nightpass', '0003_hostel_backend_checkin_timer_and_more'),
]

operations = [
migrations.AddField(
model_name='hostel',
name='max_students_allowed',
field=models.IntegerField(blank=True, default=0, null=True),
),
]
2 changes: 2 additions & 0 deletions apps/nightpass/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Hostel(models.Model):
frontend_checkin_timer = models.IntegerField(blank=True, null=True, default=0)
backend_checkin_timer = models.IntegerField(blank=True, null=True, default=0)

max_students_allowed = models.IntegerField(blank=True, null=True, default=0)

def __str__(self):
return self.name

Expand Down
49 changes: 47 additions & 2 deletions apps/nightpass/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@
from ..users.views import *
from datetime import datetime, date, timedelta



sem_vs_year = {
"1": 1,
"2": 1,
"3": 2,
"4": 2,
"5": 3,
"6": 3,
"7": 4,
"8": 4,
}

@login_required
def campus_resources_home(request):
Expand Down Expand Up @@ -80,6 +88,43 @@ def generate_pass(request, campus_resource):
}
return HttpResponse(json.dumps(data))

if Settings.enable_yearwise_limits:
data = {
'status':False,
'message':'All slots are booked for today!'
}
student_year = sem_vs_year[user.student.semester]
if student_year == 1:
student_year_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource,
user__student__semester__in=['1','2']).count()
if student_year_pass_count >= Settings.first_year:
return HttpResponse(json.dumps(data))
elif student_year == 2:
student_year_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource,
user__student__semester__in=['3','4']).count()
if student_year_pass_count >= Settings.second_year:
return HttpResponse(json.dumps(data))
elif student_year == 3:
student_year_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource,
user__student__semester__in=['5','6']).count()
if student_year_pass_count >= Settings.third_year:
return HttpResponse(json.dumps(data))
elif student_year == 4:
student_year_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource,
user__student__semester__in=['7','8']).count()
if student_year_pass_count >= Settings.fourth_year:
return HttpResponse(json.dumps(data))

if Settings.enable_hostel_limits:
student_hostel_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource,
user__student__hostel=user.student.hostel).count()
if student_hostel_pass_count >= user.student.hostel.max_students_allowed:
data = {
'status':False,
'message':'All slots are booked for today!'
}
return HttpResponse(json.dumps(data))

if int(user.student.violation_flags) >= int(Settings.max_violation_count):
data = {
'status':False,
Expand Down
26 changes: 23 additions & 3 deletions apps/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,33 @@
from datetime import date, datetime
import io


class YearWiseFilter(admin.SimpleListFilter):
title = 'Year'
parameter_name = 'year'

def lookups(self, request, model_admin):
return (
('1', '1st Year'),
('2', '2nd Year'),
('3', '3rd Year'),
('4', '4th Year'),
)

def queryset(self, request, queryset):
if self.value() == '1':
return queryset.filter(user__student__semester__in=['1','2'])
if self.value() == '2':
return queryset.filter(user__student__semester__in=['3','4'])
if self.value() == '3':
return queryset.filter(user__student__semester__in=['5','6'])
if self.value() == '4':
return queryset.filter(user__student__semester__in=['7','8'])

class NightPassAdmin(admin.ModelAdmin):
list_display = ( 'name','user','hostel','date', 'campus_resource','hostel_check_out', 'check_in', 'check_out', 'hostel_check_in', 'defaulter')
search_fields = ('user__student__name','user__student__registration_number','user__student__email')
actions = ['export_as_xlsx']
list_filter = (('date', DateRangeFilter),'campus_resource','user__student__gender','user__student__hostel', 'defaulter', 'check_in', 'check_out')
list_filter = (('date', DateRangeFilter),'campus_resource','user__student__gender','user__student__hostel', YearWiseFilter,'defaulter', 'check_in', 'check_out')
autocomplete_fields = ('user', 'campus_resource')
readonly_fields = ('pass_id', 'check_in_time', 'check_out_time', 'hostel_checkout_time', 'hostel_checkin_time')

Expand Down Expand Up @@ -83,7 +103,7 @@ class StudentAdmin(ImportExportModelAdmin):
autocomplete_fields = ('user',)
resource_class = StudentResource
readonly_fields = ('hostel_checkin_time', 'hostel_checkout_time', 'last_checkout_time',)
list_filter = ('hostel', 'has_booked', 'violation_flags')
list_filter = ('hostel', YearWiseFilter,'has_booked', 'violation_flags')

class SecurityAdmin(admin.ModelAdmin):
list_display = ('name', 'admin_incharge', 'user')
Expand Down
3 changes: 2 additions & 1 deletion apps/users/management/commands/generate_uuids.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ class Command(BaseCommand):

def handle(self, *args, **options):
users_without_uuid = CustomUser.objects.filter(unique_id__isnull=True)

for user in users_without_uuid:
user.unique_id = uuid.uuid4()
user.save()
print(f'[{user.unique_id}] {user.username}')

self.stdout.write(self.style.SUCCESS('UUIDs generated successfully for existing users.'))

0 comments on commit cc25539

Please sign in to comment.