##// END OF EJS Templates
codingstyle: trivial whitespace fixes...
codingstyle: trivial whitespace fixes Reported by flake8.

File last commit:

r6483:3760df62 default
r6789:76912908 default
Show More
api_key.py
77 lines | 2.6 KiB | text/x-python | PythonLexer
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 # -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.model.api_key
~~~~~~~~~~~~~~~~~~~~~~~
Mads Kiilerich
spelling: more consistent casing of 'API key'
r5124 API key model for Kallithea
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187
Bradley M. Kuhn
RhodeCode GmbH is not the sole author of this work
r4211 This file was forked by the Kallithea project in July 2014.
Original author and date, and relevant copyright and licensing information is below:
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 :created_on: Sep 8, 2013
:author: marcink
Bradley M. Kuhn
RhodeCode GmbH is not the sole author of this work
r4211 :copyright: (c) 2013 RhodeCode GmbH, and others.
Bradley M. Kuhn
Correct licensing information in individual files....
r4208 :license: GPLv3, see LICENSE.md for more details.
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 """
import time
import logging
from kallithea.lib.utils2 import generate_api_key
Søren Løvborg
model: inline BaseModel._get_user calls
r6423 from kallithea.model.db import User, UserApiKeys
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 from kallithea.model.meta import Session
log = logging.getLogger(__name__)
Søren Løvborg
model: remove BaseModel class...
r6483 class ApiKeyModel(object):
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187
def create(self, user, description, lifetime=-1):
"""
:param user: user or user_id
:param description: description of ApiKey
:param lifetime: expiration time in seconds
"""
Søren Løvborg
model: inline BaseModel._get_user calls
r6423 user = User.guess_instance(user)
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187
new_api_key = UserApiKeys()
Mads Kiilerich
utils: make API key generator more random...
r5217 new_api_key.api_key = generate_api_key()
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 new_api_key.user_id = user.user_id
new_api_key.description = description
new_api_key.expires = time.time() + (lifetime * 60) if lifetime != -1 else -1
Session().add(new_api_key)
return new_api_key
def delete(self, api_key, user=None):
"""
Deletes given api_key, if user is set it also filters the object for
deletion by given user.
"""
api_key = UserApiKeys.query().filter(UserApiKeys.api_key == api_key)
Mads Kiilerich
cleanup: check for None object identity in cases where that is what the 'contract' says...
r5306 if user is not None:
Søren Løvborg
model: inline BaseModel._get_user calls
r6423 user = User.guess_instance(user)
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 api_key = api_key.filter(UserApiKeys.user_id == user.user_id)
api_key = api_key.scalar()
Mads Kiilerich
cleanup: avoid some 'except Exception' catching - catch specific exceptions or log it and show what happened...
r4733 Session().delete(api_key)
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187
def get_api_keys(self, user, show_expired=True):
Søren Løvborg
model: inline BaseModel._get_user calls
r6423 user = User.guess_instance(user)
Mads Kiilerich
cleanup: consistent space before line continuation backslash
r5585 user_api_keys = UserApiKeys.query() \
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 .filter(UserApiKeys.user_id == user.user_id)
if not show_expired:
Søren Løvborg
api: simplify API key expiration checks
r6474 user_api_keys = user_api_keys.filter_by(is_expired=False)
Bradley M. Kuhn
Second step in two-part process to rename directories....
r4187 return user_api_keys