コード例 #1
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def revision_page(request=None, pagename=None, nb_revision=None):
    if pagename is None:
        pagename ='Home'
        
    page = get_page(request.site._id, pagename)
    if not page:
        raise NotFound
        
    if nb_revision is None:
        nb_revision = 0
    else:
        try:
            nb_revision = int(nb_revision)
        except ValueError:
            raise NotFound

    revision = page.revision(nb_revision)
    if revision is None:
        return render_response('page/revision_notfound.html', page=page, pages=pages, site=request.site)
        
    # revert page
    if request.method == "POST" and "srevert" in request.form:
        page.content = revision.content
        page.save()
        return redirect(url_for("show_page", pagename=pagename))
        
    # get all pages
    pages = all_pages(request.site._id)
    
    return render_response('page/show.html', page=revision, pages=pages)
コード例 #2
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_login(request):
    error = None
    notify = None
    back = ''
    if request.method == "GET":
        back=request.values.get('back', '')
        notify = request.session.get('notify', '')
        if notify:
            del request.session['notify']
    
    if request.method == "POST":
        error = u'Password is invalid.'
        if validate_password(request.site._id, request.form['password']):
            print "here"
            request.session['%s_authenticated' % request.site.cname] = True
            if 'remember' in request.form:
                request.session['permanent'] = True
            elif 'permanent' in request.session:
                del request.session['permanent']
            back = request.form.get('back', '')
            if back:
                redirect_url = back
            else:
                if local.site_url:
                    redirect_url = local.site_url
                else:
                    redirect_url = '/'
            return redirect(redirect_url)
            

    return render_response('site/login.html', back=back, error=error, notify=notify)
コード例 #3
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_address(request):
    error = None
    if request.is_xhr:
        alias = request.values.get('alias')
        if alias is None:
            return send_json({
                'ok': False,
                'error': u"alias is empty or length < 3"
            })
        elif get_site(alias, True) and request.site.alias != alias or alias in FORBIDDEN_CNAME:
            return send_json({
                'ok': False,
                'error':  u"A site with this name has already been registered in couch.it"
            })
        return send_json({ 'ok': True })
    
    if request.method == "POST":
        alias = request.form.get('alias')
        if not alias or len(alias) <= 3:
            error = u"alias is empty or length < 3"
        elif not re_address.match(alias):
            error = u"Address name is invalid. It should only contain string and _ or -."
        elif get_site(alias, True) and request.site.alias != alias:
            error = u"A site with this name has already been registered in couch.it"
        else:
            site = get_site(request.site.cname)
            site.alias = alias
            site.save()
            request.site = site
            redirect_url = "http://%s.%s" % (site.alias, settings.SERVER_NAME)
            return redirect(redirect_url)
            
    return render_response('site/site_address.html', error=error)
コード例 #4
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_forgot_password(request):
    back=request.values.get('back', '')
    if request.method == 'POST':
        back = request.form.get('back', '')
        
        # create token
        otoken = PasswordToken(site=request.site._id)
        otoken.save()
        
        if request.site.alias:
            site_url = "http://%s.%s" % (request.site.alias, settings.SERVER_NAME)
        else:
            site_url = "http://%s/%s" % (settings.SERVER_NAME, request.site.cname)
        
        # send email
        mail_subject = u"Password to your couchit site"
        mail_content = render_template('site/forgot_password.txt', url=site_url, token=otoken._id)
        send_mail(mail_subject, mail_content, "CouchIt <*****@*****.**>", 
            [request.site.email], fail_silently=True)
            
        request.session['notify'] = u"We've sent out the secret link. Go check your email!"
        redirect_url = url_for('site_login', back=back)
        return redirect(redirect_url)
        
    return render_response('site/forgot_password.html', back=back)
コード例 #5
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def diff_page(request=None, pagename=None):
    if pagename is None:
        pagename ='Home'
    page = get_page(request.site._id, pagename)
    if not page:
        if request.is_xhr:
            return send_json({'ok': False, 'reason': 'not found'})
        raise NotFound
    
    diff = ''
    rev1 = rev2 = page
    revisions = request.values.getlist('r')
    if revisions and len(revisions) >=2:
        diff, rev1, rev2 = get_diff(page, revisions[0], revisions[1])
    
    if request.is_xhr:
         return send_json({
            'ok': True,
            'diff': render_template('page/diff_inc.html', diff=diff, rev1=rev1, rev2=rev2)
         })
     
    all_revisions = [page] + page.revisions()

    # get all pages
    pages = all_pages(request.site._id)

    return render_response('page/diff.html', page=page, pages=pages, diff=diff, rev1=rev1, 
    rev2=rev2, revisions=all_revisions)
コード例 #6
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_claim(request):
    if request.method == "POST":
        site = get_site(request.site.cname)
        site.password = make_hash(request.form['password'])
        site.email = request.form['email']
        site.privacy = request.form['privacy']
        site.claimed = True
        site.save()
        request.site = site
        
        if site.alias:
            site_url = "http://%s.%s" % (site.alias, settings.SERVER_NAME)
        else:
            site_url = "http://%s/%s" % (settings.SERVER_NAME, site.cname)
        
        mail_subject = u"You claimed %s" % site_url
        mail_content = render_template("site/email_claimed.txt", url=site_url)
        send_mail(mail_subject, mail_content, "CouchIt <*****@*****.**>", 
            [site.email], fail_silently=True)
            
        if local.site_url:
            redirect_url = local.site_url
        else:
            redirect_url = '/'
        
        request.session['%s_authenticated' % site.cname] = True;
        return redirect(redirect_url)
        
    return render_response('site/claim.html')
コード例 #7
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def home(request, cname=None, alias=None):
    def randomid():
        return str(uuid.uuid4()).replace('-','')
        
    def validate(request):
        createid = request.session.get('createid')
        spamid = request.session.get('spamid')
        spaminput = request.session.get('spaminput')
        
        if createid is None or spaminput is None or spamid is None:
            return False
            
        if request.form.get(spamid, False) or request.form.get(spaminput, False):
            return False
            
        if not request.form.get(createid, False):
            return False
            
        del request.session['createid']
        del request.session['spamid']
        del request.session['spaminput']
        return True
    
    if request.method == "POST" and validate(request):
        site = Site()
        if 'cname' in request.form:
            site.cname = request.form['cname']
        if 'alias' in request.form:
            site.alias = request.form['alias']
        site.save()
        content = ''
        if 'content' in request.form:
            content = request.form['content']
        page = Page(
            title='Home',
            site=site._id,
            content=content,
            user = UserInfos(ip=request.environ['REMOTE_ADDR'], ua=request.environ['HTTP_USER_AGENT'])
        )
        page.save()
        if site.alias:
            redirect_url = 'http://%s.%s' % (site.alias, settings.SERVER_NAME)
        else:
            redirect_url = '/%s' % site.cname
        return redirect(redirect_url)

    # try to fight bots
    b1 = "c%s" % randomid()
    b2 = "c%s" % randomid()
    spamid = randomid()
    spaminput = randomid()
    createid = randomid()
    
    request.session['createid'] = createid
    request.session['spamid'] = spamid
    request.session['spaminput'] = spaminput

    return render_response('home.html', cname=cname, alias=alias,
                b1=b1, b2=b2, spamid=spamid, spaminput=spaminput, createid=createid)
コード例 #8
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_delete(request):
    if request.method == "POST":
        authkey = '%s_authenticated' % request.site.cname
        if authkey in request.session:
            del request.session[authkey]
        del db[request.site._id]
        redirect_url = "http://%s" % settings.SERVER_NAME
        return redirect(redirect_url)
    return render_response('site/delete.html')
