def prepare_qs_params(self, request, search_term, search_fields): distance_id = request.GET.get('distance_id', None) group = None if distance_id: distance = Distance.objects.get(id=request.GET.get('distance_id')) class_ = load_class(distance.competition.processing_class) processing_class = class_(distance.competition.id) group = processing_class.get_group_for_number_search(distance.id, request.GET.get('gender', ''), request.GET.get('birthday', None)) if search_term: qs = super(NumberMixin, self).prepare_qs_params(request, search_term, search_fields) else: qs = {'or': [], 'and': {}} qs['and'].update({'participant_slug': ''}) if distance_id: qs['and'].update({'distance_id': distance_id}) if group: qs['and'].update({'group': group}) competition_id = request.GET.get('competition_id', None) if competition_id: competition = Competition.objects.get(id=competition_id) qs['and'].update({'distance__competition_id__in': competition.get_ids()}) return qs
def post(self, request, *args, **kwargs): self.competition = Competition.objects.get(id=kwargs.get('pk')) class_ = load_class(self.competition.processing_class) self._competition_class = class_(self.competition.id) if request.POST.get('action') == 'assign_numbers_continuously': self._competition_class.assign_numbers_continuously() elif request.POST.get('action') == 'legacy_sync': messages.add_message(request, messages.INFO, 'Sinhronizācija sākta. Gaidiet e-pastu uz %s ar paziņojumu par beigām.' % request.user.email) legacy_sync.delay(request.user.email) elif request.POST.get('action') == 'start_list': file_obj = create_start_list(competition=self.competition) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=start_list.xls' response.write(file_obj.getvalue()) file_obj.close() return response elif request.POST.get('action') == 'payment_list': file_obj = payment_list(competition=self.competition) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=payment_list.xls' response.write(file_obj.getvalue()) file_obj.close() return response elif request.POST.get('action') == 'create_standing_list': file_obj = create_standing_list(competition=self.competition) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=standing_list.xls' response.write(file_obj.getvalue()) file_obj.close() return response elif request.POST.get('action') == 'team_member_list': file_obj = team_member_list(competition=self.competition) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=team_member_list.xls' response.write(file_obj.getvalue()) file_obj.close() return response elif request.POST.get('action') == 'create_team_list': file_obj = create_team_list(competition=self.competition) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=applied_teams.xls' response.write(file_obj.getvalue()) file_obj.close() return response elif request.POST.get('action') == 'create_insured_list': file_obj = create_insured_list(competition=self.competition) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=insured_list.xls' response.write(file_obj.getvalue()) file_obj.close() return response elif request.POST.get('action') == 'match_applied_to_participants': match_applied_to_participants(competition_id=self.competition.id) messages.info(request, 'Veiksmīgi atjaunots') elif request.POST.get('action') == 'recalculate_all_points': self._competition_class.recalculate_all_points() messages.info(request, 'Veiksmīgi atjaunots') return super(ManageCompetitionDetail, self).get(request, *args, **kwargs)
def forms_valid(self, form, inlines): ret = super(ManageTeamUpdate, self).forms_valid(form, inlines) class_ = load_class(self.object.distance.competition.processing_class) competition_class = class_(competition_id=self.kwargs.get('pk')) competition_class.recalculate_team_result(team=self.object) return ret
def save(self, *args, **kwargs): self.full_name = '%s %s' % (self.first_name, self.last_name) # Full name always should be based on first_name and last_name fields self.team_name_slug = slugify(self.team_name.replace(' ', '')) # In case first name, last name or birthday is changed, then slug changes as well. old_slug = self.slug self.set_slug() if old_slug != self.slug: self.numbers(slug=old_slug).update(participant_slug=self.slug) # We update number slugs to match new slug if self.team: team = self.team self.team = None # Remove connection to existing team member. # Because of slug change connection to team is disconnected. # After disconnection team points are recalculated on every stage team have participated. try: member = team.member_set.get(slug=old_slug) for appl in member.memberapplication_set.all(): class_ = load_class(appl.competition.processing_class) processing_class = class_(competition=appl.competition) processing_class.recalculate_team_result(team=team) appl.participant = None appl.participant_unpaid = None appl.participant_potential = None appl.save() except: pass sebs = self.primary_sebstandings_set.filter(competition_id__in=self.competition.get_ids()) if sebs: seb = sebs[0] seb.participant_slug = self.slug seb.save() self.set_group() if not self.primary_number and self.is_participating: numbers = self.numbers().order_by('-number') if numbers: self.primary_number = numbers[0] if not self.registration_dt: self.registration_dt = timezone.now() # Recalculate totals. # TODO: This should be done when creating payment, not on any save. recalculate_participant(self, commit=False) obj = super(Participant, self).save(*args, **kwargs) if old_slug != self.slug: from team.utils import match_participant_to_applied match_participant_to_applied(self) return obj
def sitetrees_build(): items = [] for competition in Competition.objects.filter(is_in_menu=True): children = [] if competition.processing_class: _class = load_class(competition.processing_class) processing = _class(competition=competition) items.append(processing.build_menu()) else: items.append(item(unicode(competition), '#', url_as_pattern=False, children=children)) return (tree('dynamic_competition', items=items),)
def get(self, *args, **kwargs): self.object = self.get_object() if self.object.competition.processing_class: _class = load_class(self.object.competition.processing_class) else: raise Http404 processing_class = _class(self.object.competition_id) file_obj = processing_class.number_pdf(participant_id=self.object.id) response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename=%s.pdf' % self.object.slug response.write(file_obj.getvalue()) file_obj.close() return response
def match_participant_to_applied(participant): results = participant.result_set.all() for result in results: ma = MemberApplication.objects.filter(competition=result.competition, participant=None, member__slug=participant.slug) if ma: ma1 = ma.get() ma1.participant = participant ma1.save() participant.team = ma1.member.team participant.save() # Recalculate points class_ = load_class(result.competition.processing_class) _competition_class = class_(result.competition.id) _competition_class.recalculate_team_result(team=ma1.member.team)
def update_results_for_result(result): """ Full result recalculation in case points have changed for participant. """ class_ = load_class(result.competition.processing_class) competition_class = class_(competition=result.competition) updated = result.set_all() if updated: print 'points have been updated.' result.save() competition_class.assign_result_place() competition_class.recalculate_standing_for_result(result) competition_class.assign_standing_places() if result.participant.team: competition_class.recalculate_team_result(team=result.participant.team)
def get(self, *args, **kwargs): self.object = self.get_object() if self.object.competition.processing_class: _class = load_class(self.object.competition.processing_class) else: raise Http404 try: processing_class = _class(self.object.competition_id) file_obj = processing_class.generate_diploma(self.object) except: raise Http404 response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename=%s.pdf' % self.object.participant.slug response.write(file_obj.getvalue()) file_obj.close() return response
def __init__(self, competition=None, competition_id=None): if not competition and not competition_id: raise Exception('Expected at least one variable') if not competition: self.competition = Competition.objects.get(id=competition_id) else: self.competition = competition if self.competition.level == 2: self.primary_competition = self.competition.parent else: self.primary_competition = self.competition self.output = StringIO.StringIO() self.doc = SimpleDocTemplate(self.output, pagesize=A4, topMargin=0.2 * inch, bottomMargin=0.8 * inch, leftMargin=0.2 * inch, rightMargin=0.2 * inch, showBoundary=0) class_ = load_class(self.competition.processing_class) self.processing_class = class_(self.competition.id) self.elements = []
def clean(self): super(ParticipantCreateForm, self).clean() distance = self.cleaned_data.get('distance') gender = self.cleaned_data.get('gender') birthday = self.cleaned_data.get('birthday') if distance and gender and birthday: try: competition = Competition.objects.get(id=self.request_kwargs.get('pk')) if competition.processing_class: class_ = load_class(competition.processing_class) processing_class = class_(competition.id) processing_class.assign_group(distance.id, gender, birthday) except: self._errors.update({'distance': ['Dalībnieks nevar startēt šajā distancē.', ]}) return self.cleaned_data
def generate_thumbnails(pk, field, model_class=None): model = load_class(model_class) instance = model._default_manager.get(pk=pk) fieldfile = getattr(instance, field) # optimize file call(["/usr/bin/jpegoptim", fieldfile.path]) generate_all_aliases(fieldfile, include_global=True) try: if model.__name__ == 'Photo': instance.is_processed = True instance.save() album = instance.album if not album.photo_set.filter(is_processed=False).count(): album.is_processed = True album.save() except: pass
def __init__(self, *args, **kwargs): self.participants = kwargs.pop('participants', None) super(ApplicationPayUpdateForm, self).__init__(*args, **kwargs) insured_participants = self.participants.exclude(insurance=None) if insured_participants: self.fields['accept_insurance'].required = True insurance_company = insured_participants[0].insurance.insurance_company terms_doc = "<a href='%s' target='_blank'>Noteikumi</a>" % insurance_company.terms_doc.url if insurance_company.terms_doc else "" self.fields['accept_insurance'].label = mark_safe("%s %s" % (insurance_company.term, terms_doc)) else: self.fields['accept_insurance'].widget = forms.HiddenInput() now = timezone.now() competition = self.instance.competition checkboxes = ( 'accept_terms', 'accept_inform_participants', 'accept_insurance', ) if competition.processing_class: _class = load_class(competition.processing_class) processing = _class(competition=competition) if hasattr(processing, 'payment_additional_checkboxes'): for key, field in processing.payment_additional_checkboxes(application=self.instance): self.fields[key] = field checkboxes += (key, ) payments = competition.activepaymentchannel_set.filter(from_date__lte=now, till_date__gte=now).select_related('payment_channel') # If user have already requested bill, then we are not showing possibility to request one more. if self.instance.external_invoice_code: payments = payments.filter(payment_channel__is_bill=False) self.fields['payment_type'].choices = [(obj.id, obj) for obj in payments] self.fields['donation'].required = False self.helper = FormHelper() self.helper.form_tag = False self.helper.layout = Layout( Row( Column( *checkboxes, css_class='col-sm-8' ), Column( Div( HTML( "<h4>%s: <span id='final_price' data-amount='%s'>%s</span> €</h4>" % (_('Final Price'), self.instance.total_entry_fee + self.instance.total_insurance_fee, floatformat(self.instance.final_price, -2)), ), css_class='margin-bottom-40' ), 'payment_type', Fieldset( _('Invoice Fields'), 'company_name', 'company_vat', 'company_regnr', 'company_address', 'company_juridical_address', 'invoice_show_names', css_class='invoice_fields', ), css_class='col-sm-4', ) ), )
def get_competition_class(self): if not self._competition_class: class_ = load_class(self.competition.processing_class) self._competition_class = class_(self.competition.id) return self._competition_class
def set_group(self): if not self.group and self.is_participating: if self.competition.processing_class: class_ = load_class(self.competition.processing_class) processing_class = class_(self.competition_id) self.group = processing_class.assign_group(self.distance_id, self.gender, self.birthday)