Commit 787c83d5 authored by KUNAL GOYAL's avatar KUNAL GOYAL

some changes 2

parent 9414fe04
from django import forms
from allocation.models import Applicant,Choice
from allocation.models import *
from django.forms import ModelForm
# class PreferenceForm(forms.Form):
......@@ -10,4 +10,13 @@ from django.forms import ModelForm
# class PreferenceForm(ModelForm):
# class Meta:
# model = Applicant
# fields = ['name', 'rank', 'choices']
\ No newline at end of file
# fields = ['name', 'rank', 'choices']
# class AppForm(ModelForm):
# def __init__(self, *args, **kwargs):
# super(AppForm, self).__init__(*args, **kwargs)
# applicant=Applicant.objects.filter(name=self.request.user.username)
# self.fields['choices'].queryset = Choice.objects.filter(institute=applicant__institute)
# class Meta:
# model = Application
# fields = ['applicant','choice', 'priority']
\ No newline at end of file
from __future__ import unicode_literals
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
# import CsvModel
import csv
import os
......@@ -28,7 +29,7 @@ class Applicant(models.Model):
choices = models.ManyToManyField(Choice, through='Application', related_name='applicants')
name = models.CharField(max_length=200)
alloted_choice = models.ManyToManyField(Choice, through='Allocationcl', related_name='alloted_applicant')
rank = models.IntegerField()
rank = models.IntegerField(validators=[MinValueValidator(1)])
def __str__(self):
return self.name
def ordered_choices(self):
......@@ -39,7 +40,7 @@ class Applicant(models.Model):
class Application(models.Model):
applicant = models.ForeignKey(Applicant, on_delete=models.CASCADE)
choice = models.ForeignKey(Choice, on_delete=models.CASCADE)
priority = models.IntegerField()
priority = models.IntegerField(validators=[MinValueValidator(1)])
class Meta:
# if you have an inline configured in the admin, this will
# make the roles order properly
......
......@@ -11,7 +11,10 @@
{% if applicant.institute.name == request.user.username %}
<p><b>Name:</b> {{ applicant.name }}</p>
<p><b>Rank:</b> {{ applicant.rank }}</p>
<p><b>Allotted Choice:</b> {{ applicant.alloted_choice }}
<p><b>Allotted Choice:</b>
{% for choice in applicant.alloted_choice.all %}
{{choice}}
{% endfor %}
{% else %}
Unauthorised access
{% endif %}
......
......@@ -37,24 +37,32 @@
<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
{% block content %}
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% if success %}
<p style="color:green">update successfull</p>
{% endif %}
<p> <b>Name:</b> {{applicant.name}}<br>
<b>Rank:</b> {{applicant.rank}}<br>
<b>Allocated choice:</b>
{% for choice in applicant.alloted_choice.all %}
{{choice}}
{{choice}}
{% endfor %}
</p>
<form action="/allocation/submit" method="post">{% csrf_token %}
<form action="/allocation/" method="post">{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{{ form }}<br>
{{ form.non_field_errors }}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<!-- {{ form.applicant }} {{ form.choice }} {{ form.priority }}<br> -->
<br>
{% endfor %}
<input type="submit" value="Submit">
</form>
......@@ -63,7 +71,6 @@
</div>
<!-- END Content -->
{% block footer %}<div id="footer"></div>{% endblock %}
</div>
</body>
......
......@@ -6,7 +6,7 @@ from django.contrib.auth import views as auth_views
app_name = 'allocation'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^submit$', views.submit, name='submit'),
#url(r'^submit$', views.submit, name='submit'),
url(r'^admin$', views.admin1, name='admin1'),
url(r'^admin/choices$', views.ChoiceListView.as_view(), name='choice_list'),
url(r'^admin/applicants$', views.ApplicantListView.as_view(), name='applicant_list'),
......
......@@ -3,9 +3,8 @@ from django.contrib.auth import authenticate
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from allocation.models import *
#from allocation.forms import PreferenceForm
from django.forms import modelform_factory
from django.forms import modelformset_factory
#from allocation.forms import AppForm
from django.forms import modelformset_factory, formset_factory, modelform_factory, ModelForm
from django.views import generic
from django.views.generic.edit import *
......@@ -51,7 +50,7 @@ def choice_make(request):
file_data = csvfile.read().decode("utf-8")
lines = file_data.split("\n")
user_name = request.user.username
institute = Institute.objects.filter(name=user_name)[0]
institute = get_object_or_404(Institute, name=user_name)
for line in lines:
line = line.split(',')
# tmp = Choice.objects.create(choice_name=line[0],capacity=line[1])
......@@ -66,7 +65,7 @@ def applicant_make(request):
csvfile = request.FILES['myfile']
file_data = csvfile.read().decode("utf-8")
user_name = request.user.username
institute = Institute.objects.filter(name=user_name)[0]
institute = get_object_or_404(Institute, name=user_name)
lines = file_data.split("\n")
for line in lines:
line = line.split(',')
......@@ -80,35 +79,58 @@ def applicant_make(request):
return HttpResponse("Error")
AppFormset = modelformset_factory(Application,fields=("choice", "priority" ), extra=5)
#AppFormset = modelformset_factory(Application,fields=("choice", "priority" ), extra=5)
# @login_required(login_url='/admin/login')
# def index(request): #for /allocation
# applicant=get_object_or_404(Applicant, name=request.user.username)
# # form1 = modelform_factory(Applicant, fields=("name", "rank" ))
# formset = AppFormset(queryset=Application.objects.filter(applicant=applicant),)
# return render(request, 'allocation/index.html', {'applicant': applicant, 'formset': formset,})
# AppFormset = formset_factory(AppForm, extra=5)
# @login_required(login_url='/admin/login')
# def index(request): #for /allocation
# applicant=get_object_or_404(Applicant,name=request.user.username)
# # form1 = modelform_factory(Applicant, fields=("name", "rank" ))
# formset = AppFormset()
# return render(request, 'allocation/index.html', {'applicant': applicant, 'formset': formset,})
@login_required(login_url='/admin/login')
def index(request): #for /allocation
applicant=get_object_or_404(Applicant, name=request.user.username)
# form1 = modelform_factory(Applicant, fields=("name", "rank" ))
formset = AppFormset(queryset=Application.objects.filter(applicant=applicant),)
return render(request, 'allocation/index.html', {'applicant': applicant, 'formset': formset,})
# def submit(request):
# if request.method == 'POST':
# formset = AppFormset(request.POST)
# if formset.is_valid():
# for form in formset:
# if form.is_valid():
# cd = f.cleaned_data
# application.applicant=cd.get('applicant')
# application.choice=cd.get('choice')
# application.priority=cd.get('priority')
# application.save()
# else:
# return HttpResponse('error')
def submit(request):
if request.method == 'POST':
formset = AppFormset(request.POST)
instances = formset.save()
return HttpResponse("successful")
# if request.method == 'POST':
# formset = AppFormset(request.POST)
# instances = formset.save()
# return HttpResponse("successful")
def make_application_form(applicant1):
class ApplicationForm(forms.ModelForm):
choice = forms.ModelChoiceField(queryset=Choice.objects.filter(institute=applicant1.institute))
applicant = forms.ModelChoiceField(widget=forms.HiddenInput(), initial=applicant1, queryset=Applicant.objects.all() )
class Meta:
model = Application
fields = ['applicant','choice', 'priority']
return ApplicationForm
@login_required(login_url='/admin/login')
def index(request):
applicant=get_object_or_404(Applicant,name=request.user.username)
ApplicationForm = make_application_form(applicant)
ApplicationFormSet = modelformset_factory(Application, form=ApplicationForm, extra=5)
if request.method == "POST":
formset = ApplicationFormSet(request.POST, queryset=Application.objects.filter(applicant=applicant))
if formset.is_valid():
#Application.objects.filter(applicant=applicant).delete()
formset.save()
return render(request, 'allocation/index.html', {'applicant': applicant, 'formset': formset, 'success':True,})
else:
form_errors = formset.errors
return render(request, 'allocation/index.html', {'applicant': applicant, 'formset': formset,'form_errors': form_errors})
else:
formset = ApplicationFormSet(queryset=Application.objects.filter(applicant=applicant),)
return render(request, 'allocation/index.html', {'applicant': applicant, 'formset': formset,})
@login_required(login_url='/admin/login')
def admin1(request):
......
No preview for this file type
......@@ -31,7 +31,6 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'guardian',
'allocation.apps.AllocationConfig',
'django.contrib.admin',
'django.contrib.auth',
......@@ -124,4 +123,6 @@ STATIC_URL = '/static/'
AUTHENTICATION_BACKENDS = ( #django-guardian
'django.contrib.auth.backends.ModelBackend', # this is default
'guardian.backends.ObjectPermissionBackend',
)
\ No newline at end of file
)
LOGOUT_REDIRECT_URL = '/'
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment