Пример #1
0
 def report(self, quote_id):
     authorize()
     quote = db.query(Quote).filter(Quote.id == quote_id).first()
     if not quote:
         return {'msg': 'Invalid quote ID.',
                 'status': 'error'}
     if request.environ['REQUEST_METHOD'] == 'POST':
         if self._has_made_too_many_reports():
             # TODO: This should return a HTTP 429! But pylons.controllers.util.abort()
             #       doesn't seem to support it :/
             return {'msg': 'You are reporting quotes too fast. Slow down!',
                     'status': 'error'}
         if db.query(ReportedQuotes).filter_by(user_id=c.user.id).\
             filter_by(quote_id=quote.id).first():
             return {'msg': 'You already reported this quote in the past. Ignored.',
                     'status': 'error'}
         if not quote.status == QSTATUS['approved']:
             return {'msg': 'Quote is not approved, therefore cannot be reported.',
                     'status': 'error'}
         if db.query(ReportedQuotes).filter_by(user_id=c.user.id).\
             filter_by(quote_id=quote.id).first():
             return {'msg': 'You already reported this quote in the past. Ignored.',
                     'status': 'error'}
         c.user.reported_quotes.append(quote)
         quote.status = QSTATUS['reported']
         db.commit()
         
         return {'msg': 'Quote reported.',
                 'status': 'success'}
     else:
         abort(405)
Пример #2
0
def create_or_get_tag(tagname):
    tag = db.query(Tag).filter(Tag.tag == tagname).first()
    if not tag:
        tag = Tag()
        tag.tag = tagname
        db.add(tag)
        db.commit()
    return tag
Пример #3
0
def create_or_get_tag(tagname):
    tag = db.query(Tag).filter(Tag.tag == tagname).first()
    if not tag:
        tag = Tag()
        tag.tag = tagname
        db.add(tag)
        db.commit()
    return tag
Пример #4
0
    def reset_password(self):
        c.page = 'pw reset'
        c.key = request.params.get('key')
        c.redirect_url = url(controller='account', action='login')
        if request.environ['REQUEST_METHOD'] == 'GET':
            if not c.key:
                return render('/pw_reset/request.mako')
            else:
                reset_token = self._check_valid_pw_reset_key(c.key)
                if not reset_token:
                    h.add_message('Invalid reset token', 'error')
                    return render('/blank.mako')
                return render('/pw_reset/set.mako')
        elif request.environ['REQUEST_METHOD'] == 'POST':
            if not c.key:
                # create a password request key
                email = request.params['email']
                user = db.query(User).filter(User.email == email).first()
                if not user:
                    h.add_message('Invalid email address provided.', 'error')
                    return render('/pw_reset/request.mako')
                already_requested = db.query(PasswordResets).filter(PasswordResets.user_id == user.id).first()
                if already_requested:
                    if already_requested.created < now() - datetime.timedelta(hours=2):
                        db.delete(already_requested)
                    else:
                        h.add_message('A password reset has already been requested for this user.', 'error')
                        return render('/blank.mako')
                pw_reset_key = PasswordResets()
                pw_reset_key.user_id = user.id
                pw_reset_key.key = self._generate_pw_reset_key()
                db.add(pw_reset_key)
                db.commit()

                send_reset_password_email(user.email, pw_reset_key.key)
                h.add_message('Password reset email sent!', 'success')
                return render('/blank.mako')
            else:
                # reset the user's password to what they've submitted
                reset_token = self._check_valid_pw_reset_key(c.key)
                if not reset_token:
                    h.add_message('Invalid reset token', 'error')
                    return render('/blank.mako')
                password = request.params['password']
                password_confirm = request.params['password_confirm']
                valid_password = validate_password(password, password_confirm)
                if not valid_password['status']:
                    h.add_message(valid_password['msg'], 'error')
                    return render('/pw_reset/set.mako')
                user = db.query(User).filter(User.id == reset_token.user_id).first()
                hashed_pass = h.hash_password(password)
                user.password = hashed_pass
                db.delete(reset_token)
                db.commit()

                h.add_message('Password successfully set. You should now be able to login.', 'success')
                return render('/blank.mako')