コード例 #9
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_changes(request, feedtype=None):
    pages = all_pages(request.site._id)
    changes = get_changes(request.site._id)

    if feedtype == "atom":
        
        feed = AtomFeed(
                    title="%s: Latest changes" % request.site.title and request.site.title or request.site.cname,
                    subtitle=request.site.subtitle,
                    updated = changes[0].updated,
                    feed_url = request.url
        )
        for rev in changes:
            _url = "%s%s" % (request.host_url, url_for("show_page", pagename=rev.title.replace(' ', '_')))
            feed.add(rev.title, convert_markdown(rev.content), 
                updated=rev.updated,
                url=_url,
                id=_url,
                author=rev.title.replace(' ', '_')
            )
        return feed.get_response()
    elif feedtype == 'json':
        json = {
                'title': "%s: Latest changes" % request.site.title and request.site.title or request.site.cname,
                'subtitle': request.site.subtitle,
                'updated':datetime_tojson(changes[0].updated),
                'feed_url': request.url,
                'pages': []
            }
        for rev in changes:
            url = "%s%s" % (request.host_url, url_for("show_page", pagename=rev.title.replace(' ', '_')))
            json['pages'].append({
                'title': rev.title,
                'content': rev.content,
                'url':  url,
                'updated':datetime_tojson(rev.updated),
                'id':rev.title.replace(' ', '_')
            })
        return send_json(json)
    elif feedtype == 'rdf':
        site_title = request.site.title and request.site.title or request.site.cname
        site_url = request.host_url
        if not local.site_url:
            site_url += local.site_url
        
        sioc = SiocWiki(site_url, site_title, datetime_tojson(request.site.created))
        for rev in changes:
            _url = "%s%s" % (request.host_url, url_for("show_page", pagename=rev.title.replace(' ', '_')))
            sioc.add_page(rev.content, rev.title, _url, datetime_tojson(rev.updated))
        return send_sioc(sioc.to_str())

    return render_response('site/changes.html', changes=changes, pages=pages)
コード例 #10
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def history_page(request=None, pagename=None):
    if pagename is None:
        pagename ='Home'
    page = get_page(request.site._id, pagename)
    
    if not page:
        raise NotFound
    
    revisions = page.revisions()
    
    # get all pages
    pages = all_pages(request.site._id)

    return render_response('page/history.html', page=page, pages=pages, revisions=revisions)
コード例 #11
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def show_page(request=None, pagename=None):
    mimetypes = request.accept_mimetypes
    
    if pagename is None:
        pagename ='home'
        
    #pagename = pagename.replace(" ", "_")

    redirect_from = request.values.get('redirect_from', '')
        
    page = get_page(request.site._id, pagename)
    if not page or page._id is None:
        alias = AliasPage.get_alias(request.site._id, pagename)
        if alias is not None:
            page = Page.get(alias.page)
            return redirect(url_for('show_page', 
            pagename=page.title.replace(' ', '_'),
            redirect_from=pagename))
            
   
    if not page or page._id is None:
        if pagename.lower() in FORBIDDEN_PAGES:
            redirect_url = "%s?error=%s" % (
                url_for('show_page', pagename='home'),
                u"Page name invalid."
            )
            return redirect(redirect_url)
        page = Page(
            site=request.site._id,
            title=pagename.replace("_", " ")
        )
    
    if mimeparse.best_match(['application/rdf+xml', 'text/xml', 'text/html'], 
    request.headers['ACCEPT']) == "application/rdf+xml":
        site_title = request.site.title and request.site.title or request.site.cname
        site_url = request.host_url
        if not local.site_url:
            site_url += local.site_url
        
        sioc = SiocWiki(site_url, site_title, datetime_tojson(request.site.created))
        sioc.add_page(page.content, page.title, request.url, datetime_tojson(page.updated))
        return send_sioc(sioc.to_str())

    # get all pages
    pages = all_pages(request.site._id)
    
    response = render_response('page/show.html', page=page, pages=pages, 
        lexers=LEXERS_CHOICE, redirect_from=redirect_from)
        
    return response
