Example #1
0
 def post_save(self):
     if not self.approved or not self.new_approval:
         return
     # notification data
     notif_data = {}
     if self.join_type == 'R':
         notif_data['owner'] = self.person.owner
         notif_data['type'] = 'join_accept'
         notif_data['text'] = self.location.name
         notif_data['picture'] = self.location.picture
     elif self.join_type == 'I':
         loc_p = models.get_model(settings.DATA_APP,
                                  'Person').objects.getOwn(
                                      self.location.owner)
         notif_data['owner'] = self.location.owner
         notif_data['type'] = 'invite_accept'
         notif_data['text'] = misc.formatFullname(self.person,
                                                  loc_p.name_order, True)
         notif_data['picture'] = self.person.picture
     if not notif_data['picture']:
         notif_data['picture'] = '/static/images/globe.png'
     # create notification
     notif = models.get_model(settings.DATA_APP,
                              'Notification').objects.create(**notif_data)
     # comet notify
     message = {}
     message['model'] = 'Notification'
     message['operation'] = 'create'
     message['uuid'] = notif.uuid
     message['user'] = notif_data['owner'].username
     message['session'] = None
     comet.Notifier().postMessage(message)
Example #2
0
def __bill_item(membership, item, price, bill_for_member):
    # get account
    try:
        account = Account.objects.get(membership=membership,
                                      currency=price.currency,
                                      deleted=False)
    except ObjectDoesNotExist:
        account = Account.objects.create(owner=membership.location.owner,
                                         membership=membership,
                                         currency=price.currency)
    note_append = ''
    if bill_for_member:
        note_append += " for '%s'" % \
            misc.formatFullname(bill_for_member.person, Person.objects.getOwn(req.user).name_order, True)
    if item.reuseable:
        # BuyedItem save() method will create AccountOperation and update balance
        BuyedItem.objects.create(owner=membership.location.owner,
                                 membership=membership,
                                 item=item,
                                 price=price,
                                 consuming=True,
                                 usage_count=1,
                                 operation_note_append=note_append)
    else:
        # create operation to update balance
        # one shot items are not saved in BuyedItem to save space on memberships because
        # BuyedItem records are carried out with memberships records
        AccountOperation.objects.create(owner=membership.location.owner,
                                        account=account,
                                        type='B',
                                        amount=price.price,
                                        note="Billed '%s'%s" %
                                        (item.name, note_append))
 def post_save(self):
     if not self.approved or not self.new_approval:
         return
     # notification data
     notif_data = {}
     if self.join_type == 'R':
         notif_data['owner'] = self.person.owner
         notif_data['type'] = 'join_accept'
         notif_data['text'] = self.location.name
         notif_data['picture'] = self.location.picture  
     elif self.join_type == 'I':
         loc_p = models.get_model(settings.DATA_APP, 'Person').objects.getOwn(self.location.owner)
         notif_data['owner'] = self.location.owner
         notif_data['type'] = 'invite_accept'
         notif_data['text'] = misc.formatFullname(self.person, loc_p.name_order, True)
         notif_data['picture'] = self.person.picture
     if not notif_data['picture']:
         notif_data['picture'] = '/static/images/globe.png'
     # create notification
     notif = models.get_model(settings.DATA_APP, 'Notification').objects.create(**notif_data)
     # comet notify
     message = {}
     message['model'] = 'Notification'
     message['operation'] = 'create'
     message['uuid'] = notif.uuid
     message['user'] = notif_data['owner'].username
     message['session'] = None
     comet.Notifier().postMessage(message)
