Ejemplo n.º 1
0
    def generate_single_line(self, diary):
        helper = ReHelper()
        try:
            old_id = diary.old_id
            object_id = diary.pk
            diary_title = urllib.quote(helper.r_slash(diary.title.encode('utf-8')))

            record = 'rewrite ^/diary/detail/' + str(old_id) + '$  ' + str(Config.SITE_URL) +'/diary/' + str(object_id) + '/' + diary_title + ' permanent;' + '\n'

            return record
        except Exception as e:
            print str(e)
Ejemplo n.º 2
0
    def generate_single_line(self, diary):
        helper = ReHelper()
        try:
            old_id = diary.old_id
            object_id = diary.pk
            diary_title = urllib.quote(
                helper.r_slash(diary.title.encode('utf-8')))

            record = 'rewrite ^/diary/detail/' + str(old_id) + '$  ' + str(
                Config.SITE_URL) + '/diary/' + str(
                    object_id) + '/' + diary_title + ' permanent;' + '\n'

            return record
        except Exception as e:
            print str(e)
Ejemplo n.º 3
0
def gallery():
    """Gallery Admin Page.

    Used for upload new photos to UpYun.

    Methods:
        GET and POST

    Args:
        GET:
            none

        PSOT(*for ajax only):
            files: [name: 'Filedata']

    Returns:
        GET:
            photos

        POST:
            status: {success: true/false, url: url}
    """
    if request.method == 'POST':
        re_helper = ReHelper()
        data = request.files['Filedata']
        filename = re_helper.r_slash(data.filename.encode('utf-8'))
        helper = UpYunHelper()
        url = helper.up_to_upyun('gallery', data, filename)

        if url:
            photo = Photo(url=url)
            photo.title = filename
            photo.save()

            return json.dumps({'success': 'true', 'url': url})
        else:
            return json.dumps({'success': 'false'})
    else:
        photos = Photo.objects.order_by('-publish_time')

        return render_template('admin/gallery/detail.html', photos=photos)
Ejemplo n.º 4
0
def gallery():
    """Gallery Admin Page.

    Used for upload new photos to UpYun.

    Methods:
        GET and POST

    Args:
        GET:
            none

        PSOT(*for ajax only):
            files: [name: 'Filedata']

    Returns:
        GET:
            photos

        POST:
            status: {success: true/false, url: url}
    """
    if request.method == 'POST':
        re_helper = ReHelper()
        data = request.files['Filedata']
        filename = re_helper.r_slash(data.filename.encode('utf-8'))
        helper = UpYunHelper()
        url = helper.up_to_upyun('gallery', data, filename)

        if url:
            photo = Photo(url=url)
            photo.title = filename
            photo.save()

            return json.dumps({'success': 'true', 'url': url})
        else:
            return json.dumps({'success': 'false'})
    else:
        photos = Photo.objects.order_by('-publish_time')

        return render_template('admin/gallery/detail.html', photos=photos)
Ejemplo n.º 5
0
def diary_add_photo():
    """Admin Diary Add Photo Action.

    *for Ajax only.

    Methods:
        POST

    Args:
        files: [name: 'userfile']

    Returns:
        status: {success: true/false}
    """
    if request.method == 'POST':
        re_helper = ReHelper()
        data = request.files['userfile']
        filename = re_helper.r_slash(data.filename.encode('utf-8'))
        helper = UpYunHelper()
        url = helper.up_to_upyun('diary', data, filename)
        if url:
            return json.dumps({'success': 'true', 'url': url})
        else:
            return json.dumps({'success': 'false'})
Ejemplo n.º 6
0
def diary_add_photo():
    """Admin Diary Add Photo Action.

    *for Ajax only.

    Methods:
        POST

    Args:
        files: [name: 'userfile']

    Returns:
        status: {success: true/false}
    """
    if request.method == 'POST':
        re_helper = ReHelper()
        data = request.files['userfile']
        filename = re_helper.r_slash(data.filename.encode('utf-8'))
        helper = UpYunHelper()
        url = helper.up_to_upyun('diary', data, filename)
        if url:
            return json.dumps({'success': 'true', 'url': url})
        else:
            return json.dumps({'success': 'false'})
Ejemplo n.º 7
0
def cmspage_edit(page_url):
    """CMS page edit or create.

    Action for CMS Page.
    Receives title, content(html), tags and cagetory
    Save title, content(html), pure content(further use), page_url
    also auto save author as current_user.

    Methods:
        GET and POST

    Args:
        POST:
            title: page title
            html: content html
            url: page url
        GET:
            page_url: string

    Returns:
        POST:
            none (for create or save page only)
        GET:
            page object or none

    Save:
        title: string
        html: string
        content: string without html tags
        url: string page_url
        summary: first 80 characters in content with 3 dots in the end
        author: current_user_object
    """
    if request.method == 'POST':
        re_helper = ReHelper()

        title = re_helper.r_slash(request.form["title"])
        html = request.form["content"]
        url = request.form["url"]

        parser = MyHTMLParser()
        parser.feed(html)
        content = parser.html # the pure content without html tags

        author = UserModel.objects.first()

        created = StaticPage.objects(url=url)

        if created:
            page = created[0]
        else:
            page = StaticPage(title=title, url=re_helper.r_slash(url))

        page.content = content
        page.summary = content[0:80] + '...'
        page.html = html
        page.author = author
        page.save()

        return redirect(url_for('admin.cmspage_list'))

    else:
        page = StaticPage.objects(url=page_url).first()

        return render_template('admin/page/edit.html', page=page)
