95 lines
2.1 KiB
Python
95 lines
2.1 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)
|
|
verified = BooleanField(default=False)
|
|
|
|
|
|
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 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])
|
|
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()
|