From a5788d511cae356054ca7862140451c250de6cfd Mon Sep 17 00:00:00 2001 From: Varshaa Naganathan Date: Wed, 26 Dec 2012 20:53:38 +0530 Subject: [PATCH 1/4] Shaastra ID creation script added --- forms.py | 3 ++- templates/create_accounts.html | 13 +++++++++++++ urls.py | 1 + views.py | 35 +++++++++++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 templates/create_accounts.html diff --git a/forms.py b/forms.py index 64a0296..6f44f6c 100644 --- a/forms.py +++ b/forms.py @@ -35,4 +35,5 @@ def clean_confirm_password(self): raise forms.ValidationError(u'Passwords do not match') return self.cleaned_data['confirm_password'] - +class FileForm(forms.Form): + files = forms.FileField(required = True) diff --git a/templates/create_accounts.html b/templates/create_accounts.html new file mode 100644 index 0000000..a8914bd --- /dev/null +++ b/templates/create_accounts.html @@ -0,0 +1,13 @@ + + + Shaastra 2013 Control Room + + +

{{msg}}

+
+ {% csrf_token %} + {{ form.as_table }} + +
+ + diff --git a/urls.py b/urls.py index 1d75e38..11f978a 100644 --- a/urls.py +++ b/urls.py @@ -19,6 +19,7 @@ urlpatterns = patterns( '', url(r'^$', 'views.home', name='home'), + url(r'^create_accounts', 'views.create', name='create'), url(r'^spons', 'views.spons', name='spons'), url(r'^user/', include('users.urls')), url(r'^admin/', include('admin.urls')), diff --git a/views.py b/views.py index 42d1b71..4377665 100755 --- a/views.py +++ b/views.py @@ -2,9 +2,13 @@ from django.template.context import Context, RequestContext from django.shortcuts import render_to_response from django.conf import settings +from users.models import UserProfile from events.models import Event, EVENT_CATEGORIES, Tag, Update, Sponsor from django.template.defaultfilters import slugify from events.views import home as events_home +from django.core.mail import send_mail +from django.template.loader import get_template +from forms import * def home(request): fragment = request.GET.get('_escaped_fragment_','') @@ -79,4 +83,33 @@ def method_splitter(request, *args, **kwargs): raise Http404 def landing(request): - return render_to_response('landing.html',locals(),context_instance = RequestContext(request)) \ No newline at end of file + return render_to_response('landing.html',locals(),context_instance = RequestContext(request)) + +def create(request): + form = FileForm() + if request.method == 'POST': + form = FileForm(request.POST, request.FILES) + if form.is_valid(): + line_number = 0 + for line in form.cleaned_data['files']: + line = line.replace('\n', '').replace('\r', '') + if line == '': + continue + line_number += 1 + try: + new = User.objects.get(email = line) + except: + new = User( + username = line.split('@')[0].lower(), + email = line + ) + new.set_password(line.split('@')[0].lower()) + new.save() + x = 1300000 + new.id + new_profile = UserProfile(user = new, + shaastra_id = ("SHA" + str(x))) + new_profile.save() + msg = "Account created" + return render_to_response('create_accounts.html', locals(), + context_instance=RequestContext(request)) + From bdcb7c240218c1e423ea131996a7a2f94f54b229 Mon Sep 17 00:00:00 2001 From: Varshaa Naganathan Date: Fri, 28 Dec 2012 22:06:06 +0530 Subject: [PATCH 2/4] Bill generation for hospi --- controlroom/ajax.py | 12 +-- controlroom/generate_bill.py | 149 +++++++++++++++++++++++++++++++++++ forms.py | 1 + urls.py | 2 +- views.py | 12 ++- 5 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 controlroom/generate_bill.py diff --git a/controlroom/ajax.py b/controlroom/ajax.py index 2498543..50196c8 100644 --- a/controlroom/ajax.py +++ b/controlroom/ajax.py @@ -12,6 +12,7 @@ from django.contrib.sitemaps import ping_google from operator import attrgetter from datetime import datetime +from controlroom.generate_bill import * @dajaxice_register def save_individual_checkin(request,form): @@ -104,9 +105,10 @@ def send_participants(request,form): @dajaxice_register def GenerateBill(request,s_id=''): dajax = Dajax() - checkedin = IndividualCheckIn.objects.get(shaastra_ID=s_id) - response = HttpResponse(mimetype='application/pdf') - response['Content-Disposition'] = \ - 'attachment; filename=Accomodation Bill.pdf' - pdf = canvas.Canvas(response, pagesize=A4) + try: + pdf = generateParticipantPDF(s_id) + return HttpResponse(pdf) + except: + return dajax.json + diff --git a/controlroom/generate_bill.py b/controlroom/generate_bill.py new file mode 100644 index 0000000..f7e1c89 --- /dev/null +++ b/controlroom/generate_bill.py @@ -0,0 +1,149 @@ +from users.models import UserProfile, College, Team +from django.contrib.auth.models import User +from events.models import Event, EventSingularRegistration +from controlroom.models import * + +from django.contrib.auth.decorators import login_required +from django.core.mail import EmailMultiAlternatives +from django.http import HttpResponseForbidden, HttpResponse + +from reportlab.pdfgen import canvas +from reportlab.lib.pagesizes import A4 +from reportlab.lib.units import cm +from reportlab.lib import colors +from reportlab.platypus import Table, TableStyle +from reportlab.pdfbase.pdfmetrics import getFont, getAscentDescent +from reportlab.platypus import Paragraph +from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet + +def PDFSetFont(pdf, font_name, font_size): + """ + Sets the font and returns the lineheight. + """ + + pdf.setFont(font_name, font_size) + (ascent, descent) = getAscentDescent(font_name, font_size) + return ascent - descent # Returns line height + +def initNewPDFPage(pdf, page_title, page_no, (pageWidth, pageHeight),): + """ + Paints the headers on every new page of the PDF document. + Also returns the coordinates (x, y) where the last painting operation happened. + """ + + y = pageHeight + + # Leave a margin of one cm at the top + + y = pageHeight - cm + + # Set font for 'SHAASTRA 2013' + + lineheight = PDFSetFont(pdf, 'Times-Roman', 18) + + # SHAASTRA 2013 in centre + + pdf.drawCentredString(pageWidth / 2, y, 'SHAASTRA 2013') + y -= lineheight + cm + + # Set font for Page Title + + lineheight = PDFSetFont(pdf, 'Times-Roman', 16) + + # Page Title in next line, centre aligned + + pdf.drawCentredString(pageWidth / 2, y, page_title) + + # Set font for Document Title + + PDFSetFont(pdf, 'Times-Roman', 9) + + # Page number in same line, right aligned + + pdf.drawRightString(pageWidth - cm, y, '#%d' % page_no) + + y -= lineheight + cm + + return y + +def paintParagraph(pdf, x, y, text): + + styles = getSampleStyleSheet() + styles.add(ParagraphStyle(name = 'paraStyle', fontSize = 12)) + + p = Paragraph(text, styles['paraStyle'], None) # None for bullet type + + (A4Width, A4Height) = A4 + availableWidth = A4Width - 2 * cm # Leaving margins of 1 cm on both sides + availableHeight = y + (paraWidth, paraHeight) = p.wrap(availableWidth, availableHeight) # find required space + + p.drawOn(pdf, x, y - paraHeight) + + y -= paraHeight + cm + + return y + +def printParticipantDetails(pdf, x, y, s_id): + try: + checkedin = IndividualCheckIn.objects.get(shaastra_ID=s_id) + except: + msg = "This participant has not been checked in!" + + lineheight = PDFSetFont(pdf, 'Times-Roman', 12) + + pdf.drawString(x, y, 'Shaastra ID: %s' % checkedin.shaastra_ID) + + y -= lineheight + (cm * 0.8) + + pdf.drawString(x, y, 'Name: %s %s' % (checkedin.first_name, checkedin.last_name)) + + y -= lineheight + (cm * 0.8) + + + pdf.drawString(x, y, 'Mobile No: %s' % checkedin.phone_no) + + y -= lineheight + (cm * 0.8) + + return y + +def generateParticipantPDF(s_id): + + userProfile = UserProfile.objects.get(shaastra_id = s_id) + + response = HttpResponse(mimetype='application/pdf') + response['Content-Disposition'] = \ + 'filename=Accomodation Bill.pdf' + + # Create the PDF object, using the response object as its "file." + + pdf = canvas.Canvas(response, pagesize=A4) + + # Define the title of the document as printed in the document header. + + page_title = 'Accomodation Bill' + + # Get the width and height of the page. + + (A4Width, A4Height) = A4 + + # Page number + + pageNo = 1 + + # Paint the headers and get the coordinates + + y = initNewPDFPage(pdf, page_title, pageNo, A4) + + # Setting x to be a cm from the left edge + + x = cm + + # Print Participant Details in PDF + + y = printParticipantDetails(pdf, x, y, userProfile.shaastra_id) + + pdf.showPage() + pdf.save() + + return response diff --git a/forms.py b/forms.py index 6f44f6c..d783770 100644 --- a/forms.py +++ b/forms.py @@ -36,4 +36,5 @@ def clean_confirm_password(self): return self.cleaned_data['confirm_password'] class FileForm(forms.Form): + event_id = forms.IntegerField() files = forms.FileField(required = True) diff --git a/urls.py b/urls.py index 095de37..ebc4167 100644 --- a/urls.py +++ b/urls.py @@ -19,7 +19,7 @@ urlpatterns = patterns( '', url(r'^$', 'views.home', name='home'), - url(r'^create_accounts', 'views.create', name='create'), + url(r'^create_accounts/', 'views.create', name='create'), url(r'^spons', 'views.spons', name='spons'), url(r'^user/', include('users.urls')), url(r'^admin/', include('admin.urls')), diff --git a/views.py b/views.py index cae84f3..d48af27 100755 --- a/views.py +++ b/views.py @@ -87,10 +87,11 @@ def landing(request): return render_to_response('landing.html',locals(),context_instance = RequestContext(request)) def create(request): - form = FileForm() + form = FileForm() if request.method == 'POST': form = FileForm(request.POST, request.FILES) if form.is_valid(): + evt = Event.objects.get(id = form.cleaned_data['event_id']) line_number = 0 for line in form.cleaned_data['files']: line = line.replace('\n', '').replace('\r', '') @@ -99,6 +100,12 @@ def create(request): line_number += 1 try: new = User.objects.get(email = line) + if not evt.team_event: + try: + event = EventSingularRegistration.objects.get(user = new, event = evt) + except: + event = EventSingularRegistration(user = new, event = evt) + event.save() except: new = User( username = line.split('@')[0].lower(), @@ -110,6 +117,9 @@ def create(request): new_profile = UserProfile(user = new, shaastra_id = ("SHA" + str(x))) new_profile.save() + if not evt.team_event: + event = EventSingularRegistration(user = new, event = evt) + event.save() msg = "Account created" return render_to_response('create_accounts.html', locals(), context_instance=RequestContext(request)) From 8dcbfc0d760d6f60833770830a2a5ef9a45e55fa Mon Sep 17 00:00:00 2001 From: Varshaa Naganathan Date: Fri, 28 Dec 2012 22:29:45 +0530 Subject: [PATCH 3/4] Search with email added to hospi --- controlroom/forms.py | 3 ++- controlroom/views.py | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/controlroom/forms.py b/controlroom/forms.py index 6552100..e18c763 100644 --- a/controlroom/forms.py +++ b/controlroom/forms.py @@ -62,7 +62,8 @@ class AddMultipleRoomsForm(forms.Form): rooms = forms.FileField(required = True) class ShaastraIDForm(forms.Form): - shaastraID = forms.CharField(help_text = 'Enter Shaastra ID of participant') + shaastraID = forms.CharField(required = False,help_text = 'Enter Shaastra ID') + email = forms.CharField(required = False,help_text = 'Enter Email ID') class IndividualForm(ModelForm): room = chosenforms.ChosenModelChoiceField(queryset=AvailableRooms.objects.filter(already_checkedin__lt=F('max_number')).order_by('hostel')) diff --git a/controlroom/views.py b/controlroom/views.py index dfa356a..b74e27c 100644 --- a/controlroom/views.py +++ b/controlroom/views.py @@ -111,9 +111,13 @@ def individual(request): form = ShaastraIDForm(request.POST) if form.is_valid(): inputs = form.cleaned_data - participant = UserProfile.objects.get(shaastra_id=inputs['shaastraID']) + if inputs['shaastraID']: + participant = UserProfile.objects.get(shaastra_id=inputs['shaastraID']) + else: + usr = User.objects.get(email = inputs['email']) + participant = UserProfile.objects.get(user = usr) try: - checkedin = IndividualCheckIn.objects.get(shaastra_ID=inputs['shaastraID']) + checkedin = IndividualCheckIn.objects.get(shaastra_ID=participant.shaastra_id) msg = "This participant is already checked-in!" return render_to_response('controlroom/shaastraIDform.html', locals(), context_instance=RequestContext(request)) @@ -141,9 +145,13 @@ def team(request): if form.is_valid(): inputs = form.cleaned_data try: - leader = UserProfile.objects.get(shaastra_id=inputs['shaastraID']) + try: + leader = UserProfile.objects.get(shaastra_id=inputs['shaastraID']) + except: + usr = User.objects.get(email = inputs['email']) + leader = UserProfile.objects.get(user = usr) except: - msg = "This shaastra ID does not exist" + msg = "This participant does not exist" return render_to_response('controlroom/shaastraIDform.html', locals(), context_instance=RequestContext(request)) check = 0 @@ -209,9 +217,16 @@ def CheckOut(request): form = ShaastraIDForm(request.POST) if form.is_valid(): inputs = form.cleaned_data - participant = UserProfile.objects.get(shaastra_id=inputs['shaastraID']) + if inputs['shaastraID']: + participant = UserProfile.objects.get(shaastra_id=inputs['shaastraID']) + else: + usr = User.objects.get(email = inputs['email']) + participant = UserProfile.objects.get(user = usr) + s_id = participant.shaastra_id + checkedin = IndividualCheckIn.objects.get(shaastra_ID=s_id) + print s_id try: - checkedin = IndividualCheckIn.objects.get(shaastra_ID=inputs['shaastraID']) + checkedin = IndividualCheckIn.objects.get(shaastra_ID=s_id) if checkedin.check_out_date: msg = "This participant is already checked-out!" return render_to_response('controlroom/shaastraIDform.html', locals(), From c950186a4f8387de3d16c7979d6ac553a65bce07 Mon Sep 17 00:00:00 2001 From: Anant Girdhar Date: Fri, 28 Dec 2012 22:36:21 +0530 Subject: [PATCH 4/4] Participant PDFs: Save PDF to server. --- participantPdfs.py | 60 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/participantPdfs.py b/participantPdfs.py index d036afb..886d92c 100644 --- a/participantPdfs.py +++ b/participantPdfs.py @@ -221,6 +221,7 @@ def generateParticipantPDF(user): if (not singularEventRegistrations) and (not userTeams): # The user is not registered for any event. + return None y -= cm * 0.5 pdf.drawString(x, y, 'You are not registered for any events this Shaastra') @@ -281,6 +282,63 @@ def mailParticipantPDFs(request): for participant in participants: pdf = generateParticipantPDF(participant) + if pdf is None: + continue mailPDF(participant, pdf) - return HttpResponse('Mails sent. Timestamp: %s' % str(datetime.datetime.now)) + return HttpResponse('Mails sent. Timestamp: %s' % str(datetime.datetime.now())) + +def savePDF(pdf, sID): + + destination = open('/home/shaastra/hospi/participantPDFs/'+sID+'.pdf', 'wb+') + for chunk in pdf.chunks(): + destination.write(chunk) + destination.close() + print 'File '+sID+'.pdf saved.' + +@login_required +def generateParticipantPDFs(request): + + if not request.user.is_superuser: + return HttpResponseForbidden('The participant mailer can only be accessed by superusers. You don\'t have enough permissions to continue.') + + participants = [] + userProfilesWithShaastraIds = UserProfile.objects.exclude(shaastra_id = '') #TODO Exclude non active users?? + participantProfilesWithShaastraIds = userProfilesWithShaastraIds.exclude(is_core = True).filter(is_coord_of = None) + for profile in participantProfilesWithShaastraIds: + try: + u = profile.user + except: + continue + participants.append(u) + + participants = [User.objects.get(id = 5787)] #TODO: Remove this line for finale + + for participant in participants: + pdf = generateParticipantPDF(participant) + if pdf is None: + continue + savePDF(pdf, participant.get_profile().shaastra_id) + + return HttpResponse('PDFs generated. Timestamp: %s' % str(datetime.datetime.now())) + +def generatePDFs(): + + participants = [] + userProfilesWithShaastraIds = UserProfile.objects.exclude(shaastra_id = '') #TODO Exclude non active users?? + participantProfilesWithShaastraIds = userProfilesWithShaastraIds.exclude(is_core = True).filter(is_coord_of = None) + for profile in participantProfilesWithShaastraIds: + try: + u = profile.user + except: + continue + participants.append(u) + + participants = [User.objects.get(id = 1351)] #TODO: Remove this line for finale + + for participant in participants: + pdf = generateParticipantPDF(participant) + if pdf is None: + continue + savePDF(pdf, participant.get_profile().shaastra_id) +