Example #4
0
 def save(self, *args, **kwargs):
     notify_location = False
     # receive join request
     if self.owner != self.location.owner:
         self.owner = self.location.owner
         self.join_type = 'R'
         if self.location.member_auto_accept:
             self.approved = True
             notify_location = True
         try:
             self.profile = self.location.membershipprofile_set.get(
                 default=True)
         except:
             pass
     super(LocationMembership, self).save(*args, **kwargs)
     # auto join notification
     if notify_location:
         loc_p = models.get_model(settings.DATA_APP,
                                  'Person').objects.getOwn(
                                      self.location.owner)
         notif_data = {}
         notif_data['owner'] = self.location.owner
         notif_data['type'] = 'auto_join'
         notif_data['text'] = misc.formatFullname(self.person,
                                                  loc_p.name_order, True)
         notif_data['text2'] = self.location.name
         if self.person.picture:
             notif_data['picture'] = self.person.picture
         else:
             notif_data['picture'] = '/static/images/globe.png'
         notif = models.get_model(
             settings.DATA_APP, 'Notification').objects.create(**notif_data)
         message = {}
         message['model'] = 'Notification'
         message['operation'] = 'create'
         message['uuid'] = notif.uuid
         message['user'] = notif_data['owner'].username
         message['session'] = None
         comet.Notifier().postMessage(message)
     # delete
     # mark person deleted if person is not self created
     if self.deleted and not self.person.self_created and self.person.owner == self.location.owner:
         self.person.deleted = True
         self.person.save(force_update=True)
Example #5
0
def __bill_item(membership, item, price, bill_for_member):
    # get account
    try: account = Account.objects.get(membership=membership, currency=price.currency, deleted=False)
    except ObjectDoesNotExist:
        account = Account.objects.create(owner=membership.location.owner, membership=membership, 
                                         currency=price.currency)
    note_append = ''
    if bill_for_member:
        note_append += " for '%s'" % \
            misc.formatFullname(bill_for_member.person, Person.objects.getOwn(req.user).name_order, True)        
    if item.reuseable:
        # BuyedItem save() method will create AccountOperation and update balance
        BuyedItem.objects.create(owner=membership.location.owner, membership=membership, 
                    item=item, price=price, consuming=True, usage_count=1, operation_note_append=note_append)
    else:
        # create operation to update balance
        # one shot items are not saved in BuyedItem to save space on memberships because
        # BuyedItem records are carried out with memberships records
        AccountOperation.objects.create(owner=membership.location.owner, account=account, type='B', 
                                        amount=price.price, note="Billed '%s'%s" % (item.name, note_append))
 def save(self, *args, **kwargs):
     notify_location = False
     # receive join request
     if self.owner != self.location.owner:
         self.owner = self.location.owner
         self.join_type = 'R'
         if self.location.member_auto_accept:
             self.approved = True
             notify_location = True
         try: self.profile = self.location.membershipprofile_set.get(default=True)
         except: pass
     super(LocationMembership, self).save(*args, **kwargs)
     # auto join notification
     if notify_location:
         loc_p = models.get_model(settings.DATA_APP, 'Person').objects.getOwn(self.location.owner)
         notif_data = {}
         notif_data['owner'] = self.location.owner
         notif_data['type'] = 'auto_join'
         notif_data['text'] = misc.formatFullname(self.person, loc_p.name_order, True)
         notif_data['text2'] = self.location.name
         if self.person.picture:
             notif_data['picture'] = self.person.picture
         else:
             notif_data['picture'] = '/static/images/globe.png'
         notif = models.get_model(settings.DATA_APP, 'Notification').objects.create(**notif_data)
         message = {}
         message['model'] = 'Notification'
         message['operation'] = 'create'
         message['uuid'] = notif.uuid
         message['user'] = notif_data['owner'].username
         message['session'] = None
         comet.Notifier().postMessage(message)
     # delete
     # mark person deleted if person is not self created
     if self.deleted and not self.person.self_created and self.person.owner == self.location.owner:
         self.person.deleted = True
         self.person.save(force_update=True)
