mitrofun /
weakorm
| 1 | from datetime import datetime |
||
|
0 ignored issues
–
show
|
|||
| 2 | |||
| 3 | from weekorm import db |
||
| 4 | |||
| 5 | |||
| 6 | class Field: |
||
|
0 ignored issues
–
show
This class should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 7 | field_type = '' |
||
| 8 | default = '' |
||
| 9 | is_foreign_key = False |
||
| 10 | unique = False |
||
| 11 | not_null = False |
||
| 12 | python_type = str |
||
| 13 | |||
| 14 | def field_sql(self, field_name): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 15 | field_sql = f'"{field_name}" {self.field_type}' |
||
| 16 | if self.unique: |
||
| 17 | field_sql += ' UNIQUE' |
||
| 18 | if self.not_null: |
||
| 19 | field_sql += ' NOT NULL' |
||
| 20 | return field_sql |
||
| 21 | |||
| 22 | def to_python(self, value): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 23 | if self.python_type == datetime: |
||
| 24 | value = datetime.fromtimestamp(value) |
||
| 25 | return value |
||
| 26 | return self.python_type(value) |
||
| 27 | |||
| 28 | |||
| 29 | class CharField(Field): |
||
|
0 ignored issues
–
show
This class should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 30 | def __init__(self, max_length=255, default='', unique=False): |
||
| 31 | self.field_type = f'varchar({max_length})' |
||
| 32 | self.default = default |
||
| 33 | self.max_length = max_length |
||
| 34 | self.unique = unique |
||
| 35 | self.python_type = str |
||
| 36 | |||
| 37 | |||
| 38 | class IntegerField(Field): |
||
|
0 ignored issues
–
show
This class should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 39 | def __init__(self, default=0, unique=False): |
||
| 40 | self.field_type = 'integer' |
||
| 41 | self.default = default |
||
| 42 | self.unique = unique |
||
| 43 | self.python_type = int |
||
| 44 | |||
| 45 | |||
| 46 | class FloatField(Field): |
||
|
0 ignored issues
–
show
This class should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 47 | def __init__(self, default=0.0, unique=False): |
||
| 48 | self.field_type = 'real' |
||
| 49 | self.default = default |
||
| 50 | self.unique = unique |
||
| 51 | self.python_type = float |
||
| 52 | |||
| 53 | |||
| 54 | class BooleanField(Field): |
||
|
0 ignored issues
–
show
This class should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 55 | def __init__(self, default=False, unique=False): |
||
| 56 | self.field_type = 'integer' |
||
| 57 | self.default = default |
||
| 58 | self.unique = unique |
||
| 59 | self.python_type = bool |
||
| 60 | |||
| 61 | |||
| 62 | class DateTimeField(Field): |
||
|
0 ignored issues
–
show
This class should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 63 | def __init__(self, default=0.0, unique=False): |
||
| 64 | self.field_type = 'real' |
||
| 65 | self.default = default |
||
| 66 | self.unique = unique |
||
| 67 | self.python_type = datetime |
||
| 68 | |||
| 69 | |||
| 70 | class ForeignKey(Field): |
||
|
0 ignored issues
–
show
This class should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 71 | def __init__(self, model_class): |
||
| 72 | self.field_type = 'foreignkey' |
||
| 73 | self.is_foreign_key = True |
||
| 74 | self.model_class = model_class |
||
| 75 | self.python_type = int |
||
| 76 | |||
| 77 | def field_sql(self, field_name): |
||
| 78 | return f'"{field_name}" integer REFERENCES "{self.model_class.__name__.lower()}" ("id")' |
||
| 79 | |||
| 80 | |||
| 81 | class Model: |
||
|
0 ignored issues
–
show
This class should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 82 | |||
| 83 | def __init__(self, inst_id=0, **kwargs): |
||
| 84 | self.table_name = self.__class__.__name__.lower() |
||
| 85 | self.id = inst_id |
||
|
0 ignored issues
–
show
The name
id does not conform to the attribute naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. Loading history...
|
|||
| 86 | self.__class__.try_create_table() |
||
| 87 | |||
| 88 | for name in self.field_names: |
||
| 89 | field = getattr(self.__class__, name.replace("`", "")) |
||
| 90 | setattr(self, name.replace("`", ""), field.default) |
||
| 91 | for key, value in kwargs.items(): |
||
| 92 | setattr(self, key.replace("`", ""), value) |
||
| 93 | |||
| 94 | def __str__(self): |
||
| 95 | return f'{self.__class__.__name__}_{self.id}' |
||
| 96 | |||
| 97 | def __repr__(self): |
||
| 98 | return self.__str__() |
||
| 99 | |||
| 100 | @property |
||
| 101 | def field_names(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 102 | names = [] |
||
| 103 | for name in dir(self.__class__): |
||
| 104 | var = getattr(self.__class__, name.replace("`", "")) |
||
| 105 | if isinstance(var, Field): |
||
| 106 | names.append(f"`{name}`") |
||
| 107 | return names |
||
| 108 | |||
| 109 | @property |
||
| 110 | def field_values(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 111 | values = [] |
||
| 112 | for name in self.field_names: |
||
| 113 | value = getattr(self, name.replace("`", "")) |
||
| 114 | if isinstance(value, datetime): |
||
| 115 | value = value.timestamp() |
||
| 116 | if isinstance(value, Model): |
||
| 117 | value = value.id |
||
| 118 | if isinstance(value, str): |
||
| 119 | value = value.replace("'", "''") |
||
| 120 | values.append(f"'{value}'") |
||
| 121 | return values |
||
| 122 | |||
| 123 | def insert(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 124 | cursor = db.get_cursor() |
||
| 125 | field_names_sql = ", ".join(self.field_names) |
||
| 126 | field_values_sql = ", ".join(self.field_values) |
||
| 127 | |||
| 128 | sql = f"insert into `{self.table_name}`({ field_names_sql}) values({field_values_sql})" |
||
| 129 | db.execute_sql(cursor, sql) |
||
| 130 | db.db_commit() |
||
| 131 | |||
| 132 | sql = f"select id from `{self.table_name}` order by id desc;" |
||
| 133 | db.execute_sql(cursor, sql) |
||
| 134 | self.id = cursor.fetchone()[0] |
||
| 135 | |||
| 136 | def update(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 137 | cursor = db.get_cursor() |
||
| 138 | |||
| 139 | name_value = [] |
||
| 140 | |||
| 141 | for name, value in zip(self.field_names, self.field_values): |
||
| 142 | name_value.append(f"{name}={value}") |
||
| 143 | name_value_sql = ", ".join(name_value) |
||
| 144 | |||
| 145 | sql = f"update `{self.table_name}` set {name_value_sql} where id = {self.id}" |
||
| 146 | db.execute_sql(cursor, sql) |
||
| 147 | db.db_commit() |
||
| 148 | |||
| 149 | def save(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 150 | if self.id: |
||
| 151 | self.update() |
||
| 152 | else: |
||
| 153 | self.insert() |
||
| 154 | return self |
||
| 155 | |||
| 156 | def delete(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 157 | cursor = db.get_cursor() |
||
| 158 | sql = f"delete from `{self.table_name}` where id = {self.id}" |
||
| 159 | db.execute_sql(cursor, sql) |
||
| 160 | db.db_commit() |
||
| 161 | |||
| 162 | @classmethod |
||
| 163 | def try_create_table(cls): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 164 | table_name = cls.__name__.lower() |
||
| 165 | cursor = db.get_cursor() |
||
| 166 | sql = f"select * from sqlite_master where type='table' AND name='{table_name}';" |
||
| 167 | db.execute_sql(cursor, sql) |
||
| 168 | if not cursor.fetchall(): |
||
| 169 | sql = f"drop table if exists `{table_name}`;" |
||
| 170 | db.execute_sql(cursor, sql) |
||
| 171 | |||
| 172 | fields_sql = "" |
||
| 173 | for name in dir(cls): |
||
| 174 | var = getattr(cls, name.replace("`", "")) |
||
| 175 | if isinstance(var, Field): |
||
| 176 | field = var |
||
| 177 | field_sql = field.field_sql(name) |
||
| 178 | fields_sql += ", " + field_sql |
||
| 179 | sql = f'create table `{table_name}` ("id" integer not null primary key {fields_sql});' |
||
| 180 | db.execute_sql(cursor, sql) |
||
| 181 | |||
| 182 | db.db_commit() |
||
| 183 | |||
| 184 | @classmethod |
||
| 185 | def query(cls): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 186 | query = Query(cls) |
||
| 187 | return query |
||
| 188 | |||
| 189 | @classmethod |
||
| 190 | def get(cls, **kwargs): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 191 | query = Query(cls) |
||
| 192 | return query.filter(**kwargs).first() |
||
| 193 | |||
| 194 | |||
| 195 | class Query: |
||
|
0 ignored issues
–
show
This class should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 196 | |||
| 197 | def __init__(self, model_class): |
||
| 198 | model_class.try_create_table() |
||
| 199 | self.model_class = model_class |
||
| 200 | self.table_name = self.model_class.__name__ |
||
| 201 | self.where_sql = "1=1" |
||
| 202 | |||
| 203 | def __str__(self): |
||
| 204 | return f"{self.__class__.__name__}_{self.table_name,}_{self.query_sql}" |
||
| 205 | |||
| 206 | def __repr__(self): |
||
| 207 | return self.__str__() |
||
| 208 | |||
| 209 | @property |
||
| 210 | def field_names(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 211 | names = [] |
||
| 212 | for name in dir(self.model_class): |
||
| 213 | var = getattr(self.model_class, name.replace("`", "")) |
||
| 214 | if isinstance(var, Field): |
||
| 215 | names.append(f"`{name}`") |
||
| 216 | return names |
||
| 217 | |||
| 218 | @property |
||
| 219 | def query_sql(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 220 | sql = f"select * from `{self.table_name}` where {self.where_sql};" |
||
| 221 | return sql |
||
| 222 | |||
| 223 | def filter(self, operator="=", **kwargs): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 224 | where_sql = self.where_sql |
||
| 225 | for name, value in kwargs.items(): |
||
| 226 | if f"`{name}`" in self.field_names + ["`id`"]: |
||
| 227 | if isinstance(value, Model): |
||
| 228 | value = value.id |
||
| 229 | if isinstance(value, str): |
||
| 230 | value = value.replace("'", "''") |
||
| 231 | where_sql += f" and `{name}` {operator} '{value}'" |
||
| 232 | query = self.__class__(self.model_class) |
||
| 233 | query.where_sql = where_sql |
||
| 234 | return query |
||
| 235 | |||
| 236 | def _r2ob(self, item): |
||
| 237 | value = '' |
||
| 238 | inst_id = item[0] |
||
| 239 | obj = self.model_class(inst_id=inst_id) |
||
| 240 | for i in range(1, len(item)): |
||
| 241 | name = self.field_names[i - 1] |
||
| 242 | field = getattr(self.model_class, name.replace("`", "")) |
||
| 243 | if not field.is_foreign_key: |
||
| 244 | value = field.to_python(item[i]) |
||
| 245 | if field.is_foreign_key: |
||
| 246 | if field.field_type == "foreignkey": |
||
| 247 | fid = item[i] |
||
| 248 | value = field.model_class.get(id=fid) |
||
| 249 | setattr(obj, name.replace("`", ""), value) |
||
| 250 | return obj |
||
| 251 | |||
| 252 | def all(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 253 | cursor = db.get_cursor() |
||
| 254 | sql = self.query_sql |
||
| 255 | db.execute_sql(cursor, sql) |
||
| 256 | rows = cursor.fetchall() |
||
| 257 | obs = [] |
||
| 258 | for item in rows: |
||
| 259 | ob = self._r2ob(item) |
||
|
0 ignored issues
–
show
The name
ob does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. Loading history...
|
|||
| 260 | obs.append(ob) |
||
| 261 | return obs |
||
| 262 | |||
| 263 | def first(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 264 | cursor = db.get_cursor() |
||
| 265 | sql = self.query_sql |
||
| 266 | db.execute_sql(cursor, sql) |
||
| 267 | rows = cursor.fetchall() |
||
| 268 | if rows: |
||
|
0 ignored issues
–
show
|
|||
| 269 | r = rows[0] |
||
|
0 ignored issues
–
show
The name
r does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. Loading history...
|
|||
| 270 | ob = self._r2ob(r) |
||
|
0 ignored issues
–
show
The name
ob does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. Loading history...
|
|||
| 271 | return ob |
||
| 272 | else: |
||
| 273 | return None |
||
| 274 | |||
| 275 | def delete(self): |
||
|
0 ignored issues
–
show
This method should have a docstring.
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods: class SomeClass:
def some_method(self):
"""Do x and return foo."""
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions. Loading history...
|
|||
| 276 | cursor = db.get_cursor() |
||
| 277 | sql = f"delete from `{self.table_name}` where {self.where_sql}" |
||
| 278 | db.execute_sql(cursor, sql) |
||
| 279 | db.db_commit() |
||
| 280 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.