Ejemplo n.º 1
0
    def default(self,id):
        """ return page for media w/ given id """
        try:
            media = m.Media.get(id)
            if not media:
                raise e.ValidationException('Media not found')

            next = None
            prev = None

            album_ids = cherrypy.session.get('current_albums',None)

            # grab the next and previous media if browing an album
            if album_ids:
                next = m.Media.query.join('albums') \
                                    .filter(m.Album.id.in_(album_ids)) \
                                    .filter(m.Media.id > media.id) \
                                    .order_by(m.Media.id.asc()).first()

                prev = m.Media.query.join('albums') \
                                     .filter(m.Album.id.in_(album_ids)) \
                                     .filter(m.Media.id < media.id) \
                                     .order_by(m.Media.id.desc()).first()

        except e.ValidationException, ex:
            add_flash('error','%s' % ex)
Ejemplo n.º 2
0
    def update(self,handle=None,email=None,password1=None,
                    password2=None,action=None,user_id=None):
        """ users can only edit themselves """

        # if they don't supply the user id, we are editing current user
        if user_id:
            user = m.User.get(user_id)
        else:
            user = cherrypy.request.user

        try:
            if action:
                # update what we were passed
                if handle and user.handle != handle:
                    if m.User.get_by(handle=handle):
                        raise e.ValidationException('Handle taken')
                    user.handle = handle
                if email:
                    cherrypy.log('email:%s' % email)
                    user.email_address = email
                if password1:
                    if password1 != password2:
                        raise e.ValidationException('Passwords do not match')
                    user.password = password1

                # save our changes
                m.session.commit()

                # take a look
                redirect('/user/%s' % user.id)
        except e.ValidationException, ex:
            add_flash('error','%s' % ex)
Ejemplo n.º 3
0
 def template(f,*args,**kwargs):
     try:
         cherrypy.log('path: %s' % path)
         r = f(*args,**kwargs)
         if r:
             return render(path,**r)
         else:
             return render(path)
     except e.Validation, ex:
         add_flash('error','%s' % ex)
Ejemplo n.º 4
0
    def create(self,content=None,title=None,rating=None,media_id=None,
                    tags=[], action=None):
        """ create a new comment """
        try:
            if action:
                media = m.Media.get(media_id)
                # must have something
                if not content and not title and not rating:
                    add_flash('error',
                              'Must have @ least a comment / title or rating')

                # rating must be between 1 and 5
                elif rating and not rating.isdigit() or 0> int(rating) >5:
                    add_flash('error','invalid rating!')

                # make sure our media is good
                elif media_id and not media:
                    add_flash('error',"Sry, I can't find the media!")

                else:
                    # create our new comment
                    comment = m.Comment(title=title,
                                        rating=rating,
                                        content=content,
                                        media=media,
                                        user=cherrypy.request.user)

                    # add our tags
                    for tag in tags:
                        comment.add_tag_by_name(tag)

                    # add the comment to the db
                    m.session.add(comment)
                    m.session.commit()

                    # let the user know it worked
                    add_flash('info','Comment successfully added!')

                    # send them to the page for the media they
                    # commented on
                    if media:
                        redirect('/media/%s' % media.id)
                    else:
                        redirect('/comment/%s' % comment.id)

        except Exception, ex:
            raise
            # woops, let the user know we had an error
            add_flash('error','%s' % ex)
Ejemplo n.º 5
0
    def default(self,*args,**kwargs):
        # TODO: support multiple albums @ a time
        albums = []

        for arg in args:
            # we'll assume it's an id for now
            found = m.Album.get(arg)
            if found:
                albums.append(found)

        if not albums:
            add_flash('error','album not found!')

        cherrypy.session['current_albums'] = [i.id for i in albums]

        return render('/albums/single.html',album=albums[0])
Ejemplo n.º 6
0
    def create(self,handle=None,email=None,password=None,action=None):
        """ create a new user """
        try:
            if action:
                # user has got to have a password + handle
                if not password or not handle:
                    raise e.ValidationException('Must supply password and handle')

                # create our user
                user = m.User(handle=handle,email=email,password=password)

                # add it to the db and push the user to it's page
                m.session.add(user)
                m.session.commit()
                redirect('/user/%s' % user.id)

        except e.ValidationException, ex:
            add_flash('error','%s' % ex)
Ejemplo n.º 7
0
    def login(self,username=None,password=None,action=None,endpoint=None,
                   new_user=None):
        """ prompts the user to login, creates the user if it doesn't exist """


        # see if they are trying to login / create user
        if action or username or password:

            # if they didn't provide a password than we need to
            # either create a new user or prompt them for pass if one exists
            if not password:
                user = m.User.get_by(handle=username)
                if user:
                    return render('/login_password.html',username=username)

                else:
                    return render('/login_new_user.html',username=username)

            # if we are creating a new user, do so
            if new_user == 'true':
                user = m.User(handle=username)
                user.password = password
                m.session.commit()

            # if they gave us a username and a password then they are
            # trying to login
            if not login_user(username,password):
                # fail !
                add_flash('error','Your login has failed!')

            else:

                # yay, success. push them to their next point
                if endpoint:
                    redirect(endpoint)

                # or home
                else:
                    redirect('/')


        return render('/login_front.html',username=username)
Ejemplo n.º 8
0
    def search(self,*args,**kwargs):
        """ use the args to find media """
        media = []
        try:
            for arg in args:
                # start by checking for it by id
                found = m.Media.get(arg)

                if not found:
                    found = m.Media.query.join('user').filter_by(handle=arg). \
                                          order_by(m.Media.created_at).first()

                if not found:
                    found = m.Media.query.filter(m.Media.title==arg). \
                                          order_by(m.Media.created_at).first()

                if found:
                    media = found

                # whaaa, no media?
                if not media:
                    add_flash('error','media appears to be awol!')
        except ValidationException, ex:
            add_flash('error','%s' % ex)
