示例#1
0
    def sell_to(self, user):
        if not get_rule('RULE_CAN_BUY_CAR', user, self):
            raise self.NotAllowedException
        old_user = self.owner
        price = get_rule('RULE_GET_STREETCAR_PRICE', user, self)
        profile = user.get_profile()
        if profile.balance < price:
            raise UserProfile.InsufficientFundsException
        self.owner = profile
        self.owner_fares = FareInfo()
        self.save()
        profile.balance -= price

        Event.objects.add_car_bought(self, user, price, self._get_owner_user())
        profile.save()
示例#2
0
文件: car.py 项目: israanjjar/rockt
    def sell_to(self, user):
        if not get_rule('RULE_CAN_BUY_CAR', user, self):
            raise self.NotAllowedException
        old_user = self.owner
        price = get_rule('RULE_GET_STREETCAR_PRICE', user, self)
        profile = user.get_profile()
        if profile.balance < price:
            raise UserProfile.InsufficientFundsException
        self.owner = profile
        self.owner_fares = FareInfo()
        self.save()
        profile.balance -= price

        Event.objects.add_car_bought(self, user, price, self._get_owner_user())
        profile.save()
示例#3
0
文件: car.py 项目: israanjjar/rockt
    def ride(self, user, on, off):
        profile = user.get_profile()
        #You don't have to pay for your own streetcars
        insufficient_funds = False
        fare_paid = get_rule('RULE_FIND_FARE', user, self, on, off)
        if fare_paid > profile.balance:
            fare_paid = 0
            insufficient_funds = True

        for fare_info in (self.owner_fares, self.total_fares):
            fare_info.riders += 1
            fare_info.revenue += fare_paid
        self.save()

        Event.objects.add_car_ride(user, self._get_owner_user(), self,
                                   on, off, fare_paid)

        if (insufficient_funds):
            raise UserProfile.InsufficientFundsException
        profile.balance -= fare_paid
        profile.save()
        if self.owner:
            self.owner.balance += fare_paid
            self.owner.save()

        return fare_paid
示例#4
0
    def ride(self, user, on, off):
        profile = user.get_profile()
        #You don't have to pay for your own streetcars
        insufficient_funds = False
        fare_paid = get_rule('RULE_FIND_FARE', user, self, on, off)
        if fare_paid > profile.balance:
            fare_paid = 0
            insufficient_funds = True

        for fare_info in (self.owner_fares, self.total_fares):
            fare_info.riders += 1
            fare_info.revenue += fare_paid
        self.save()

        Event.objects.add_car_ride(user, self._get_owner_user(), self, on, off,
                                   fare_paid)

        if (insufficient_funds):
            raise UserProfile.InsufficientFundsException
        profile.balance -= fare_paid
        profile.save()
        if self.owner:
            self.owner.balance += fare_paid
            self.owner.save()

        return fare_paid
示例#5
0
文件: web.py 项目: israanjjar/rockt
def sell(request, number):
    try:
        car = Car.objects.get(number=number)
    except Car.DoesNotExist:
        raise Http404
    if not car.owner == request.user.get_profile():
        messages.error(request, 'Not allowed')
        return redirect('map')
    if request.method == 'POST':
        try:
            confirm = request.POST['confirm']
            car.buy_back(request.user)
        except KeyError:
            #Continue to rendering the page as usual
            pass
        except Car.NotAllowedException:
            messages.error(request, 'Not allowed')
            return redirect('map')
        else:
            messages.success(request, 'Sold!')
            return redirect(fleet)
    dic = {
        'number': car.number,
        'price': get_rule('RULE_GET_STREETCAR_PRICE', request.user, car)
    }
    return TemplateResponse(request, 'sell.html', dic)
示例#6
0
文件: car.py 项目: israanjjar/rockt
 def post(self, request):
     stop_number = get_key_or_400(request.POST, 'stop_number')
     stop = get_model_or_404(Stop, number=stop_number)
     try:
         profile = self.user.get_profile()
         if profile.riding:
             car = profile.riding.car
         fare = self.user.get_profile().check_out(stop)
         dic = {'fare': fare}
         if get_rule('RULE_CAN_BUY_CAR', self.user, car):
             dic['purchase'] = {
                 'price': get_rule('RULE_GET_STREETCAR_PRICE',
                                   self.user,
                                   car),
                 'url': reverse('car-sell', args=(car.number,))}
         return dic
     except UserProfile.NotCheckedInException:
         raise ErrorResponse(400, {'detail': 'User is not checked in'})
示例#7
0
 def post(self, request):
     stop_number = get_key_or_400(request.POST, 'stop_number')
     stop = get_model_or_404(Stop, number=stop_number)
     try:
         profile = self.user.get_profile()
         if profile.riding:
             car = profile.riding.car
         fare = self.user.get_profile().check_out(stop)
         dic = {'fare': fare}
         if get_rule('RULE_CAN_BUY_CAR', self.user, car):
             dic['purchase'] = {
                 'price': get_rule('RULE_GET_STREETCAR_PRICE', self.user,
                                   car),
                 'url': reverse('car-sell', args=(car.number, ))
             }
         return dic
     except UserProfile.NotCheckedInException:
         raise ErrorResponse(400, {'detail': 'User is not checked in'})
示例#8
0
文件: car.py 项目: israanjjar/rockt
    def buy_back(self, user):
        profile = user.get_profile()
        if not self.owner == profile:
            raise self.NotAllowedException
        price = get_rule('RULE_GET_STREETCAR_PRICE', self.owner, self)
        self.owner = None
        self.owner_fares = FareInfo()
        self.save()

        profile.balance += price
        Event.objects.add_car_sold(self, user, price)
        profile.save()
示例#9
0
    def buy_back(self, user):
        profile = user.get_profile()
        if not self.owner == profile:
            raise self.NotAllowedException
        price = get_rule('RULE_GET_STREETCAR_PRICE', self.owner, self)
        self.owner = None
        self.owner_fares = FareInfo()
        self.save()

        profile.balance += price
        Event.objects.add_car_sold(self, user, price)
        profile.save()
示例#10
0
文件: web.py 项目: israanjjar/rockt
def sell(request, number):
    try:
        car = Car.objects.get(number=number)
    except Car.DoesNotExist:
        raise Http404
    if not car.owner == request.user.get_profile():
        messages.error(request, 'Not allowed')
        return redirect('map')
    if request.method == 'POST':
        try:
            confirm = request.POST['confirm']
            car.buy_back(request.user)
        except KeyError:
            #Continue to rendering the page as usual
            pass
        except Car.NotAllowedException:
            messages.error(request, 'Not allowed')
            return redirect('map')
        else:
            messages.success(request, 'Sold!')
            return redirect(fleet)
    dic = {'number': car.number,
           'price': get_rule('RULE_GET_STREETCAR_PRICE', request.user, car)}
    return TemplateResponse(request, 'sell.html', dic)