コード例 #12
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_design(request):
    DEFAULT_COLORS = dict(
        background_color = 'E7E7E7',
        text_color = '000000',
        link_color = '14456E',
        border_color = 'D4D4D4',
        page_fill_color = 'FFFFFF',
        page_text_color = '000000',
        page_link_color = '14456E',
        menu_inactive_color = '666666',
        syntax_style = 'default'
    )
    
    if not request.site.theme or request.site.theme is None:
        request.site.theme = DEFAULT_COLORS
        
    if request.method == 'POST':
        site = get_site(request.site.cname)
        style = request.form.get('style', 'default')
        if style == 'default':
            site.default_theme = True
            site.theme = DEFAULT_COLORS
        else:
            site.default_theme = False
            site.theme = Theme(
                background_color = request.form.get('background_color', 'E7E7E7'),
                text_color = request.form.get('text_color', '000000'),
                link_color = request.form.get('link_color', '14456E'),
                border_color = request.form.get('border_color', 'D4D4D4'),
                page_fill_color = request.form.get('page_fill_color', 'FFFFFF'),
                page_text_color = request.form.get('page_text_color', '000000'),
                page_link_color = request.form.get('page_link_color', '14456E'),
                menu_inactive_color = request.form.get('menu_inactive_color', '666666'),
                syntax_style = request.form.get('syntax_style', 'default')
            )
        site.save()
        request.site = site 
     
    pages = all_pages(request.site._id)       
    return render_response('site/design.html', pages=pages)
コード例 #13
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_change_password(request):
    authenticated = request.session.get('%s_authenticated' % request.site.cname, False)
    if authenticated:
        return change_password_authenticated(request)
    
    error = None
    token = request.values.get('t', None)
    invalid_token = False
    if request.method == 'GET':
        if token is None or not validate_token(request.site._id, token):
            error = u"Invalid token. Please verify url in your mail."
            invalid_token = True
    if request.method == 'POST':
        token = request.form.get('token', '')
        password = request.form.get('password')
        if not validate_token(request.site._id, token):
            error = u"Invalid token. Please verify url in your mail."
            invalid_token = True
        else:
            if password:
                site = get_site(request.site.cname)
                site.password = make_hash(request.form['password'])
                site.save()
            
                # delete token
                del db[token]
            
                request.session['%s_authenticated' % request.site.cname] = True
                request.site = site
                if local.site_url:
                    redirect_url = local.site_url
                else:
                    redirect_url = '/'
            
                return redirect(redirect_url)
            else:
                error=u'Password is empty.'
    return render_response('site/change_password.html', token=token, 
                error=error, invalid_token=invalid_token)
コード例 #14
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def change_password_authenticated(request):
    error = None
    if request.method == 'POST':
        site = get_site(request.site.cname)
        p1 = request.form.get('password', '')
        p2 = request.form.get('old_password', '')
        
        if not p1:
            error = u"New password can't be empty"
        elif not p2:
            error = u"Old password can't be empty"
        elif make_hash(p2) != site.password:
            error = u"Old password is invalid."
        else:
            h = make_hash(p1)
            if (h != site.password):
                site.password = h
                site.save()
            request.site = site
            return redirect(url_for('site_settings'))
        
    return render_response('site/change_password_authenticated.html', error=error)
コード例 #15
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_settings(request):
    if request.is_xhr and request.method == "POST":
        data = json.loads(request.data)
        allow_javascript = data.get('allow_javascript', False) and True or False
        site = get_site(request.site.cname)
        site.title = data.get('title', site.title)
        site.subtitle = data.get('subtitle', site.subtitle)
        site.email = data.get('email', site.email)
        site.privacy = data.get('privacy', site.privacy)
        site.akismet_key = data.get('akismet_key', "")
        site.allow_javascript = allow_javascript
        site.save()
        request.site = site
        return send_json({ 'ok': True })
        
    
    site_address = None
    if request.site.alias is not None and request.site.alias:
        site_address = "http://%s.%s" % (request.site.alias, settings.SERVER_NAME)
        
    # get all pages
    pages = all_pages(request.site._id)
    return render_response('site/settings.html', pages=pages, site_address=site_address)
コード例 #16
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def couchit_about(request):
    return render_response('about.html')
コード例 #17
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def site_spam(request):
    pages = all_pages(request.site._id)
    spammed_pages = spam(request.site._id)
    return render_response('site/spam.html', spam=spammed_pages, pages=pages)
コード例 #18
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def sitemap(request):
    pages = all_pages(request.site._id)
    return render_response("site/sitemap.xml", pages=pages)
コード例 #19
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def couchit_help(request):
    return render_response('help.html')
コード例 #20
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def couchit_find(request):
    return render_response('find.html')
コード例 #21
0
ファイル: views.py プロジェクト: haxwithaxe/couchit
def not_found(request):
    return render_response("not_found.html")