示例#1
0
文件: meta.py 项目: octaflop/synapse
    def __init__(self):
        self.logged_in = False
        self.loginform = LoginForm(request.form)
        if 'username' in session:
            self.username = escape(session['username'])
            self.user = User.objects(username=self.username).first()
            if not self.user == None:
                self.logged_in = True
        else:
            self.user = None
        self.site = Site.objects.first()
        ## this should probably be somewhere else (in the model?)
        self.footy = {}
        copyrightinfo_html = \
u"""
<a rel="license"
href="http://creativecommons.org/licenses/by-sa/2.5/ca/"><img
alt="Creative Commons License" style="border-width:0"
src="http://i.creativecommons.org/l/by-sa/2.5/ca/88x31.png" /></a><br
/>Content is released under a
<a href='http://creativecommons.org/licenses/by-sa/2.5/ca/'>Creative Commons
Attribution-ShareAlike 2.5 Canada License</a>
"""
        self.footy['copyrightinfo'] = copyrightinfo_html
        # May want to set up a model from the flatpages.
        self.footy['links'] = [{\
                'title' : u"github page",
                'url' : u"http://github.org/octaflop/synapse"
                }]
示例#2
0
def index():
    meta = Meta()
    posts = Post.objects()
    # posts = Post.live()
    users = User.objects()
    images = Image.objects()
    return dict(meta=meta, users=users, posts=posts, images=images)
示例#3
0
def profile(username=None, id=None):
    meta = Meta()
    user_is_home = False
    if not (username == None and id == None):
        try:
            userpage = User.objects(username=username).get()
        except:
            try:
                userpage = User.objects(id=id).get()
            except:
                return abort(404)
    else:
        return abort(404)
    if "username" in session:
        if session["username"] == userpage.username:
            user_is_home = True
    return render_template("home.html", meta=meta, userpage=userpage, user_is_home=user_is_home)
示例#4
0
文件: admin.py 项目: octaflop/synapse
def add_image():
    meta = Meta()
    form = ImagePostForm(request.form)
    if form.validate_on_submit():
        if form.image.file:
            filename = secure_filename(form.image.file.filename)
            try:
                os.chdir(os.path.join(UPLOAD_FOLDER, 'orig'))
                slugid = slugidfy()
                filename = "%s_%s%s" % (filename[:-4], slugid, filename[-4:])
                form.image.file.save(filename)
                orig = os.path.join(STATIC_PATH, 'orig', filename)
                path = os.path.join(UPLOAD_FOLDER, 'orig', filename)
                image = Picture.open(path)
                # small images
                size = 100,100
                os.chdir('../small')
                image.thumbnail(size, Picture.ANTIALIAS)
                image.save(filename)
                small = os.path.join(STATIC_PATH, 'small', filename)
                image = Picture.open(path)
                # medium images
                size = 200,200
                os.chdir('../medium')
                image.thumbnail(size, Picture.ANTIALIAS)
                image.save(filename)
                medium = os.path.join(STATIC_PATH, 'medium', filename)
                image = Picture.open(path)
                # large images
                size = 650,650
                os.chdir('../large')
                image.thumbnail(size, Picture.ANTIALIAS)
                image.save(filename)
                large = os.path.join(STATIC_PATH, 'large', filename)
            except:
                return "error in file %s upload" % filename
            image = Image(
                title=form.title.data, \
                author=User.objects(username=form.author.data).get(),\
                description=form.description.data,\
                slug=slugfy(form.title.data), slugid=slugid,\
                filename=filename, created=datetime.datetime.now(),\
                published=datetime.datetime.now(),\
                path=path,\
                orig=orig,\
                small=small,\
                medium=medium,\
                large=large
                )
            try:
                image.save()
                #return jsonify(slugid=image.slugid)
                return image.slugid
            except:
                return "Something went wrong while saving"
    return render_template('add_image.html', meta=meta, form=form)
示例#5
0
文件: admin.py 项目: octaflop/synapse
def login(next=None):
    """Login Function. Upcoming "next" function"""
    meta = Meta()
    form = LoginForm(request.form)
    if form.validate_on_submit():
        username = form.username.data
        password = form.password.data
        given_passhash = hash_it(username, password)
        user = User.objects(username=username, hashedpassword=given_passhash).first()
        if user is not None:
            flash("%s is logged in" % user.username)
            session['username'] = user.username
            return redirect(url_for('frontend.profile', username=user.username))
        else:
            flash("Not found")
            return redirect('login')
    if 'username' in session:
        user = User.objects(username=escape(session['username'])).first()
        if user is not None:
            return redirect(url_for('frontend.profile', username=user.username))
        else:
            abort(500)
    return render_template('login.html', meta=meta, form=form)
示例#6
0
文件: admin.py 项目: octaflop/synapse
def add_text_post():
    meta = Meta()
    form = TextPostForm(request.form)
    if form.validate_on_submit():
        username = escape(session['username'])
        text_post = TextPost(slug=slugfy(form.title.data))
        text_post.is_published = form.is_published.data
        try:
            text_post.author = User.objects(username=username).get()
        except:
            flash("user not found")
            return redirect(url_for('add_text_post', meta=meta, form=form))
        # Media ripping method
        def rip_media(mediastr):
            try:
                assert len(mediastr) % 8 == 0
            except:
                flash("could not save media %s " % escape(form.media.data))
            ret = []
            for i in range(len(mediastr) / 8):
                ret.append(mediastr[i*8:(i+1)*8])
            return ret
        if form.media.data:
            media = rip_media(form.media.data)
            if media:
                for j in range(len(media)):
                    med = Media.objects(slugid=media[j]).get()
                    text_post.media.append(med)
        published_time = escape(form.published_time.data)
        published_date = escape(form.published_date.data)
        published = "%s %s" % (published_date, published_time)
        datetimeformat = "%Y-%m-%d %H:%M"
        published = datetime.datetime.strptime(published, datetimeformat)
        text_post.published = published
        text_post.created = datetime.datetime.now()
        text_post.title = escape(form.title.data)
        text_post.content = escape(form.content.data)
        text_post.html_content = markdown(text_post.content, extensions)
        text_post.slugid = slugidfy()
        try:
            text_post.save()
            flash("%s was successfully saved as slugid %s" % (text_post.title,\
                text_post.slugid))
            return redirect(url_for('frontend.post_by_slugid', slugid=text_post.slugid))
        except:
            flash("Error: Post was not saved")
            return redirect(url_for('add_text_post', meta=meta, form=form))
    return render_template('admin/admin_entry.html', meta=meta, form=form)
示例#7
0
def reset_db():
    try:
        posts = Post.objects()
        sites = Site.objects()
        users = User.objects()
        images = Image.objects()
        deps = Dependency.objects()
        flatpages = FlatPage.objects()

        posts.delete()
        sites.delete()
        users.delete()
        deps.delete()
        flatpages.delete()
        return "all images, posts, sites, and users deleted successfully!"
    except:
        print "There was an error..."