示例#1
0
文件: cli.py 项目: mredle/expenseapp
    def icons(overwrite, subfolder):
        """Initialize icons."""

        # Update icons
        icon_path = os.path.join(app.config['IMAGE_ROOT_PATH'], 'resources',
                                 subfolder)
        files = [f for f in os.listdir(icon_path) if f.endswith('.svg')]
        for file in files:
            url = os.path.join(icon_path, file)
            name = os.path.splitext(file)[0]
            existing_image = Image.query.filter_by(name=name).first()
            if existing_image:
                if overwrite:
                    try:
                        existing_image.update(url,
                                              keep_original=True,
                                              name=name)
                        existing_image.description = 'Static image'
                        if not existing_image.vector:
                            create_thumbnails(existing_image)
                        db.session.commit()
                    except:
                        print('Updating icon {} failed'.format(file))
                        db.session.rollback()
            else:
                try:
                    image = Image(url, keep_original=True, name=name)
                    image.description = 'Static image'
                    if not image.vector:
                        create_thumbnails(image)
                    db.session.add(image)
                    db.session.commit()
                except:
                    print('Adding icon {} failed'.format(file))
                    db.session.rollback()
示例#2
0
def import_image(guid, path, add_to_class, add_to_id):
    try:
        # Saving the image to a new file
        user = User.get_by_guid_or_404(guid)
        image = Image(path)
        image.description = 'Image uploaded by {}'.format(user.username)
        
        db.session.add(image)
        if add_to_class == 'User':
            add_to_user = User.query.get(add_to_id)
            add_to_user.profile_picture = image  
        elif add_to_class == 'Event':
            add_to_event = Event.query.get(add_to_id)
            add_to_event.image = image
        elif add_to_class == 'EventUser':
            add_to_eventuser = EventUser.query.get(add_to_id)
            add_to_eventuser.profile_picture = image
        elif add_to_class == 'Expense':
            add_to_expense = Expense.query.get(add_to_id)
            add_to_expense.image = image
        elif add_to_class == 'Settlement':
            add_to_settlement = Settlement.query.get(add_to_id)
            add_to_settlement.image = image 
        db.session.commit()
        
        # Create thumbnails
        create_thumbnails(image, update_progress=True)
        
    except:
        _set_task_progress(100)
        app.logger.error('Unhandled exception', exc_info=sys.exc_info())
示例#3
0
文件: cli.py 项目: mredle/expenseapp
    def currency_flags(overwrite):
        """Initialize currency flags."""

        # Update flags
        flag_path = os.path.join(app.config['IMAGE_ROOT_PATH'], 'resources',
                                 'flags')
        existing_currencies = Currency.query.all()
        for currency in existing_currencies:
            country_code = currency.code[0:2].upper()
            url = os.path.join(flag_path, country_code + '.svg')
            if currency.image:
                if overwrite:
                    try:
                        currency.image.update(url,
                                              keep_original=True,
                                              name=country_code)
                        currency.image.description = 'Static image'
                        if not currency.image.vector:
                            create_thumbnails(currency.image)
                        db.session.commit()
                    except:
                        print('Adding flag for {} failed'.format(country_code))
                        db.session.rollback()
            else:
                try:
                    image = Image(url, keep_original=True, name=country_code)
                    image.description = 'Static image'
                    if not image.vector:
                        create_thumbnails(image)
                    currency.image = image
                    db.session.commit()
                except:
                    print('Adding flag for {} failed'.format(country_code))
                    db.session.rollback()