Example #7
0
def __bill_slot(slot, membership=None, bill_for_member=None):
    ret = {}

    if membership is None:
        membership = LocationMembership.objects.get(
            person=slot.person, location=slot.load.location, deleted=False)

    pp = misc.get_person_profile(membership)
    billing_mode = pp['billing_mode']

    if billing_mode == 'other' and bill_for_member:
        billing_mode = 'post'

    if billing_mode == 'other':
        try:
            payer_membership = LocationMembership.objects.get(
                person=pp['bill_person'],
                location=slot.load.location,
                deleted=False)
        except:
            payer_membership = None
            billing_mode = 'post'
        if payer_membership:
            req_person = Person.objects.getOwn(req.user)
            ret['payer'] = misc.formatFullname(pp['bill_person'],
                                               req_person.name_order, True)
            ret.update(__bill_slot(slot, payer_membership, membership))
            return ret

    # bill extra items if any
    # extra items are always billed even if billing mode is 'none'
    __bill_extra_items(membership, pp, bill_for_member)

    # if item or price is not set, set billing to none
    # only 'pre' and 'post' billing modes are processed
    if not slot.item or not slot.price:
        billing_mode = 'none'

    # at this stage prepaid and postpaid mode are the same
    # because even if limits are reached, user forced the
    # load.
    if billing_mode in ('pre', 'post'):
        ret['payment_type'] = 'prepaid' if billing_mode == 'pre' else 'postpaid'
        # buyed item
        buyed_item = None
        buyed_items_rs = BuyedItem.objects.filter(membership=membership,
                                                  item=slot.item,
                                                  price=slot.price,
                                                  consumed=False,
                                                  deleted=False)
        buyed_items_count = buyed_items_rs.count()
        if buyed_items_count == 1:
            buyed_item = buyed_items_rs[0]
        elif buyed_items_count > 1:
            agg = buyed_items_rs.aggregate(Max('usage_count'))
            buyed_item = buyed_items_rs.filter(
                usage_count=agg['usage_count__max'])[0]
        # found a buyed item
        if buyed_item:
            if slot.item.reuseable:
                total_slots = __get_item_total_slots(slot.item)
                if buyed_item.usage_count + 1 == total_slots:
                    buyed_item.consumed = True
                    buyed_item.consuming = False
                else:
                    buyed_item.consuming = True
                buyed_item.usage_count = F('usage_count') + 1
            else:
                buyed_item.usage_count = 1
                buyed_item.consumed = True
            buyed_item.save(force_update=True)
            # item is already paid
            ret['has_buyed_item'] = True
        else:
            __bill_item(membership, slot.item, slot.price, bill_for_member)

    return ret
Example #8
0
def archive_load(load_uuid, note, del_options={}):

    try:
        load = Load.objects.get_by_natural_key(load_uuid)
    except ObjectDoesNotExist:
        raise Http404

    req_person = Person.objects.getOwn(req.user)
    if req.user != load.owner:
        return HttpResponseForbidden('Access denied')

    load_log = {}
    load_log['owner'] = req_person.uuid
    load_log['location'] = load.location.uuid
    load_log['aircraft'] = load.aircraft.uuid
    load_log['aircraft_reg'] = load.aircraft.registration
    load_log['pilot'] = load.pilot.uuid
    load_log['pilot_name'] = load.pilot.name
    load_log['date'] = load.date
    load_log['number'] = load.number
    load_log['total_slots'] = 0
    load_log['prepaid_slots'] = 0
    load_log['postpaid_slots'] = 0
    load_log['unpaid_slots'] = 0
    load_log['staff_slots'] = 0
    if note: load_log['note'] = note

    slots_log = []
    prices = {}

    for slot in load.slot_set.filter(deleted=False):

        # skip empty slot
        if not slot.person and not slot.phantom and not slot.worker:
            continue

        slot_log = {}
        slot_log['owner'] = req_person.uuid

        if slot.person:
            slot_log['jumper'] = slot.person.uuid
            slot_log['jumper_name'] = misc.formatFullname(
                slot.person, req_person.name_order, True)
        elif slot.phantom:
            slot_log['jumper_name'] = slot.phantom.name
        elif slot.worker:
            slot_log['jumper'] = slot.worker.uuid
            slot_log['jumper_name'] = slot.worker.name
        else:
            slot_log['jumper_name'] = 'N/A'

        if slot.worker_type:
            slot_log['catalog_item'] = slot.worker_type.label
            slot_log['is_worker'] = True
        elif slot.item:
            slot_log['catalog_item'] = slot.item.name
        else:
            slot_log['catalog_item'] = 'N/A'

        slot_log['exit_order'] = slot.exit_order

        slot_log['payment_type'] = 'none'
        if slot.person:
            slot_log.update(__archive_person_slot(slot, del_options))
        elif slot.phantom:
            slot_log['payment_type'] = 'prepaid' if slot.is_paid else 'unpaid'

        # slot price
        if not slot.worker_type:
            if slot_log['payment_type'] == 'none':
                pass
            elif slot.price:
                price = slot.price.price
                # zero price for buyed items, they are already paid
                if slot_log.has_key(
                        'has_buyed_item') and slot_log['has_buyed_item']:
                    price = 0
                    del slot_log['has_buyed_item']
                slot_log['catalog_price'] = ujson.encode(
                    {slot.price.currency.code: str(price)})
                if not prices.has_key(slot.price.currency.code):
                    prices[slot.price.currency.code] = 0
                prices[slot.price.currency.code] += price
            else:
                slot_log['catalog_price'] = 'N/A'

        # counters
        load_log['total_slots'] += 1
        if slot.worker_type:
            load_log['staff_slots'] += 1
        elif slot_log['payment_type'] != 'none':
            load_log['%s_slots' % slot_log['payment_type']] += 1

        # label
        labels = {
            'none': 'None',
            'unpaid': 'Unpaid',
            'prepaid': 'Prepaid',
            'postpaid': 'Postpaid',
        }
        slot_log['payment'] = labels[slot_log['payment_type']]

        slots_log.append(slot_log)

    if not del_options.has_key('delLoad') or not del_options['delLoad']:
        for i in prices:
            prices[i] = str(prices[i])
        load_log['prices'] = ujson.encode(prices)
        load_log_rec = LoadLog.objects.create(**load_log)
        for i in slots_log:
            i['load'] = load_log_rec
            SlotLog.objects.create(**i)

    # delete the load
    load.delete()
