Skip to content

Commit

Permalink
dev: isolate field in decore_fields
Browse files Browse the repository at this point in the history
fix: revert id to charfield with uuiid as string type
  • Loading branch information
KemoPanzah committed Jun 22, 2023
1 parent 5423eab commit 60fd897
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 84 deletions.
99 changes: 89 additions & 10 deletions decore_base/classes/decore_fields.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
import inspect
import logging
from pathlib import Path, PosixPath, WindowsPath
from shutil import move
from uuid import UUID

from peewee import (BooleanField, CharField, Field, ForeignKeyField,
IntegerField, ManyToManyField, MetaField, TextField,
UUIDField)
from pykeepass.entry import Entry

from peewee import CharField, Field, UUIDField
from ..globals import globals

__all__ = [
'BackRefMetaField',
'BooleanField',
'CharField',
'ForeignKeyField',
'IntegerField',
'ManyToManyField',
'PasswordField',
'TextField',
]

class FileFieldAccessor(object):
def __init__(self, model, field, name):
self.model = model
self.field = field
self.name = name

def __get__(self, instance, instance_type=None):
if instance is not None:
t_path = Path(globals.config['default']['state_path']).joinpath('filebase').joinpath(self.model.__name__).joinpath(instance.id).joinpath(self.name).joinpath(instance.__data__.get(self.name))
if t_path.exists():
return t_path.absolute()
else:
return None
return self.field

def __set__(self, instance, value):
if instance.id:
if type(value) == WindowsPath or type(value) == PosixPath:
t_path = Path(globals.config['default']['state_path']).joinpath('filebase').joinpath(self.model.__name__).joinpath(instance.id).joinpath(self.name).joinpath(value.name)
t_path.parent.mkdir(parents=True, exist_ok=True)
move(value, t_path)
instance.__data__[self.name] = value.name
else:
instance.__data__[self.name] = value
else:
logging.error('%s > %s' % (self.name, 'u can not store files while ur item id was not setted'))
instance.__data__[self.name] = None
instance._dirty.add(self.name)

class UUIDFieldAccessor(object):
def __init__(self, model, field, name):
self.model = model
Expand All @@ -24,7 +70,7 @@ def __set__(self, instance, value):
instance.__data__[self.name] = UUID(value) if value is not None else None
instance._dirty.add(self.name)

class DecoreCustomField(Field):
class CustomField(Field):

@property
def instance(self):
Expand All @@ -35,13 +81,23 @@ def instance(self):
return instance
return None

class DecoreCharField(CharField):
class BackRefMetaField(MetaField):
def __init__(self,verbose_name=None, help_text=None, filter_fields=None):
super().__init__(False, False, False, None, None, False, None, None, None, False, None, help_text, verbose_name, None, None, False)
self.filter_fields = filter_fields

def bind(self, model, name, set_attribute):
super().bind(model, name, set_attribute)
setattr(model,'br_'+name, getattr(model, name))
delattr(model, name)

class BooleanField(BooleanField):
pass

class DecoreUUIDField(UUIDField):
accessor_class = UUIDFieldAccessor
class CharField(CharField):
pass

class DecorePasswordField(DecoreCustomField):
class PasswordField(CustomField):
field_type = 'VARCHAR'

@property
Expand All @@ -58,12 +114,12 @@ def db_value(self, value):
# MEMO - Suche nach einem Eintrag der dem Identifiziern entspricht
i_entry: Entry
for i_entry in self.kdb_group.entries:
if i_entry.title == self.name and i_entry.username == self.instance.id.hex:
if i_entry.title == self.name and i_entry.username == self.instance.id:
t_entry = i_entry

# MEMO - Wenn kein Eintrag vorhanden ist, lege einen neuen an und füge diesen der Gruppe hinzu
if not t_entry:
t_entry = Entry(title=self.name, username=self.instance.id.hex, password=value, kp=globals.kdb)
t_entry = Entry(title=self.name, username=self.instance.id, password=value, kp=globals.kdb)
self.kdb_group.append(t_entry)
try:
globals.kdb.save()
Expand All @@ -79,7 +135,7 @@ def db_value(self, value):
raise Exception('Could not save to keybase')

# MEMO - schreibe die UUID des Entries in das __data__ Dict um das Kennwort zu verschleiern
return t_entry.uuid.hex
return str(t_entry.uuid)

def python_value(self, value):
t_entry = None
Expand All @@ -92,4 +148,27 @@ def python_value(self, value):