Пример #5
0
def create_quote(quote_body, notes, tags):
    newquote = Quote()
    newquote.body = quote_body
    newquote.notes = notes
    newquote.tags = [h.create_or_get_tag(tag) for tag in tags]
    newquote.submitted_by = c.user

    db.add(newquote)
    db.commit()
    return True
Пример #6
0
def create_quote(quote_body, notes, tags):
    newquote = Quote()
    newquote.body = quote_body
    newquote.notes = notes
    newquote.tags = [h.create_or_get_tag(tag) for tag in tags]
    newquote.submitted_by = c.user
    
    db.add(newquote)
    db.commit()
    return True
Пример #7
0
    def vote(self, direction, quote_id):
        authorize()
        quote = db.query(Quote).filter(Quote.id == quote_id).first()
        if request.environ['REQUEST_METHOD'] == 'PUT':
            if not quote:
                return {'msg': 'Invalid quote ID',
                        'status': 'error'}

            already_voted = ''
            for assoc in quote.voters:
                if assoc.user == c.user:
                    already_voted = True
                    # cancel the last vote:
                    if assoc.direction == 'up':
                        quote.rating -= 1
                    elif assoc.direction == 'down':
                        quote.rating += 1
                    db.delete(assoc)
            
            assoc = VoteToUser(direction=direction)
            assoc.user = c.user
            quote.voters.append(assoc)

            if direction == 'up':
                quote.rating += 1
            elif direction == 'down':
                quote.rating -= 1
            else:
                return {'msg': 'Invalid vote direction',
                        'status': 'error'}

            if not already_voted:
                quote.votes += 1
            db.commit()
            return {'status': 'success',
                    'msg': 'Vote cast!'}
        elif request.environ['REQUEST_METHOD'] == 'DELETE':
            for assoc in quote.voters:
                if assoc.user == c.user:
                    db.delete(assoc)
            if direction == 'up':
                quote.rating -= 1
            elif direction == 'down':
                quote.rating += 1
            else:
                return {'msg': 'Invalid vote direction',
                        'status': 'error'}
            
            quote.votes -= 1
            db.commit()
            return {'status': 'success',
                    'msg': 'Vote annulled!!'}

        else:
            abort(405)
Пример #8
0
 def disapprove(self, quote_id):
     if request.environ['REQUEST_METHOD'] == 'POST':
         delete_check = self._is_deleteable_or_disapprovable(quote_id)
         if delete_check['status'] == 'error':
             return delete_check
         else:
             quote = delete_check['quote']
         quote.status = QSTATUS['disapproved']
         msg = 'Quote disapproved.'
         db.commit()
         return {'msg': msg, 'status': 'success'}
     else:
         abort(405)
Пример #9
0
 def approve(self, quote_id):
     authorize()
     if not h.is_admin():
         abort(401)
     if request.environ['REQUEST_METHOD'] == 'POST':
         quote = db.query(Quote).filter(Quote.id == quote_id).first()
         if not quote:
             return {'msg': 'Invalid quote ID.', 'status': 'error'}
         quote.status = QSTATUS['approved']
         db.commit()
         return {'msg': 'Quote approved.', 'status': 'success'}
     else:
         abort(405)
Пример #10
0
 def approve(self, quote_id):
     authorize()
     if not h.is_admin():
         abort(401)
     if request.environ['REQUEST_METHOD'] == 'POST':
         quote = db.query(Quote).filter(Quote.id == quote_id).first()
         if not quote:
             return {'msg': 'Invalid quote ID',
                     'status': 'error'}
         quote.approved = 1
         db.commit()
         return {'msg': 'Quote approved',
                 'status': 'success'}
Пример #11
0
 def disapprove(self, quote_id):
     if request.environ['REQUEST_METHOD'] == 'POST':
         delete_check = self._is_deleteable_or_disapprovable(quote_id)
         if delete_check['status'] == 'error':
             return delete_check
         else:
             quote = delete_check['quote']
         quote.status = QSTATUS['disapproved']
         msg = 'Quote disapproved.'
         db.commit()
         return {'msg': msg,
                 'status': 'success'}
     else:
         abort(405)
