Пример #1
0
class VendorController(BaseController):
    def __init__(self, request):
        BaseController.__init__(self, request)
        self.vendor_repo = VendorRepo()
        self.vendor_engagement_repo = VendorEngagementRepo()
        self.vendor_rating_repo = VendorRatingRepo()

    def list_vendors(self):
        location = Auth.get_location()
        vendors = self.vendor_repo.filter_by(is_deleted=False,
                                             is_active=True,
                                             location_id=location)
        vendors_list = [vendor.serialize() for vendor in vendors.items]
        return self.handle_response('OK',
                                    payload={
                                        'vendors': vendors_list,
                                        'meta': self.pagination_meta(vendors)
                                    })

    def list_deleted_vendors(self):
        location = Auth.get_location()
        vendors = self.vendor_repo.filter_by(is_deleted=True,
                                             location_id=location)
        vendors_list = [vendor.serialize() for vendor in vendors.items]
        return self.handle_response('OK',
                                    payload={
                                        'vendors': vendors_list,
                                        'meta': self.pagination_meta(vendors)
                                    })

    def list_suspended_vendors(self):
        location = Auth.get_location()
        vendors = self.vendor_repo.filter_by(is_deleted=False,
                                             is_active=False,
                                             location_id=location)
        vendors_list = [vendor.serialize() for vendor in vendors.items]
        return self.handle_response('OK',
                                    payload={
                                        'vendors': vendors_list,
                                        'meta': self.pagination_meta(vendors)
                                    })

    def get_vendor(self, vendor_id):
        vendor = self.vendor_repo.get(vendor_id)
        if vendor:
            vendor = vendor.serialize()
            return self.handle_response('OK', payload={'vendor': vendor})
        else:
            return self.handle_response(
                'Bad Request - Invalid or Missing vendor_id', status_code=400)

    def create_vendor(self):
        location = Auth.get_location()
        name, tel, address, is_active, contact_person = self.request_params(
            'name', 'tel', 'address', 'isActive', 'contactPerson')

        vendor = self.vendor_repo.new_vendor(name, address, tel, is_active,
                                             contact_person,
                                             location).serialize()
        return self.handle_response('OK',
                                    payload={'vendor': vendor},
                                    status_code=201)

    def update_vendor(self, vendor_id):
        name, tel, address, is_active, contact_person = self.request_params(
            'name', 'tel', 'address', 'isActive', 'contactPerson')
        vendor = self.vendor_repo.get(vendor_id)
        if vendor:
            updates = {}
            if name:
                updates['name'] = name
            if tel:
                updates['tel'] = tel
            if address:
                updates['address'] = address
            if is_active:
                updates['is_active'] = is_active
            if contact_person:
                updates['contact_person'] = contact_person

            self.vendor_repo.update(vendor, **updates)
            return self.handle_response('OK',
                                        payload={'vendor': vendor.serialize()})

        return self.handle_response('Invalid or incorrect vendor_id provided',
                                    status_code=400)

    def suspend_vendor(self, vendor_id):
        vendor = self.vendor_repo.get(vendor_id)
        if vendor:
            updates = {}
            updates['is_active'] = 0
            self.vendor_repo.update(vendor, **updates)
            return self.handle_response('OK',
                                        payload={'vendor': vendor.serialize()})
        return self.handle_response('Invalid or incorrect vendor_id provided',
                                    status_code=400)

    def un_suspend_vendor(self, vendor_id):
        vendor = self.vendor_repo.get(vendor_id)
        if vendor:
            updates = {}
            updates['is_active'] = 1
            self.vendor_repo.update(vendor, **updates)
            return self.handle_response('OK',
                                        payload={'vendor': vendor.serialize()})
        return self.handle_response('Invalid or incorrect vendor_id provided',
                                    status_code=400)

    def delete_vendor(self, vendor_id):
        vendor = self.vendor_repo.get(vendor_id)
        if vendor:
            if vendor.is_deleted:
                return self.handle_response('Vendor has already been deleted',
                                            status_code=400)

            if any(not dependent.is_deleted
                   for dependent in (vendor.engagements or vendor.ratings)):
                return self.handle_response(
                    'Vendor cannot be deleted because it has a child object',
                    status_code=400)
            updates = {}
            updates['is_deleted'] = True

            self.vendor_repo.update(vendor, **updates)

            engagements = self.vendor_engagement_repo.filter_by(
                vendor_id=vendor_id)
            for e in engagements.items:
                updates_engagement = {}
                updates_engagement['is_deleted'] = True

                self.vendor_engagement_repo.update(e, **updates_engagement)
            return self.handle_response('Vendor deleted',
                                        payload={"status": "success"})
        return self.handle_response('Invalid or incorrect vendor_id provided',
                                    status_code=400)