Example #9
0
def __bill_slot(slot, membership=None, bill_for_member=None):
    ret = {}
    
    if membership is None:
        membership = LocationMembership.objects.get(person=slot.person, location=slot.load.location, deleted=False)
            
    pp = misc.get_person_profile(membership)
    billing_mode = pp['billing_mode']
    
    if billing_mode == 'other' and bill_for_member:
        billing_mode = 'post'
    
    if billing_mode == 'other':
        try: payer_membership = LocationMembership.objects.get(person=pp['bill_person'], location=slot.load.location, deleted=False)
        except:
            payer_membership = None
            billing_mode = 'post'
        if payer_membership:
            req_person = Person.objects.getOwn(req.user)
            ret['payer'] = misc.formatFullname(pp['bill_person'], req_person.name_order, True)
            ret.update(__bill_slot(slot, payer_membership, membership))
            return ret

    # bill extra items if any
    # extra items are always billed even if billing mode is 'none'
    __bill_extra_items(membership, pp, bill_for_member)
    
    # if item or price is not set, set billing to none
    # only 'pre' and 'post' billing modes are processed
    if not slot.item or not slot.price:
        billing_mode = 'none'

    # at this stage prepaid and postpaid mode are the same
    # because even if limits are reached, user forced the
    # load. 
    if billing_mode in ('pre', 'post'):
        ret['payment_type'] = 'prepaid' if billing_mode == 'pre' else 'postpaid'
        # buyed item
        buyed_item = None
        buyed_items_rs = BuyedItem.objects.filter(membership=membership, item=slot.item, price=slot.price, consumed=False, deleted=False)
        buyed_items_count = buyed_items_rs.count()
        if buyed_items_count == 1:
            buyed_item = buyed_items_rs[0]
        elif buyed_items_count > 1:
            agg = buyed_items_rs.aggregate(Max('usage_count'))
            buyed_item = buyed_items_rs.filter(usage_count=agg['usage_count__max'])[0]
        # found a buyed item
        if buyed_item:
            if slot.item.reuseable:
                total_slots = __get_item_total_slots(slot.item)
                if buyed_item.usage_count+1 == total_slots:
                    buyed_item.consumed = True
                    buyed_item.consuming = False
                else:
                    buyed_item.consuming = True
                buyed_item.usage_count = F('usage_count') + 1
            else:
                buyed_item.usage_count = 1
                buyed_item.consumed = True
            buyed_item.save(force_update=True)
            # item is already paid
            ret['has_buyed_item'] = True
        else:
            __bill_item(membership, slot.item, slot.price, bill_for_member)
    
    return ret
