103 lines
2.3 KiB
Python
103 lines
2.3 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)
|
|
|
|
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):
|
|
image_id = CharField(unique=True)
|
|
checksum = CharField(null=True)
|
|
|
|
|
|
class RepositoryTag(BaseModel):
|
|
name = CharField()
|
|
image = ForeignKeyField(Image)
|
|
repository = ForeignKeyField(Repository)
|
|
|
|
|
|
class RepositoryImage(BaseModel):
|
|
repository = ForeignKeyField(Repository)
|
|
image = ForeignKeyField(Image)
|
|
tag = CharField()
|
|
|
|
class Meta:
|
|
database = db
|
|
indexes = (
|
|
# we don't really want duplicates
|
|
(('repository', 'image', 'tag'), True),
|
|
)
|
|
|
|
|
|
def initialize_db():
|
|
create_model_tables([User, Repository, Image, RepositoryImage, 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()
|