Ejemplo n.º 9
0
    def delete(self,media_id=[],action=None,confirmed=False):
        """ delete this media, only allowed by media owner """
        try:
            found = []
            if not media_id:
                add_flash('error','no media selected to delete')
            else:
                for id in media_id:
                    media = m.Media.get(id)
                    if not media:
                        raise e.ValidationException('Media not found')
                    found.append(media)
                    if confirmed:
                        m.session.delete(media)
                if confirmed:
                    m.session.commit()
                    add_flash('info','Media successfully deleted')
                    redirect('/media/')

        except Exception, ex:
            # error!
            add_flash('error','%s' % ex)
Ejemplo n.º 10
0
 def default(self,id):
     user = m.User.get(id)
     if not user:
         add_flash('error',"Can't find user")
     return render('/users/single.html',user=user)
Ejemplo n.º 11
0
    def create(self,title=None,file_data=None,comment=None,rating=None,
                    tags=[],album_id=None,album_name=None,action=None,
                    multi=False):
        """ creates new media, metadata and media data submitted """
        # check and see if they are creating new media

        try:
            if action:

                # validate our form info
                file_data = m.Media.validate_form_data(title,file_data,comment,rating,
                                                       tags,album_id,album_name)

                for fd in file_data:
                    # create our new media
                    media = m.Media(title=title)

                    # add our new media to the session
                    m.session.add(media)

                    # who uploaded this?
                    media.user = cherrypy.request.user

                    # set the extension as the type
                    media.type = str(fd.type)

                    # add the filename
                    if fd.filename:
                        ext = fd.filename.rsplit('.',1)[-1]
                        if ext:
                            media.extension = ext

                    # if there is a comment from the author add it
                    if comment:
                        c = m.Comment(media=media,
                                      content=comment,
                                      rating=rating,
                                      user=cherrypy.request.user)
                        m.session.add(c)

                    # save file data to the drive
                    media.set_data(fd.file.read())

                    # add our tags
                    for tag_name in tags:
                        media.add_tag_by_name(tag_name)

                    # the album can either be an id or a
                    # new name
                    if album_id or album_name:
                        if album_id:
                            album = m.Album.get(album_id)
                        else:
                            album = m.Album.get_by(name=album_name)
                            if not album:
                                album = m.Album(name=album_name,
                                                owner=cherrypy.request.user)
                                m.session.add(album)
                        media.albums.append(album)

                    m.session.commit()

                # let our user know it worked
                add_flash('info','New media successfully created!')

                # if it's a multi upload than we don't want to redirect
                if multi:
                    return '1'

                # send them to the new media's page
                if album_name:
                    redirect('/album/%s' % album.id)
                else:
                    redirect('/media/%s' % media.id)

        except e.ValidationException, ex:
            # woops, alert of error
            add_flash('error','%s' % ex)
Ejemplo n.º 12
0
    def update(self,id,action=None,**kwargs):
        """ replace the (meta)data for this media, only allowed by
            media owner """
        try:

            # grab the media
            media = m.Media.get(id)

            # must have media to update media
            if not media:
                raise e.ValidationException('Media not found')

            # media must belong to user
            owner_or_error(media)

            if action:

                # we need to validate our form data
                file_data = m.Media.validate_form_data(ignore_file=True,**kwargs)
                if file_data:
                    file_data = file_data[0]

                # now we update our object

                # who uploaded this?
                media.user = cherrypy.request.user

                # set the extension as the type
                if file_data and file_data.file is not None:

                    # grab the file data
                    data = file_data.file.read()

                    if len(data) >= 0:

                        # they uploaded a new photo save it down
                        media.set_data(data)

                        media.type = str(file_data.type)

                        # add the filename
                        if file_data.filename:
                            ext = file_data.filename.rsplit('.',1)[-1]
                            if ext:
                                media.extension = ext


                # is there a comment for the photo?
                comment = kwargs.get('comment')
                rating = kwargs.get('rating')
                if comment:

                    # see if the author originally left a comment
                    if media.comments and media.comments[0].user == cherrypy.request.user:
                        c = media.comments[0]

                        # if so did something change?
                        if c.content != comment:
                            c.content = comment
                        if c.rating != rating:
                            c.rating = rating

                    else:
                        # add a new comment
                        c = m.Comment(media=media,
                                      content=comment,
                                      rating=rating,
                                      user=cherrypy.request.user)
                        m.session.add(c)


                # add our tags
                tags = kwargs.get('tags',[])
                media.set_tags(tags)

                # the album can either be an id or a
                # new name
                album_id = kwargs.get('album_id')
                album_name = kwargs.get('album_name')
                if album_id or album_name:
                    if album_id:
                        album = m.Album.get(album_id)
                    else:
                        album = m.Album.get_by(name=album_name)
                        if not album:
                            # tell our user
                            add_flash('info','New Album created: %s' % album.name)

                            album = m.Album(name=album_name,
                                            owner=cherrypy.request.user)
                            m.session.add(album)
                    media.albums.append(album)

                # add our media to the db, commit
                m.session.add(media)
                m.session.commit()

                # let our user know it worked
                add_flash('info','Media updated!')

                # send them to the media's page
                redirect('/media/%s' % media.id)

        except e.ValidationException, ex:
            add_flash('error','%s' % ex)