class TrainingEventCreateView(CreateView): form_class = CreateTrainingEventForm model = TrainingEvents template_name = "create_event.html" success_url = "/software-training/" @method_decorator(group_required("Resource Person")) def get(self, request, *args, **kwargs): return render(self.request, self.template_name, {'form': self.form_class()}) def form_valid(self, form, **kwargs): self.object = form.save(commit=False) self.object.entry_user = self.request.user self.object.Language_of_workshop = Language.objects.get(id=22) self.object.save() messages.success(self.request, "New Event created successfully.") return HttpResponseRedirect(self.success_url)
class EventPraticipantsListView(ListView): model = Participant @method_decorator(group_required("Resource Person")) def dispatch(self, *args, **kwargs): self.eventid = self.kwargs['eventid'] self.queryset = Participant.objects.filter(event_id=self.eventid) self.event = TrainingEvents.objects.get(id=self.eventid) today = date.today() self.training_status = 0 #ongoing if self.event.event_end_date < today: self.training_status = 1 #completed return super(EventPraticipantsListView, self).dispatch(*args, **kwargs) def get_context_data(self, **kwargs): context = super(EventPraticipantsListView, self).get_context_data(**kwargs) context['training_status']= self.training_status context['eventid']= self.eventid context['event']= self.event return context
class ParticipantTransactionsListView(ListView): model = PaymentTransaction raw_get_data = None header = None collection = None @method_decorator(group_required("Resource Person","Administrator")) def dispatch(self, *args, **kwargs): today = date.today() statenames = State.objects.filter(resourceperson__user_id=self.request.user, resourceperson__status=1).values('name') self.PaymentTransaction = PaymentTransaction.objects.filter(paymentdetail__state__in=statenames).order_by('-created') self.events = self.PaymentTransaction self.header = { 1: SortableHeader('#', False), 2: SortableHeader( 'paymentdetail__user__first_name', True, 'First Name' ), 3: SortableHeader( 'paymentdetail__user__last_name', True, 'Last Name' ), 4: SortableHeader( 'paymentdetail__email', True, 'Email' ), 5: SortableHeader( 'paymentdetail__state', True, 'State' ), 6: SortableHeader('transId', True, 'Transaction id'), 7: SortableHeader('paymentdetail__user_id', True, 'UserId'), 8: SortableHeader('refNo', True, 'Reference No.'), 9: SortableHeader('status', True, 'Status'), 10: SortableHeader('paymentdetail__purpose', True, 'Purpose'), 11: SortableHeader('requestType', True, 'RequestType'), 12: SortableHeader('amount', True, 'Amount'), 13: SortableHeader('created', True, 'Entry Date'), 14: SortableHeader('paymentdetail__user', True, 'Phone'), } self.raw_get_data = self.request.GET.get('o', None) self.purpose = self.request.GET.get('paymentdetail__purpose') if self.purpose != 'cdcontent': self.events= self.events.filter().exclude(paymentdetail__purpose='cdcontent') self.queryset = get_sorted_list( self.request, self.events, self.header, self.raw_get_data ) self.collection= PaymentTransFilter(self.request.GET, queryset=self.queryset, user=self.request.user) self.total_amount = self.collection.qs.filter(requestType='R').aggregate(Sum('amount')) return super(ParticipantTransactionsListView, self).dispatch(*args, **kwargs) def get_context_data(self, **kwargs): context = super(ParticipantTransactionsListView, self).get_context_data(**kwargs) context['form'] = self.collection.form page = self.request.GET.get('page') collection = get_page(self.collection.qs, page) context['collection'] = collection context['header'] = self.header context['ordering'] = get_field_index(self.raw_get_data) context['events'] = self.events context['total_amount']=self.total_amount if self.request.user: context['user'] = self.request.user return context
class ParticipantCreateView(CreateView): form_class = UploadParticipantsForm @method_decorator(group_required("Resource Person")) def dispatch(self, *args, **kwargs): if 'eventid' in kwargs: try: self.event = TrainingEvents.objects.get(pk=kwargs['eventid']) except: messages.error(self.request, 'Event not found') return HttpResponseRedirect(reverse("training:create_event")) if not self.event.training_status == 0: messages.error(self.request,'Upoad via CSV is not allowed as Event registration is closed') return super(ParticipantCreateView, self).dispatch(*args, **kwargs) def get_context_data(self, **kwargs): context = super(ParticipantCreateView, self).get_context_data(**kwargs) context['event']= self.event return context def form_valid(self, form): count = 0 csv_file_data = form.cleaned_data['csv_file'] registartion_type = form.cleaned_data['registartion_type'] if registartion_type == 2: # 3 - Manual Registration via CSV(option not visible outside) registartion_type = 3 rows_data = csv.reader(csv_file_data, delimiter=',', quotechar='|') csv_error = False for i, row in enumerate(rows_data): user = self.get_create_user(row) try: college = AcademicCenter.objects.get(academic_code=row[6]) except AcademicCenter.DoesNotExist: csv_error = True messages.add_message(self.request, messages.ERROR, "Row: "+ str(i+1) + " Institution name " + row[6] + " does not exist."+" Participant "+ row[2] + " was not created.") continue if registartion_type == 1: if not(is_college_paid(college.id)): messages.add_message(self.request, messages.ERROR, "Row: "+ str(i+1) + " Institution " + row[6] + " is not a Paid college."+" Participant "+ row[2] + " was not created.") continue try: foss_language = Language.objects.get(name=row[7].strip()) except : messages.add_message(self.request, messages.ERROR, "Row: "+ str(i+1) + " Language name " + row[7] + " does not exist."+" Participant "+ row[2] + " was not created.") continue participant = Participant.objects.filter(email=row[2].strip(),event = self.event) if participant.exists() and registartion_successful(user, self.event): messages.add_message(self.request, messages.WARNING, "Participant with email "+row[2]+" already registered for "+self.event.event_name) continue else: try: Participant.objects.create( name = row[0]+" "+row[1], email = row[2].strip(), gender = row[3], amount = row[4], event = self.event, user = user, state = college.state, college = college, foss_language = foss_language, registartion_type = registartion_type, reg_approval_status = 1 ) count = count + 1 except : csv_error = True messages.add_message(self.request, messages.ERROR, "Could not create participant having email id" + row[2]) if csv_error: messages.warning(self.request, 'Some rows in the csv file has errors and are not created.') if count > 0: messages.success(self.request, 'Successfully uploaded '+str(count)+" participants") return HttpResponseRedirect(reverse("training:upload_participants", kwargs={'eventid': self.event.pk})) def get_create_user(self, row): try: return User.objects.get(email=row[2].strip()) except User.DoesNotExist: user = User(username=row[2], email=row[2].strip(), first_name=row[0], last_name=row[1]) user.set_password(row[0]+'@ST'+str(random.random()).split('.')[1][:5]) user.save() create_profile(user, '') send_registration_confirmation(user) return user