Updates per meta.sr.ht API changes
This commit is contained in:
parent
2dd9baeaf8
commit
22f3d2d132
|
@ -0,0 +1,70 @@
|
|||
"""Add user_type to user
|
||||
|
||||
Revision ID: ec0b84a86165
|
||||
Revises: bdf809d6a775
|
||||
Create Date: 2018-12-28 17:38:08.869239
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'ec0b84a86165'
|
||||
down_revision = 'bdf809d6a775'
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
import sqlalchemy_utils as sau
|
||||
from enum import Enum
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker, Session as BaseSession, relationship
|
||||
from srht.config import cfg
|
||||
import requests
|
||||
try:
|
||||
from tqdm import tqdm
|
||||
except ImportError:
|
||||
def tqdm(iterable):
|
||||
yield from iterable
|
||||
|
||||
metasrht = cfg("meta.sr.ht", "origin")
|
||||
|
||||
Session = sessionmaker()
|
||||
Base = declarative_base()
|
||||
|
||||
class UserType(Enum):
|
||||
unconfirmed = "unconfirmed"
|
||||
active_non_paying = "active_non_paying"
|
||||
active_free = "active_free"
|
||||
active_paying = "active_paying"
|
||||
active_delinquent = "active_delinquent"
|
||||
admin = "admin"
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'user'
|
||||
id = sa.Column(sa.Integer, primary_key=True)
|
||||
username = sa.Column(sa.Unicode(256))
|
||||
oauth_token = sa.Column(sa.String(256), nullable=False)
|
||||
user_type = sa.Column(
|
||||
sau.ChoiceType(UserType, impl=sa.String()),
|
||||
nullable=False,
|
||||
default=UserType.unconfirmed)
|
||||
|
||||
def upgrade():
|
||||
op.add_column('user', sa.Column('user_type', sa.Unicode,
|
||||
nullable=False, server_default='active_non_paying'))
|
||||
|
||||
bind = op.get_bind()
|
||||
session = Session(bind=bind)
|
||||
print("Migrating user_type (this expects meta.sr.ht to be available)")
|
||||
for user in tqdm(session.query(User).all()):
|
||||
r = requests.get("{}/api/user/profile".format(metasrht), headers={
|
||||
"Authorization": f"token {user.oauth_token}"
|
||||
})
|
||||
if r.status_code != 200:
|
||||
print(f"Failed for {user.username}", r.status_code, r.json())
|
||||
continue
|
||||
p = r.json()
|
||||
user.user_type = UserType(p["user_type"])
|
||||
session.commit()
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('user', 'user_type')
|
|
@ -4,7 +4,7 @@ from srht.flask import SrhtFlask
|
|||
from todosrht import urls, filters
|
||||
from todosrht.types import EventType
|
||||
from todosrht.types import TicketAccess, TicketStatus, TicketResolution
|
||||
from todosrht.types import User
|
||||
from todosrht.types import User, UserType
|
||||
|
||||
|
||||
class TodoApp(SrhtFlask):
|
||||
|
@ -50,12 +50,13 @@ class TodoApp(SrhtFlask):
|
|||
return User.query.filter(User.username == username).one_or_none()
|
||||
|
||||
def lookup_or_register(self, exchange, profile, scopes):
|
||||
user = User.query.filter(User.username == profile["username"]).first()
|
||||
user = User.query.filter(User.username == profile["name"]).first()
|
||||
if not user:
|
||||
user = User()
|
||||
db.session.add(user)
|
||||
user.username = profile.get("username")
|
||||
user.email = profile.get("email")
|
||||
user.username = profile["name"]
|
||||
user.email = profile["email"]
|
||||
user.user_type = UserType(profile["user_type"])
|
||||
user.oauth_token = exchange["token"]
|
||||
user.oauth_token_expires = exchange["expires"]
|
||||
user.oauth_token_scopes = scopes
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<div class="col-md-4">
|
||||
{% if profile %}
|
||||
<h2>
|
||||
~{{ profile.get("username") }}
|
||||
~{{ profile.get("name") }}
|
||||
</h2>
|
||||
{% else %}
|
||||
<p>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from .user import User
|
||||
from .user import User, UserType
|
||||
from .ticketaccess import TicketAccess
|
||||
from .ticketstatus import TicketStatus, TicketResolution
|
||||
from .tracker import Tracker
|
||||
|
|
|
@ -1,6 +1,15 @@
|
|||
import sqlalchemy as sa
|
||||
import sqlalchemy_utils as sau
|
||||
from srht.database import Base
|
||||
from enum import Enum
|
||||
|
||||
class UserType(Enum):
|
||||
unconfirmed = "unconfirmed"
|
||||
active_non_paying = "active_non_paying"
|
||||
active_free = "active_free"
|
||||
active_paying = "active_paying"
|
||||
active_delinquent = "active_delinquent"
|
||||
admin = "admin"
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'user'
|
||||
|
@ -12,6 +21,10 @@ class User(Base):
|
|||
oauth_token_expires = sa.Column(sa.DateTime, nullable=False)
|
||||
oauth_token_scopes = sa.Column(sa.String, nullable=False, default="")
|
||||
email = sa.Column(sa.String(256), nullable=False)
|
||||
user_type = sa.Column(
|
||||
sau.ChoiceType(UserType, impl=sa.String()),
|
||||
nullable=False,
|
||||
default=UserType.unconfirmed)
|
||||
|
||||
def __repr__(self):
|
||||
return '<User {} {}>'.format(self.id, self.username)
|
||||
|
|
Loading…
Reference in New Issue