105 lines
2.5 KiB
Python
105 lines
2.5 KiB
Python
import string
|
|
from random import SystemRandom
|
|
|
|
from peewee import *
|
|
from peewee import create_model_tables
|
|
|
|
from datetime import datetime
|
|
|
|
db = SqliteDatabase('test.db', threadlocals=True)
|
|
|
|
|
|
class BaseModel(Model):
|
|
class Meta:
|
|
database = db
|
|
|
|
|
|
class User(BaseModel):
|
|
username = CharField(unique=True)
|
|
password_hash = CharField()
|
|
email = CharField(unique=True)
|
|
|
|
# TODO move this to False and require email verification
|
|
verified = BooleanField(default=True)
|
|
|
|
|
|
class Visibility(BaseModel):
|
|
name = CharField()
|
|
|
|
|
|
class Repository(BaseModel):
|
|
namespace = CharField()
|
|
name = CharField()
|
|
visibility = ForeignKeyField(Visibility)
|
|
description = CharField(null=True)
|
|
|
|
class Meta:
|
|
database = db
|
|
indexes = (
|
|
# create a unique index on namespace and name
|
|
(('namespace', 'name'), True),
|
|
)
|
|
|
|
|
|
class Role(BaseModel):
|
|
name = CharField()
|
|
|
|
|
|
class RepositoryPermission(BaseModel):
|
|
user = ForeignKeyField(User)
|
|
repository = ForeignKeyField(Repository)
|
|
role = ForeignKeyField(Role)
|
|
|
|
|
|
def random_string_generator(length=16):
|
|
def random_string():
|
|
random = SystemRandom()
|
|
return ''.join([random.choice(string.ascii_uppercase + string.digits)
|
|
for x in range(length)])
|
|
return random_string
|
|
|
|
|
|
class AccessToken(BaseModel):
|
|
code = CharField(default=random_string_generator(), unique=True)
|
|
user = ForeignKeyField(User)
|
|
repository = ForeignKeyField(Repository)
|
|
created = DateTimeField(default=datetime.now)
|
|
|
|
|
|
class Image(BaseModel):
|
|
# This class is intentionally denormalized. Even though images are supposed
|
|
# to be globally unique we can't treat them as such for permissions and
|
|
# security reasons. So rather than Repository <-> Image being many to many
|
|
# each image now belongs to exactly one repository.
|
|
image_id = CharField()
|
|
checksum = CharField(null=True)
|
|
created = DateTimeField(null=True)
|
|
comment = CharField(null=True)
|
|
repository = ForeignKeyField(Repository)
|
|
|
|
class Meta:
|
|
database = db
|
|
indexes = (
|
|
# we don't really want duplicates
|
|
(('repository', 'image_id'), True),
|
|
)
|
|
|
|
|
|
class RepositoryTag(BaseModel):
|
|
name = CharField()
|
|
image = ForeignKeyField(Image)
|
|
repository = ForeignKeyField(Repository)
|
|
|
|
|
|
def initialize_db():
|
|
create_model_tables([User, Repository, Image, AccessToken, Role,
|
|
RepositoryPermission, Visibility, RepositoryTag])
|
|
Role.create(name='admin')
|
|
Role.create(name='write')
|
|
Role.create(name='read')
|
|
Visibility.create(name='public')
|
|
Visibility.create(name='private')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
initialize_db()
|