Ejemplo n.º 8
0
def diary_edit(diary_id=None):
    """ Edit diary from admin

    receives title, content(html), tags and cagetory
    save title, content(html), pure content(further use), tags and cagetory
    also auto save author as current_user.

    this method will auto save new Category or Tag if not exist otherwise save
    in existed none with push only diary_object

    Args:
        diary_id: diary_id
        title: string
        html: string
        cagetory: string
        tags: list

    Save:
        title: string
        html: string
        content: string without html tags
        category: string
        tags: list
        summary: first 80 characters in content with 3 dots in the end
        author: current_user_object
    """
    if request.method == 'POST' and 'title' and 'content' in request.form:
        re_helper = ReHelper()

        title = re_helper.r_slash(request.form["title"])
        html = request.form["content"]
        category = re_helper.r_slash(request.form["category"])
        tags = request.form["tags"]

        ''' save simple data for further use'''
        parser = MyHTMLParser()
        parser.feed(html)
        content = parser.html # the pure content without html tags

        splited_tags = tags.split(',')

        author = UserModel.objects.first()

        try:
            diary = Diary.objects(pk=diary_id).first()
        except:
            diary = Diary(title=title)

        old_cat = diary.category
        old_tags = diary.tags

        diary.title = title
        diary.content = content
        diary.category = category
        diary.summary = content[0:80] + '...'
        diary.html = html
        diary.author = author
        diary.tags = splited_tags
        diary.save()

        a, cat = Category.objects.get_or_create(name=category,
                                                defaults={'diaries': [diary]})
        if not cat:
            Category.objects(name=category).update_one(push__diaries=diary)
            if old_cat is not None:
                Category.objects(name=old_cat).update_one(pull__diaries=diary)

        for t in old_tags:
            Tag.objects(name=t).update_one(pull__diaries=diary)

        for i in splited_tags:
            b, tag = Tag.objects.get_or_create(name=i,
                                               defaults={'diaries': [diary]})
            if not tag:
                Tag.objects(name=i).update_one(push__diaries=diary)

        return redirect(url_for("admin.diary_list"))

    else:
        try:
            diary = Diary.objects(pk=diary_id).first()
        except:
            diary = None
        categories = Category.objects.all()

        return render_template('admin/diary/edit.html', diary=diary,
                               categories=categories)
Ejemplo n.º 9
0
def cmspage_edit(page_url):
    """CMS page edit or create.

    Action for CMS Page.
    Receives title, content(html), tags and cagetory
    Save title, content(html), pure content(further use), page_url
    also auto save author as current_user.

    Methods:
        GET and POST

    Args:
        POST:
            title: page title
            html: content html
            url: page url
        GET:
            page_url: string

    Returns:
        POST:
            none (for create or save page only)
        GET:
            page object or none

    Save:
        title: string
        html: string
        content: string without html tags
        url: string page_url
        summary: first 80 characters in content with 3 dots in the end
        author: current_user_object
    """
    if request.method == 'POST':
        re_helper = ReHelper()

        title = re_helper.r_slash(request.form["title"])
        html = request.form["content"]
        url = request.form["url"]

        parser = MyHTMLParser()
        parser.feed(html)
        content = parser.html  # the pure content without html tags

        author = UserModel.objects.first()

        created = StaticPage.objects(url=url)

        if created:
            page = created[0]
        else:
            page = StaticPage(title=title, url=re_helper.r_slash(url))

        page.content = content
        page.summary = content[0:80] + '...'
        page.html = html
        page.author = author
        page.save()

        return redirect(url_for('admin.cmspage_list'))

    else:
        page = StaticPage.objects(url=page_url).first()

        return render_template('admin/page/edit.html', page=page)
Ejemplo n.º 10
0
def diary_edit(diary_id=None):
    """ Edit diary from admin

    receives title, content(html), tags and cagetory
    save title, content(html), pure content(further use), tags and cagetory
    also auto save author as current_user.

    this method will auto save new Category or Tag if not exist otherwise save
    in existed none with push only diary_object

    Args:
        diary_id: diary_id
        title: string
        html: string
        cagetory: string
        tags: list

    Save:
        title: string
        html: string
        content: string without html tags
        category: string
        tags: list
        summary: first 80 characters in content with 3 dots in the end
        author: current_user_object
    """
    if request.method == 'POST' and 'title' and 'content' in request.form:
        re_helper = ReHelper()

        title = re_helper.r_slash(request.form["title"])
        html = request.form["content"]
        category = re_helper.r_slash(request.form["category"])
        tags = request.form["tags"]
        ''' save simple data for further use'''
        parser = MyHTMLParser()
        parser.feed(html)
        content = parser.html  # the pure content without html tags

        splited_tags = tags.split(',')

        author = UserModel.objects.first()

        try:
            diary = Diary.objects(pk=diary_id).first()
        except:
            diary = Diary(title=title)

        old_cat = diary.category
        old_tags = diary.tags

        diary.title = title
        diary.content = content
        diary.category = category
        diary.summary = content[0:80] + '...'
        diary.html = html
        diary.author = author
        diary.tags = splited_tags
        diary.save()

        a, cat = Category.objects.get_or_create(name=category,
                                                defaults={'diaries': [diary]})
        if not cat:
            Category.objects(name=category).update_one(push__diaries=diary)
            if old_cat is not None:
                Category.objects(name=old_cat).update_one(pull__diaries=diary)

        for t in old_tags:
            Tag.objects(name=t).update_one(pull__diaries=diary)

        for i in splited_tags:
            b, tag = Tag.objects.get_or_create(name=i,
                                               defaults={'diaries': [diary]})
            if not tag:
                Tag.objects(name=i).update_one(push__diaries=diary)

        return redirect(url_for("admin.diary_list"))

    else:
        try:
            diary = Diary.objects(pk=diary_id).first()
        except:
            diary = None
        categories = Category.objects.all()

        return render_template('admin/diary/edit.html',
                               diary=diary,
                               categories=categories)