def _forum_profile(user_id):
    current_user = get_current_user()

    if current_user is None:
        return redirect('/')

    profile_user = sess.query(User).filter(User.id==user_id).first()
    images = sess.query(Image).filter(Image.user_id==profile_user.id).limit(32).all()

    return render_template('forum_profile.html', current_user=current_user, profile_user=profile_user, images=images)
Ejemplo n.º 2
0
def _forum_upload():
    current_user = get_current_user()

    if current_user is None:
        return redirect('/')

    form = Upload_image_form(csrf_enabled=False)

    msg = None

    if form.validate_on_submit():
        title = form.title.data
        description = form.description.data
        place = form.place.data

        old_place = sess.query(Place).filter(Place.name==place).first()

        if old_place is None:
            new_place = Place(
                name=place
            )
            sess.add(new_place)
            sess.commit()
        else:
            new_place = old_place

        new_filename = get_random_string(16) + '.' + form.image.data.filename.split('.')[1]

        old_image = sess.query(Image).filter(Image.filename==new_filename).first()

        while old_image is not None:
            new_filename = get_random_string(16) + '.' + form.image.data.filename.split('.')[1]
            old_image = sess.query(Image).filter(Image.filename==new_filename).first()

        image = Image(
            filename=new_filename,
            place_id=new_place.id,
            user_id=current_user.id,
            title=title,
            description=description
        )

        sess.add(image)
        sess.commit()

        form.image.data.save('flaskr/static/upload/image/' + new_filename)

        msg = 'Din bild var uppladdad!'

    return render_template('forum_upload.html', current_user=current_user, form=form, msg=msg)
Ejemplo n.º 3
0
def _login():
    current_user = get_current_user()

    form = Login_form(request.form, csrf_enabled=False)

    msg = None

    if form.validate_on_submit():
        email = form.email.data
        password = form.password.data

        print(email)

        real_user = sess.query(User).filter(User.email==email).first()

        if real_user is None:
            msg = 'Det finns ingen användare med den här email addressen'
        else:
            if password == real_user.password:
                msg = 'Du är nu inloggad'
                session['user_id'] = real_user.id

                return redirect('/')
            else:
                msg = 'Fel lösenord'

    return render_template('login.html', current_user=current_user, form=form, msg=msg)
Ejemplo n.º 4
0
def _register():
    current_user = get_current_user()

    form = Register_form(request.form, csrf_enabled=False)

    msg = None

    if form.validate_on_submit():

        old_user = sess.query(User).filter(User.email==form.email.data).first()

        if old_user is not None:
            msg = 'Det finns redan en användare med den här email addressen!'
        else:
            user = User(
                email=form.email.data,
                first_name=form.first_name.data,
                last_name=form.last_name.data,
                password=form.password.data
            )

            sess.add(user)
            sess.commit()

            msg = 'Välkommen, <a href="/login">logga in</a>'

    return render_template('register.html', current_user=current_user, form=form, msg=msg)
Ejemplo n.º 5
0
def get_current_user():
    current_user = None

    if 'user_id' in session:
        user_id = session['user_id']
        current_user = sess.query(User).filter(User.id==user_id).first()

    return current_user
Ejemplo n.º 6
0
def _forum_places():
    current_user = get_current_user()

    if current_user is None:
        return redirect('/')

    places = sess.query(Place).order_by(Place.created.desc()).all()

    return render_template('forum_places.html', current_user=current_user, places=places)
Ejemplo n.º 7
0
def _forum_images_image(image_id):
    current_user = get_current_user()
    image_owner = None

    form = Comment_form(csrf_enabled=False)

    image = sess.query(Image).filter(Image.id==image_id).first()

    if current_user is None:
        return redirect('/')
    else:
        notifications = sess.query(Notification).filter(
            Notification.user_id==current_user.id,
            Notification.image_id==image.id
        ).delete()
        sess.commit()

    comments = []

    
    if image is not None:
        uploader = sess.query(User).filter(User.id==image.user_id).first()
        place = sess.query(Place).filter(Place.id==image.place_id).first()
        image_owner = sess.query(User).filter(User.id==image.user_id).first()

        if request.method == 'POST':
            if 'submit' in request.form:
                if request.form['submit'] == 'Skicka':
                    if form.validate_on_submit():
                        comment_text = form.text.data
                        new_comment = Comment(text=comment_text, image_id=image.id, user_id=current_user.id)
                       
                        if image_owner.id != current_user.id:
                            notification = Notification(
                                image_id = image.id,
                                user_caused_id = current_user.id,
                                user_id = image_owner.id,
                                type = 'comment'
                            )
                            sess.add(notification)

                        sess.add(new_comment)
                        sess.commit()
                        form.text.data = None
                elif request.form['submit'] == 'Kasta':
                    selected_comment_id = request.form['comment_selected']
                    selected_comment = sess.query(Comment).filter(Comment.id==selected_comment_id).delete()
                    sess.commit()

        comments = sess.query(Comment, User).filter(Comment.image_id==image.id).order_by(Comment.created.desc()).join(User).all()

    return render_template('forum_image.html',
            current_user=current_user,
            image=image,
            place=place,
            comments=comments,
            uploader=uploader,
            form=form
    )
Ejemplo n.º 8
0
def _forum_images_places(place):
    current_user = get_current_user()

    if current_user is None:
        return redirect('/')

    images = sess.query(Image, Place).filter(Place.name==place).join(Place).order_by(Image.created.desc()).all()


    return render_template('forum_images.html', current_user=current_user, images=images)
Ejemplo n.º 9
0
def _action(act):

    if act == 'delete':
        song_id = request.form['id']
        song = sess.query(Song).filter(Song.id==song_id).first()


        os.remove(os.path.dirname(os.path.dirname(__file__)) + '/static/music/{file}'.format(file=song.file))

        sess.delete(song)
        sess.commit()

        return 'ok'
Ejemplo n.º 10
0
def _forum_images():
    current_user = get_current_user()

    if current_user is None:
        return redirect('/')

    images = sess.query(Image, Place)\
            .join(Place)\
            .order_by(Image.created.desc())\
            .limit(156)\
            .all()

    return render_template('forum_images.html', current_user=current_user, images=images)
Ejemplo n.º 11
0
def _action(act):

    if act == 'delete':
        song_id = request.form['id']
        song = sess.query(Song).filter(Song.id == song_id).first()

        os.remove(
            os.path.dirname(os.path.dirname(__file__)) +
            '/static/music/{file}'.format(file=song.file))

        sess.delete(song)
        sess.commit()

        return 'ok'
Ejemplo n.º 12
0
def get_playing_song():
    return sess.query(Song).filter(Song.playing==1).first()
Ejemplo n.º 13
0
def get_all_songs():
    return sess.query(Song).order_by(Song.playing.desc()).all()
Ejemplo n.º 14
0
def _index():
    current_user = get_current_user()

    images = sess.query(Image, Place).join(Place).order_by(Image.created.desc()).limit(32).all()

    return render_template('index.html', current_user=current_user, images=images)