Example #10
0
def archive_load(load_uuid, note, del_options={}):
    
    try: load = Load.objects.get_by_natural_key(load_uuid)
    except ObjectDoesNotExist: raise Http404
    
    req_person = Person.objects.getOwn(req.user)
    if req.user != load.owner:
        return HttpResponseForbidden('Access denied')
        
    load_log = {}
    load_log['owner'] = req_person.uuid
    load_log['location'] = load.location.uuid
    load_log['aircraft'] = load.aircraft.uuid
    load_log['aircraft_reg'] = load.aircraft.registration
    load_log['pilot'] = load.pilot.uuid
    load_log['pilot_name'] = load.pilot.name
    load_log['date'] = load.date
    load_log['number'] = load.number
    load_log['total_slots'] = 0
    load_log['prepaid_slots'] = 0
    load_log['postpaid_slots'] = 0
    load_log['unpaid_slots'] = 0
    load_log['staff_slots'] = 0
    if note: load_log['note'] = note
    
    slots_log = []
    prices = {}
    
    for slot in load.slot_set.filter(deleted=False):
        
        # skip empty slot
        if not slot.person and not slot.phantom and not slot.worker:
            continue
        
        slot_log = {}
        slot_log['owner'] = req_person.uuid
        
        if slot.person:
            slot_log['jumper'] = slot.person.uuid
            slot_log['jumper_name'] = misc.formatFullname(slot.person, req_person.name_order, True)
        elif slot.phantom:
            slot_log['jumper_name'] = slot.phantom.name
        elif slot.worker:
            slot_log['jumper'] = slot.worker.uuid
            slot_log['jumper_name'] = slot.worker.name
        else:
            slot_log['jumper_name'] = 'N/A'
            
        if slot.worker_type:
            slot_log['catalog_item'] = slot.worker_type.label
            slot_log['is_worker'] = True
        elif slot.item:
            slot_log['catalog_item'] = slot.item.name
        else:
            slot_log['catalog_item'] = 'N/A'
            
        slot_log['exit_order'] = slot.exit_order
                
        slot_log['payment_type'] = 'none'
        if slot.person:
            slot_log.update(__archive_person_slot(slot, del_options))
        elif slot.phantom:
            slot_log['payment_type'] = 'prepaid' if slot.is_paid else 'unpaid'
        
        # slot price
        if not slot.worker_type:
            if slot_log['payment_type'] == 'none':
                pass
            elif slot.price:
                price = slot.price.price
                # zero price for buyed items, they are already paid
                if slot_log.has_key('has_buyed_item') and slot_log['has_buyed_item']:
                    price = 0
                    del slot_log['has_buyed_item']
                slot_log['catalog_price'] = ujson.encode({slot.price.currency.code:price})
                if not prices.has_key(slot.price.currency.code):
                    prices[slot.price.currency.code] = 0
                prices[slot.price.currency.code] += price
            else:
                slot_log['catalog_price'] = 'N/A'
        
        # counters
        load_log['total_slots'] += 1
        if slot.worker_type:
            load_log['staff_slots'] += 1
        elif slot_log['payment_type'] != 'none':
            load_log['%s_slots' % slot_log['payment_type']] += 1
            
        # label
        labels = {
            'none': 'None',
            'unpaid': 'Unpaid',
            'prepaid': 'Prepaid',
            'postpaid': 'Postpaid',
        }
        slot_log['payment'] = labels[slot_log['payment_type']]
        
        slots_log.append(slot_log)
            
    if not del_options.has_key('delLoad') or not del_options['delLoad']:
        load_log['prices'] = ujson.encode(prices)
        load_log_rec = LoadLog.objects.create(**load_log)
        for i in slots_log:
            i['load'] = load_log_rec
            SlotLog.objects.create(**i)
        
    # delete the load
    load.delete()