Exemple #1
0
    def insert_or_update_distribution(self):
        if self.distr_name.get():
            if self.distr_record:
                # update
                did = self.distr_record.did
                kwargs = {
                    'name': self.distr_name.get(),
                    'system_id': self.system.get(),
                }
                try:
                    save_data(DistSet, did, **kwargs)
                except BabelError as e:
                    messagebox.showerror(
                        'Database error',
                        f'Something went wrong:\n{e}')
            else:
                user_id = get_id_from_index(
                    self.profile.get(), self.profile_idx)
                kwargs = {
                    'name': self.distr_name.get(),
                    'system_id': self.system.get(),
                    'user_id': user_id
                }
                try:
                    save_data(DistSet, **kwargs)
                    # update gui
                    self.distr_record = get_record(DistSet, **kwargs)
                except BabelError as e:
                    messagebox.showerror(
                        'Database error',
                        f'Something went wrong:\n{e}')

            # refresh distributions list
            self.update_distributionLst()
            disable_widgets([self.distnameEnt])
Exemple #2
0
 def update_distributionLst(self):
     self.distLst.delete(0, END)
     if self.profile.get() == 'All users':
         values = get_names(
             DistSet, system_id=self.system.get())
     else:
         user_id = get_id_from_index(self.profile.get(), self.profile_idx)
         values = get_names(
             DistSet, system_id=self.system.get(), user_id=user_id)
     for v in sorted(values):
         self.distLst.insert(END, v)
Exemple #3
0
    def show_distribution(self, *args):
        name = self.distLst.get(ACTIVE)
        self.distr_name.set(name)
        self.distr_record = get_record(
            DistSet,
            name=name,
            system_id=self.system.get(),
            user_id=get_id_from_index(
                self.profile.get(), self.profile_idx))

        # mlogger.debug(f'Selected Distr{self.distr_record}')

        self.update_gridLst()
        self.recreate_location_widgets()

        disable_widgets([self.distnameEnt])
def create_cart_copy(cart_id, system, user, profile_idx, cart_name, status):
    """
    Creates a copy of a cart
    args:
        cart_id: int, datastore cart did
        system: str, NYPL or BPL
        user: str, profile/user name
        profile_idx: dict, dictionary of user_id (key) and names
        cart_name: str, new cart name
        status: tkinter StringVar
    """
    valid = True
    if not cart_id:
        valid = False
        status.set('Invalid cart id')
    elif not system:
        valid = False
        status.set('Failed. Missing system parameter.')
    elif not user:
        valid = False
        status.set('Failed. Missing profile prameter.')
    elif not cart_name:
        valid = False
        status.set('Failed. Missing new cart name.')

    try:
        with session_scope() as session:
            if cart_id and system and user and cart_name:
                # verify name/user not used:
                if system == 'BPL':
                    system_id = 1
                elif system == 'NYPL':
                    system_id = 2

                rec = retrieve_record(session,
                                      Cart,
                                      system_id=system_id,
                                      user_id=get_id_from_index(
                                          user, profile_idx),
                                      name=cart_name)
                if rec:
                    valid = False
                    status.set('Failed. A cart with the same name'
                               'already exists.\nPlease change the name.')
            if valid:
                # create copy of the original cart
                old_orders = retrieve_records(session, Order, cart_id=cart_id)

                new_orders = []
                for order in old_orders:

                    resource = Resource(title=order.resource.title,
                                        add_title=order.resource.add_title,
                                        author=order.resource.author,
                                        series=order.resource.series,
                                        publisher=order.resource.publisher,
                                        pub_place=order.resource.pub_place,
                                        summary=order.resource.summary,
                                        isbn=order.resource.isbn,
                                        upc=order.resource.upc,
                                        other_no=order.resource.other_no,
                                        price_list=order.resource.price_list,
                                        price_disc=order.resource.price_disc,
                                        desc_url=order.resource.desc_url,
                                        misc=order.resource.misc)

                    new_orders.append(
                        Order(lang_id=order.lang_id,
                              audn_id=order.audn_id,
                              vendor_id=order.vendor_id,
                              matType_id=order.matType_id,
                              poPerLine=order.poPerLine,
                              note=order.note,
                              comment=order.comment,
                              resource=resource))

                insert(session,
                       Cart,
                       name=cart_name,
                       user_id=get_id_from_index(user, profile_idx),
                       system_id=system_id,
                       orders=new_orders)

                status.set('Cart copied successfully.')

    except Exception as exc:
        _, _, exc_traceback = sys.exc_info()
        tb = format_traceback(exc, exc_traceback)
        mlogger.error('Unhandled error on cart copy.' f'Traceback: {tb}')
        raise BabelError(exc)