Пример #2
0
class VendorEngagementController(BaseController):
    def __init__(self, request):
        BaseController.__init__(self, request)
        self.vendor_engagement_repo = VendorEngagementRepo()
        self.vendor_repo = VendorRepo()

    def list_vendor_engagements(self):
        location = Auth.get_location()

        engagements = self.vendor_engagement_repo.filter_by_desc(
            self.vendor_engagement_repo._model.start_date,
            is_deleted=False,
            location_id=location)

        engagements_list = []
        for e in engagements.items:
            engagement = e.serialize()
            engagement['vendor'] = e.vendor.serialize()
            engagements_list.append(engagement)

        return self.handle_response('OK',
                                    payload={
                                        'engagements': engagements_list,
                                        'meta':
                                        self.pagination_meta(engagements)
                                    })

    def list_vendor_engagements_by_vendor(self, vendor_id):

        vendor = self.vendor_repo.get(vendor_id)
        if vendor.is_deleted is True:
            return self.handle_response('Invalid Vendor', status_code=400)

        if vendor.is_active is False:
            return self.handle_response('Vendor is disabled', status_code=400)

        engagements = self.vendor_engagement_repo.filter_by(
            vendor_id=vendor_id)

        engagements_list = []
        for e in engagements.items:
            engagement = e.serialize()
            engagement['vendor'] = e.vendor.serialize()
            engagements_list.append(engagement)

        return self.handle_response('OK',
                                    payload={
                                        'engagements': engagements_list,
                                        'meta':
                                        self.pagination_meta(engagements)
                                    })

    def upcoming_vendor_engagements(self):
        location = Auth.get_location()
        engagements = self.vendor_engagement_repo.get_engagement_by_date()

        engagements_list = []
        for e in engagements.items:
            if e.location_id == location:
                engagement = e.serialize()
                engagement['vendor'] = e.vendor.serialize()
                engagements_list.append(engagement)

        return self.handle_response('OK',
                                    payload={
                                        'engagements': engagements_list,
                                        'meta':
                                        self.pagination_meta(engagements)
                                    })

    def get_vendor_engagement(self, engagement_id):
        engagement = self.vendor_engagement_repo.get(engagement_id)
        if engagement:
            e = engagement.serialize()
            e['vendor'] = engagement.vendor.serialize()

            return self.handle_response('OK', payload={'engagement': e})
        else:
            return self.handle_response('Bad Request', status_code=400)

    def create_vendor_engagement(self):
        vendor_id, start_date, end_date, status = self.request_params(
            'vendorId', 'startDate', 'endDate', 'status')
        vendor = self.vendor_repo.get(vendor_id)
        start_date = datetime.strptime(start_date, '%Y-%m-%d').date()
        end_date = datetime.strptime(end_date, '%Y-%m-%d').date()
        existing_engagement = self.vendor_engagement_repo.get_existing_engagement(
            start_date=start_date, end_date=end_date)
        if existing_engagement > 0:
            return self.handle_response(
                'An engagement already exists for this period. Kindly disable engagement first.',
                status_code=400)
        if vendor:
            engagement = self.vendor_engagement_repo.new_vendor_engagement(
                vendor_id, start_date, vendor.location_id, end_date, status)
            e = engagement.serialize()

            e['vendor'] = engagement.vendor.serialize()
            return self.handle_response('OK',
                                        payload={'engagement': e},
                                        status_code=201)

        return self.handle_response('Invalid vendor_id provided',
                                    status_code=400)

    def update_vendor_engagement(self, engagement_id):
        vendor_id, start_date, end_date, status,\
         termination_reason = self.request_params('vendorId', 'startDate', 'endDate', 'status', 'terminationReason')
        engagement = self.vendor_engagement_repo.get(engagement_id)

        if start_date:
            start_date = datetime.strptime(start_date, '%Y-%m-%d')

        if end_date:
            end_date = datetime.strptime(end_date, '%Y-%m-%d')

        if engagement:
            updates = {'vendor_id': vendor_id}
            if start_date:
                updates['start_date'] = start_date
            if end_date:
                updates['end_date'] = end_date
            if status is not None:
                updates['status'] = status
            if termination_reason:
                updates['termination_reason'] = termination_reason

            self.vendor_engagement_repo.update(engagement, **updates)
            e = engagement.serialize()
            e['vendor'] = engagement.vendor.serialize()
            return self.handle_response('OK', payload={'engagement': e})

        return self.handle_response(
            'Invalid or incorrect engagement_id provided', status_code=400)

    def delete_engagement(self, engagement_id):
        engagement = self.vendor_engagement_repo.get(engagement_id)
        if engagement:
            if engagement.is_deleted:
                return self.handle_response(
                    'This engagement has already been deleted',
                    status_code=400)

            if any(not dependent.is_deleted for dependent in engagement.menus):
                return self.handle_response(
                    'This engagement cannot be deleted because it has a child object',
                    status_code=400)
            updates = {}
            updates['is_deleted'] = True

            self.vendor_engagement_repo.update(engagement, **updates)
            return self.handle_response('Engagement deleted',
                                        payload={"status": "success"})
        return self.handle_response(
            'Invalid or incorrect engagement_id provided', status_code=400)

    def immediate_past_engagement(self, location_id):
        past_dates = self.vendor_engagement_repo.get_past_engagement_dates(
            location_id)
        if past_dates:
            latest_date = max(past_dates)

            immediate_past_engagment = VendorEngagement.query.filter_by(
                end_date=latest_date).first()
            e = immediate_past_engagment.serialize()

            engmnt = {
                k: v
                for k, v in e.items()
                if k in ['startDate', 'endDate', 'vendor']
            }
            vendor_id = e['vendorId']
            vendor = self.vendor_repo.get(vendor_id).serialize()
            vendor_info = {
                k: v
                for k, v in vendor.items()
                if k in ['id', 'name', 'tel', 'contactPerson']
            }
            engmnt['vendor'] = vendor_info

            return self.handle_response('OK', payload={'engagement': engmnt})
        else:
            return self.handle_response('No past engagement for this location',
                                        status_code=404)