示例#1
0
def get_or_create_profile(request):
    if request.profile:
        return request.profile
    p = Profile()
    p.save()
    request.profile = p
    request.session['kasse_profile_id'] = p.pk
    return p
示例#2
0
文件: views.py 项目: TK-IT/kasse
    def get(self, request, *args, **kwargs):
        data = []
        qs = TimeTrial.raw_objects.all()
        leg_qs = Leg.raw_objects.all()
        profile_qs = Profile.objects.all()
        if self.kwargs.get('live'):
            now = timezone.now()
            threshold = now - datetime.timedelta(hours=1)
            qs = qs.filter(result='', start_time__gt=threshold)
            leg_qs = leg_qs.filter(timetrial__in=qs)

        profile_qs = profile_qs.order_by()
        profile_qs = profile_qs.values_list(
            'id', 'title__period', 'title__title', 'name', 'association_id', 'association__current_period', 'association__name')
        names = {}
        associations = {}
        for profile_id, title_period, title, name, a_id, a_period, a_name in profile_qs:
            dummy = Profile()
            dummy.pk = profile_id
            dummy_a = Association()
            dummy_a.pk = a_id
            dummy_a.name = a_name
            dummy_a.current_period = a_period
            dummy_t = Title(association=dummy_a, period=title_period, title=title)
            dummy.title = dummy_t if title else None
            dummy.name = name
            names[profile_id] = str(dummy)
            associations[profile_id] = a_id

        leg_qs = leg_qs.order_by()
        leg_qs = leg_qs.values_list('timetrial_id', 'duration', 'order')
        legs = {}
        for tt_id, duration, order in leg_qs:
            legs.setdefault(tt_id, []).append((order, duration))

        qs = qs.values_list(
            'id', 'start_time', 'profile_id',
            'result', 'comment', 'residue',
        )

        for tt_id, start_time, profile_id, result, comment, residue in qs:
            durations = [d for o, d in sorted(legs.get(tt_id, ()))]
            tt_data = {
                'id': tt_id,
                'start_time': str(start_time),
                'profile_id': profile_id,
                'profile': names.get(profile_id),
                'association': associations.get(profile_id),
                'result': result,
                'comment': comment,
                'residue': residue,
                'durations': durations,
            }
            data.append(tt_data)
        response = JsonResponse(data, safe=False, encoder=IndentJSONEncoder)
        response['Access-Control-Allow-Origin'] = '*'
        return response
示例#3
0
文件: views.py 项目: TK-IT/kasse
 def form_valid(self, user_form, profile_form):
     if 'pk' in self.kwargs:
         profile = self.get_profile()
     else:
         profile = Profile()
     profile_form.save(profile)
     profile.save()
     user = user_form.save()
     user.username = '******' % profile.pk
     user.save()
     profile.user = user
     profile.save()
     logger.info("Create user %s from profile %s",
                 user.username,
                 profile,
                 extra=self.request.log_data)
     return HttpResponseRedirect(reverse('home'))
示例#4
0
文件: views.py 项目: TK-IT/kasse
    def form_valid(self, form):
        p = Profile()
        form.save(p)
        p.save()
        logger.info("Profile %s created by %s",
                    p,
                    self.request.get_or_create_profile(),
                    extra=self.request.log_data)

        r = self.request.GET.get('r')
        if r == 'ttc':
            d = 'timetrial_create'
            qs = '?profile=%d' % p.pk
        elif r == 'sw':
            d = 'timetrial_stopwatch_create'
            qs = '?profile=%d' % p.pk
        else:
            d = 'home'
            qs = ''
        success_url = reverse(d) + qs
        return HttpResponseRedirect(success_url)