Exemple #5
0
def save_new_dist_and_grid(system_id,
                           profile_id,
                           grids,
                           branch_idx,
                           shelf_idx,
                           dist=None,
                           grid=None):
    """
    args:
        system_id: int, did from System table
        profile_id: int, did from User table
        grids: dict, grids element of CartView tracker
        dist: str, name of the new DistSet record
        grid: str, name of the new DistGrid record
    """
    try:
        mlogger.debug('Creating new dist/grid from CartView. '
                      f'system: {system_id}, profile: {profile_id}, '
                      f'dist: {dist}, grid: {grid}')

        if profile_id is not None:
            with session_scope() as session:
                dist_rec = insert_or_ignore(session,
                                            DistSet,
                                            system_id=system_id,
                                            user_id=profile_id,
                                            name=dist)
                mlogger.debug(f'Dist_rec: {dist_rec}')

                # check if given grid already exists
                grid_rec = retrieve_record(session,
                                           DistGrid,
                                           name=grid,
                                           distset_id=dist_rec.did)
                mlogger.debug(f'Grid_rec: {grid_rec}')

                # determine new gridLocations
                locations = []
                locs = grids['locs']
                for l in locs:
                    if grid_rec:
                        locations.append(
                            GridLocation(distgrid_id=grid_rec.did,
                                         branch_id=get_id_from_index(
                                             l['branchCbx'].get(), branch_idx),
                                         shelfcode_id=get_id_from_index(
                                             l['shelfCbx'].get(), shelf_idx),
                                         qty=int(l['qtyEnt'].get())))
                    else:
                        locations.append(
                            GridLocation(branch_id=get_id_from_index(
                                l['branchCbx'].get(), branch_idx),
                                         shelfcode_id=get_id_from_index(
                                             l['shelfCbx'].get(), shelf_idx),
                                         qty=int(l['qtyEnt'].get())))
                mlogger.debug(f'New locations: {locations}')

                if grid_rec:
                    mlogger.debug('Updating existing grid_rec.')
                    update_record(session,
                                  DistGrid,
                                  grid_rec.did,
                                  name=grid,
                                  distset_id=dist_rec.did,
                                  gridlocations=locations)
                else:
                    mlogger.debug('Inserting new grid_rec.')
                    insert(session,
                           DistGrid,
                           name=grid,
                           distset_id=dist_rec.did,
                           gridlocations=locations)

    except ValueError as e:
        mlogger.error('User attempted to save new grid with incorrect values.'
                      f'Error: {e}')
        raise BabelError('Your new grid includes invalid values.\n'
                         'Please make sure branch, shelf, and qty are valid.')
def save_grid_data(**kwargs):
    """
    used in GridView
    """
    locs = []
    for loc in kwargs['gridlocs']:
        branch_id = get_id_from_index(loc['branch'], kwargs['branch_idx'])
        shelf_id = get_id_from_index(loc['shelf'], kwargs['shelf_idx'])
        if loc['gridloc_id']:
            locs.append(
                GridLocation(
                    did=loc['gridloc_id'],
                    distgrid_id=loc['distgrid_id'],
                    branch_id=branch_id,
                    shelfcode_id=shelf_id,
                    qty=loc['qty']))
        else:
            if loc['distgrid_id']:
                locs.append(
                    GridLocation(
                        distgrid_id=loc['distgrid_id'],
                        branch_id=branch_id,
                        shelfcode_id=shelf_id,
                        qty=loc['qty']
                    ))
            else:
                locs.append(
                    GridLocation(
                        branch_id=branch_id,
                        shelfcode_id=shelf_id,
                        qty=loc['qty']
                    ))

    with session_scope() as session:
        try:
            record = None
            if kwargs['grid_did']:
                update_record(
                    session,
                    DistGrid,
                    kwargs['grid_did'],
                    name=kwargs['name'],
                    distset_id=kwargs['distset_id'],
                    gridlocations=locs)
                record = retrieve_record(
                    session,
                    DistGrid,
                    name=kwargs['name'],
                    distset_id=kwargs['distset_id'])
                mlogger.debug(
                    f'Updated record {record}')
            else:
                record = insert(
                    session,
                    DistGrid,
                    name=kwargs['name'],
                    distset_id=kwargs['distset_id'],
                    gridlocations=locs)
                mlogger.debug(
                    f'Added new record {record}')
            session.expunge_all()
            return record
        except IntegrityError as e:
            raise BabelError(e)
        except InternalError as e:
            raise BabelError(e)