# MEMO - Wenn kein Eintrag vorhanden ist, gebe None als Passwort aus
if not t_entry:
return None
return None

class ForeignKeyField(ForeignKeyField):
pass

class IntegerField(IntegerField):
pass

class ManyToManyField(ManyToManyField):
def __init__(self, model, backref=None, on_delete=None, on_update=None, _is_backref=False, verbose_name=None, help_text=None, filter_fields=None):
super().__init__(model, backref, on_delete, on_update, _is_backref)
self.verbose_name = verbose_name
self.help_text = help_text
self.filter_fields = filter_fields

class TextField(TextField):
pass

class FileField(Field):
accessor_class = FileFieldAccessor
field_type = 'VARCHAR'

class UUIDField(UUIDField):
accessor_class = UUIDFieldAccessor
67 changes: 9 additions & 58 deletions decore_base/classes/decore_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,39 @@
import logging
import operator
from functools import reduce
from pathlib import Path, PosixPath, WindowsPath
from shutil import move
from uuid import uuid1

#
from cerberus import Validator
from peewee import *
from peewee import FieldAccessor, MetaField
from playhouse.migrate import *
from peewee import DQ, FieldAccessor, Model, SqliteDatabase
from playhouse.migrate import SqliteMigrator, migrate
from playhouse.reflection import Introspector
from playhouse.shortcuts import model_to_dict
from pykeepass.entry import Entry

#
from ..globals import globals
from .decore_fields import *
#
from .decore_translate import Decore_translate as t


class FileFieldAccessor(object):
def __init__(self, model, field, name):
self.model = model
self.field = field
self.name = name

def __get__(self, instance, instance_type=None):
if instance is not None:
t_path = Path(globals.config['default']['state_path']).joinpath('filebase').joinpath(self.model.__name__).joinpath(instance.id).joinpath(self.name).joinpath(instance.__data__.get(self.name))
if t_path.exists():
return t_path.absolute()
else:
return None
return self.field

def __set__(self, instance, value):
if instance.id:
if type(value) == WindowsPath or type(value) == PosixPath:
t_path = Path(globals.config['default']['state_path']).joinpath('filebase').joinpath(self.model.__name__).joinpath(instance.id).joinpath(self.name).joinpath(value.name)
t_path.parent.mkdir(parents=True, exist_ok=True)
move(value, t_path)
instance.__data__[self.name] = value.name
else:
instance.__data__[self.name] = value
else:
logging.error('%s > %s' % (self.name, 'u can not store files while ur item id was not setted'))
instance.__data__[self.name] = None
instance._dirty.add(self.name)

class FileField(Field):
accessor_class = FileFieldAccessor
field_type = 'VARCHAR'

class ManyToManyField(ManyToManyField):
def __init__(self, model, backref=None, on_delete=None, on_update=None, _is_backref=False, verbose_name=None, help_text=None, filter_fields=None):
super().__init__(model, backref, on_delete, on_update, _is_backref)
self.verbose_name = verbose_name
self.help_text = help_text
self.filter_fields = filter_fields

class BackRefMetaField(MetaField):
def __init__(self,verbose_name=None, help_text=None, filter_fields=None):
super().__init__(False, False, False, None, None, False, None, None, None, False, None, help_text, verbose_name, None, None, False)
self.filter_fields = filter_fields

def bind(self, model, name, set_attribute):
super().bind(model, name, set_attribute)
setattr(model,'br_'+name, getattr(model, name))
delattr(model, name)

class Decore_model(Model):
id = DecoreUUIDField(primary_key=True, unique=True, verbose_name="ID")
id = CharField(primary_key=True, unique=True, verbose_name="ID")
title = CharField(verbose_name=t('Title'))
desc = CharField(verbose_name=t('Description'), null=True)
item_type = CharField(verbose_name=t('Item type'), default='object')
parent_path = CharField(verbose_name=t('Parent path'), null=True)

class Meta:
# tbase = SqliteDatabase('state/database.db', pragmas=(('cache_size', -1024 * 64),('journal_mode', 'wal')))
database = None
tbase = SqliteDatabase('state/database.db')
migrator = SqliteMigrator(database)

def __init__(self, *args, **kwargs):
Model.__init__(self, *args, **kwargs)
if not self.id:
self.id = uuid1()
self.id = str(uuid1())

@classmethod
def register(cls):
Expand Down
1 change: 1 addition & 0 deletions decore_base/prepare/spa/static/js/586.6cfbf2b5.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion decore_base/prepare/spa/static/js/586.aa14c175.js

