Пример #1
0
    def save(self):
        values = dict(request.params)
        photos_inform=[]
        for item in values:
            if item.startswith('product_id'):
                product_id = item.split('.')[-1]
            if item.startswith('oldphoto'):
                photos_inform.append(int(item.split('-')[-1]))                    
        product = Session.query(Product).filter_by(id=product_id).one()
        
        action = request.params.getone('action')        
        del values['action']      

        if action.startswith('Delete Photo'):                        
            photos = []
            photo_id = int(action.split(' ')[-1])
            for photo in product.photos:
                if photo.id != photo_id and photo.id in photos_inform:
                    photos.append(photo)
            #delte photo_id from disk and data base too
            for photo in product.photos:
                if photo.id == photo_id:
                    path = os.path.join(config['here'],'kazhal','public','pics',photo.file_path)
                    os.remove(path)
                    Session.delete(photo)
                    Session.commit()
            #end of delete procedure

            product.photos = photos
            Session.add(product)
            Session.commit()

            new_values = remove_item(values,action,'oldphoto')              
            return render_edit_form(self.menu_items,
                                    new_values,
                                    id=product_id,
                                    number_of_photos=number_of_photos(new_values),
                                    photos=photos)

        elif action.startswith(_('Remove photo')):
            new_values = remove_item(values,action,'photo')                
            return render_edit_form(
                self.menu_items,
                values=new_values,
                id=product_id,
                number_of_photos = number_of_photos(new_values),
                photos = product.photos
            )

        elif action == _('Add Photo'):
            return render_edit_form(
                self.menu_items,
                values=values,
                id=product_id,
                number_of_photos = number_of_photos(values)+1,
                photos = product.photos
            )
        else: #action is save
            schema = NewProductForm()
            try:
                result = schema.to_python(dict(request.params), c)
            except Invalid, e:
                return render_edit_form(
                    self.menu_items,
                    values=values,
                    id=product.id,
                    errors=variabledecode.variable_encode(
                        e.unpack_errors() or {},
                        add_repetitions=False
                        ),
                    number_of_photos=number_of_photos(values),
                    photos = product.photos
                )
            else:  
Пример #2
0
    def create(self):
        action = request.params.getone('action')
        values = dict(request.params)
        del values['action']
        if action == _('Add Photo'):
            return render_new_form(
                self.menu_items,
                values=values,
                number_of_photos = number_of_photos(values) + 1
            )
        if action.startswith(_('Remove')):
            new_values = remove_item(values,action,'photo')                
            return render_new_form(
                self.menu_items,
                values=new_values,                
                number_of_photos = number_of_photos(new_values)
            )
        if action == _('Save'):
            schema = NewProductForm()
            try:
                result = schema.to_python(dict(request.params), c)
            except Invalid, e:
                return render_new_form(
                    self.menu_items,
                    values=values,
                    errors=variabledecode.variable_encode(
                        e.unpack_errors() or {},
                        add_repetitions=False
                        ),
                    number_of_photos=number_of_photos(values)
                )
            else:
                #save the f*****g photo
                photos =[]
                for picfile in result['photo']:                    
                    permanent_file = open(
                        os.path.join(
                            config['pylons.paths']['static_files'],
                            'pics',
                            picfile.filename.replace(os.sep, '_')
                            ),
                        'wb'
                    )
                    shutil.copyfileobj(picfile.file, permanent_file)
                    picfile.file.close()
                    photos.append(Photo(permanent_file.name.split('/')[-1]))
                    permanent_file.close()
                product = Product(
                    result['name'],
                    result['description'],
                    result['quantity'],
                    result['buy_price'],
                    result['sell_price'],
                    result['buy_date'],
                    result['brand'],
                    photos,
                    result['tag']
                )
                product.code = result['code']
                product.wholesale_price = result['wholesale_price']
                product.deleted = False
                Session.add(product)
                Session.commit()

                h.flash(_('Product added successfully.'))
                redirect(url(controller='product', action='index'))