Example #1
0
def uploadtheme():
    file = request.files.get('newtheme')
    if file:
        name, ext = os.path.splitext(file.filename)
        if ext == '.css':
            try:
                save_path = os.path.join(os.getcwd(), 'static', 'css',
                                         file.filename)
                print(save_path)
                save_path = os.path.abspath(save_path)
                if os.path.exists(save_path):
                    os.remove(save_path)
                file.save(save_path)
                theme = None
                try:
                    theme = Themes.objects.get(title=name)
                except:
                    pass

                if not theme:
                    for t in Themes.objects():
                        t.isactive = False
                        t.save()
                    theme = Themes()
                    theme.title = name
                    theme.date = datetime.now()
                    theme.isactive = True
                    theme.save()

                themecount = Themes.objects().count()
                returned_themes = Themes.objects.order_by('-date').skip(
                    (int(1) - 1) * 10).limit(10)
                data = {
                    "themes": returned_themes,
                    "count": themecount,
                    "ceil": math.ceil(themecount / 10),
                    "currentPage": 1
                }
                return template('admin/views/themes.jinja2', data)
            except Exception as e:
                return template('admin/views/themes.jinja2',
                                {'errorMessage': str(e)})
        else:
            return template('admin/views/themes.jinja2',
                            {'errorMessage': 'file extension not allowed!'})
    else:
        return template('admin/views/themes.jinja2',
                        {'errorMessage': "couldn't save file"})
    return template('admin/views/themes.jinja2')