This file was deleted.

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions decore_base/prepare/spa/static/js/vendor.0902ddb5.js

This file was deleted.

7 changes: 7 additions & 0 deletions decore_base/prepare/spa/static/js/vendor.6fe961ae.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion decore_base/prepare/spa/templates/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><base href=/static/ ><title>Uniform Front</title><meta charset=utf-8><meta name=description content="A part of Unform Famework"><meta name=format-detection content="telephone=no"><meta name=msapplication-tap-highlight content=no><meta name=viewport content="user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1,width=device-width"><meta name=api_prod_port content={{port}}><meta name=api_dev_port content=5566><link rel=icon type=image/png sizes=128x128 href=icons/favicon-128x128.png><link rel=icon type=image/png sizes=96x96 href=icons/favicon-96x96.png><link rel=icon type=image/png sizes=32x32 href=icons/favicon-32x32.png><link rel=icon type=image/png sizes=16x16 href=icons/favicon-16x16.png><link rel=icon type=image/ico href=favicon.ico><script defer src=/static/js/vendor.0902ddb5.js></script><script defer src=/static/js/app.af6cbe84.js></script><link href=/static/css/vendor.14c9ac7a.css rel=stylesheet><link href=/static/css/app.d398f07d.css rel=stylesheet></head><body><div id=q-app></div></body></html>
<!DOCTYPE html><html><head><base href=/static/ ><title>Uniform Front</title><meta charset=utf-8><meta name=description content="A part of Unform Famework"><meta name=format-detection content="telephone=no"><meta name=msapplication-tap-highlight content=no><meta name=viewport content="user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1,width=device-width"><meta name=api_prod_port content={{port}}><meta name=api_dev_port content=5566><link rel=icon type=image/png sizes=128x128 href=icons/favicon-128x128.png><link rel=icon type=image/png sizes=96x96 href=icons/favicon-96x96.png><link rel=icon type=image/png sizes=32x32 href=icons/favicon-32x32.png><link rel=icon type=image/png sizes=16x16 href=icons/favicon-16x16.png><link rel=icon type=image/ico href=favicon.ico><script defer src=/static/js/vendor.6fe961ae.js></script><script defer src=/static/js/app.aff5db90.js></script><link href=/static/css/vendor.14c9ac7a.css rel=stylesheet><link href=/static/css/app.d398f07d.css rel=stylesheet></head><body><div id=q-app></div></body></html>
1 change: 0 additions & 1 deletion decore_base/sample/bases/global_management_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def create_data(self):

def test_item(self):
t_account = Account_model()
t_account.id = None
t_account.title = 'test'
t_account.email = 'test@mail.com'
t_account.password = 'test'
Expand Down
2 changes: 1 addition & 1 deletion decore_base/sample/models/account_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
class Account_model(Conform_model):
person = ForeignKeyField(Person_model, backref='accounts', verbose_name='Person', null=True)
email = CharField(verbose_name='Email')
password = DecorePasswordField(verbose_name='Password')
password = PasswordField(verbose_name='Password')
2 changes: 1 addition & 1 deletion decore_base/sample/models/company_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from decore_base.uniform import *
from decore_base.uniform.conform_model import *
from .person_model import Person_model

class Company_model(Conform_model):
Expand Down
4 changes: 2 additions & 2 deletions decore_base/sample/models/test_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from decore_base.uniform.conform_model import *

class Test_model(Conform_model):
charfield = DecoreCharField(verbose_name='CharField', default='Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
charfield = CharField(verbose_name='CharField', default='Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
intfield = IntegerField(verbose_name='IntegerField', default=0)
textfield = TextField(verbose_name='TextField', default='Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.')
booleanfield = BooleanField(verbose_name='BooleanField', default=False)
passwordfield = DecorePasswordField(verbose_name='PasswordField', default='12345678')
passwordfield = PasswordField(verbose_name='PasswordField', default='12345678')
Binary file modified decore_base/sample/state/database.db
Binary file not shown.
Binary file modified decore_base/sample/state/keybase.kdbx
Binary file not shown.
1 change: 0 additions & 1 deletion decore_base/uniform/conform_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Meta:
# TODO - diese Zeile wieder einsetzen wenn alle Relationen zwischen conform und perform passen
# database = SqliteDatabase('state/database.db', pragmas=(('cache_size', -1024 * 64),('journal_mode', 'wal')))
database = SqliteDatabase('state/database.db')
migrator = SqliteMigrator(database)

@classmethod
def register(cls):
Expand Down

0 comments on commit 60fd897

Please sign in to comment.