Пример #12
0
    def vote(self, quote_id, direction):
        authorize()
        quote = db.query(Quote).filter(Quote.id == quote_id).first()
        if request.environ['REQUEST_METHOD'] == 'POST':
            if not quote:
                return {'msg': 'Invalid quote ID.', 'status': 'error'}

            already_voted = ''
            for assoc in quote.voters:
                if assoc.user == c.user:
                    already_voted = True
                    # cancel the last vote:
                    if assoc.direction == 'up':
                        quote.rating -= 1
                    elif assoc.direction == 'down':
                        quote.rating += 1
                    db.delete(assoc)

            assoc = VoteToUser(direction=direction)
            assoc.user = c.user
            quote.voters.append(assoc)

            if direction == 'up':
                quote.rating += 1
            elif direction == 'down':
                quote.rating -= 1
            else:
                return {'msg': 'Invalid vote direction.', 'status': 'error'}

            if not already_voted:
                quote.votes += 1
            db.commit()
            return {'status': 'success', 'msg': 'Vote cast!'}
        elif request.environ['REQUEST_METHOD'] == 'DELETE':
            for assoc in quote.voters:
                if assoc.user == c.user:
                    db.delete(assoc)
            if direction == 'up':
                quote.rating -= 1
            elif direction == 'down':
                quote.rating += 1
            else:
                return {'msg': 'Invalid vote direction.', 'status': 'error'}

            quote.votes -= 1
            db.commit()
            return {'status': 'success', 'msg': 'Vote annulled!'}
        else:
            abort(405)
Пример #13
0
def create_user(username, password, email):
    conflicts = db.query(User).filter(or_(User.email == email,
                                          User.username == username)).first()
    if conflicts:
        if conflicts.email == email:
            raise NameError('Sorry! That email already exists in the system.')
        elif conflicts.username == username:
            raise NameError('Sorry! That username is already taken.')
    
    hashed_pass = h.hash_password(password)
    new_user = User()
    new_user.username = username
    new_user.password = hashed_pass
    new_user.email = email
    
    db.add(new_user)
    db.commit()
    return True
Пример #14
0
def create_user(username, password, email):
    conflicts = db.query(User).filter(
        or_(User.email == email, User.username == username)).first()
    if conflicts:
        if conflicts.email == email:
            raise NameError('Sorry! That email already exists in the system.')
        elif conflicts.username == username:
            raise NameError('Sorry! That username is already taken.')

    hashed_pass = h.hash_password(password)
    new_user = User()
    new_user.username = username
    new_user.password = hashed_pass
    new_user.email = email

    db.add(new_user)
    db.commit()
    return True
Пример #15
0
 def favourite(self, quote_id):
     authorize()
     quote = db.query(Quote).filter(Quote.id == quote_id).first()
     if not quote:
         return {'msg': 'Invalid quote ID',
                 'status': 'error'}
     if request.environ['REQUEST_METHOD'] == 'PUT':
         c.user.favourites.append(quote)
         db.commit()
         return {'msg': 'Quote favourited',
                 'status': 'success'}
     elif request.environ['REQUEST_METHOD'] == 'DELETE':
         if not quote in c.user.favourites:
             return {'msg': "Can't remove: This quote isn't in your favourites",
                     'status': 'error'}
         c.user.favourites.remove(quote)
         db.commit()
         return {'msg': 'Removed favourite',
                 'status': 'success'}
Пример #16
0
 def favourite(self, quote_id):
     authorize()
     quote = db.query(Quote).filter(Quote.id == quote_id).first()
     if not quote:
         return {'msg': 'Invalid quote ID.', 'status': 'error'}
     if request.environ['REQUEST_METHOD'] == 'POST':
         c.user.favourites.append(quote)
         db.commit()
         return {'msg': 'Quote favourited.', 'status': 'success'}
     elif request.environ['REQUEST_METHOD'] == 'DELETE':
         if not quote in c.user.favourites:
             return {
                 'msg':
                 "Can't remove: This quote isn't in your favourites.",
                 'status': 'error'
             }
         c.user.favourites.remove(quote)
         db.commit()
         return {'msg': 'Removed favourite.', 'status': 'success'}
     else:
         abort(405)
Пример #17
0
    def report(self, quote_id):
        authorize()
        quote = db.query(Quote).filter(Quote.id == quote_id).first()
        if not quote:
            return {'msg': 'Invalid quote ID.', 'status': 'error'}
        if request.environ['REQUEST_METHOD'] == 'POST':
            if self._has_made_too_many_reports():
                # TODO: This should return a HTTP 429! But pylons.controllers.util.abort()
                #       doesn't seem to support it :/
                return {
                    'msg': 'You are reporting quotes too fast. Slow down!',
                    'status': 'error'
                }
            if db.query(ReportedQuotes).filter_by(user_id=c.user.id).\
                filter_by(quote_id=quote.id).first():
                return {
                    'msg':
                    'You already reported this quote in the past. Ignored.',
                    'status': 'error'
                }
            if not quote.status == QSTATUS['approved']:
                return {
                    'msg':
                    'Quote is not approved, therefore cannot be reported.',
                    'status': 'error'
                }
            if db.query(ReportedQuotes).filter_by(user_id=c.user.id).\
                filter_by(quote_id=quote.id).first():
                return {
                    'msg':
                    'You already reported this quote in the past. Ignored.',
                    'status': 'error'
                }
            c.user.reported_quotes.append(quote)
            quote.status = QSTATUS['reported']
            db.commit()

            return {'msg': 'Quote reported.', 'status': 'success'}
        else:
            abort(405)
Пример #18
0
    def reset_password(self):
        c.page = 'pw reset'
        c.key = request.params.get('key')
        c.redirect_url = url(controller='account', action='login')
        if request.environ['REQUEST_METHOD'] == 'GET':
            if not c.key:
                return render('/pw_reset/request.mako')
            else:
                reset_token = self._check_valid_pw_reset_key(c.key)
                if not reset_token:
                    h.add_message('Invalid reset token', 'error')
                    return render('/blank.mako')
                return render('/pw_reset/set.mako')
        elif request.environ['REQUEST_METHOD'] == 'POST':
            if not c.key:
                # create a password request key
                email = request.params['email']
                user = db.query(User).filter(User.email == email).first()
                if not user:
                    h.add_message('Invalid email address provided.', 'error')
                    return render('/pw_reset/request.mako')
                already_requested = db.query(PasswordResets).filter(
                    PasswordResets.user_id == user.id).first()
                if already_requested:
                    if already_requested.created < now() - datetime.timedelta(
                            hours=2):
                        db.delete(already_requested)
                    else:
                        h.add_message(
                            'A password reset has already been requested for this user.',
                            'error')
                        return render('/blank.mako')
                pw_reset_key = PasswordResets()
                pw_reset_key.user_id = user.id
                pw_reset_key.key = self._generate_pw_reset_key()
                db.add(pw_reset_key)
                db.commit()

                send_reset_password_email(user.email, pw_reset_key.key)
                h.add_message('Password reset email sent!', 'success')
                return render('/blank.mako')
            else:
                # reset the user's password to what they've submitted
                reset_token = self._check_valid_pw_reset_key(c.key)
                if not reset_token:
                    h.add_message('Invalid reset token', 'error')
                    return render('/blank.mako')
                password = request.params['password']
                password_confirm = request.params['password_confirm']
                valid_password = validate_password(password, password_confirm)
                if not valid_password['status']:
                    h.add_message(valid_password['msg'], 'error')
                    return render('/pw_reset/set.mako')
                user = db.query(User).filter(
                    User.id == reset_token.user_id).first()
                hashed_pass = h.hash_password(password)
                user.password = hashed_pass
                db.delete(reset_token)
                db.commit()

                h.add_message(
                    'Password successfully set. You should now be able to login.',
                    'success')
                return render('/blank.mako')