2020-07-05 23:40:12 +08:00
|
|
|
# The core application of the Mia project.
|
|
|
|
# by imacat <imacat@mail.imacat.idv.tw>, 2020/6/29
|
|
|
|
|
|
|
|
# Copyright (c) 2020 imacat.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-07-08 07:24:36 +08:00
|
|
|
"""The data models of the Mia core application.
|
2020-07-05 23:40:12 +08:00
|
|
|
|
|
|
|
"""
|
2020-08-09 22:41:22 +08:00
|
|
|
import hashlib
|
|
|
|
|
2020-08-02 09:53:26 +08:00
|
|
|
from dirtyfields import DirtyFieldsMixin
|
2020-08-09 22:41:22 +08:00
|
|
|
from django.conf import settings
|
2020-08-17 09:29:03 +08:00
|
|
|
from django.db import models, connection, OperationalError, transaction, \
|
|
|
|
ProgrammingError
|
2020-08-10 02:39:05 +08:00
|
|
|
from django.db.models.functions import Now
|
2020-07-04 08:17:08 +08:00
|
|
|
|
2020-08-09 22:41:22 +08:00
|
|
|
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr, \
|
|
|
|
new_pk
|
2020-07-14 22:01:32 +08:00
|
|
|
|
2020-07-05 23:40:12 +08:00
|
|
|
|
2020-08-02 09:53:26 +08:00
|
|
|
class Country(DirtyFieldsMixin, models.Model):
|
2020-07-05 23:40:12 +08:00
|
|
|
"""A country."""
|
2020-08-10 23:35:21 +08:00
|
|
|
id = models.PositiveIntegerField(primary_key=True, db_column="sn")
|
|
|
|
code = models.CharField(max_length=2, unique=True, db_column="id")
|
2020-07-05 23:40:12 +08:00
|
|
|
name_en = models.CharField(max_length=64)
|
2020-07-16 23:13:15 +08:00
|
|
|
name_zh_hant = models.CharField(
|
|
|
|
max_length=32, null=True, db_column="name_zhtw")
|
|
|
|
name_zh_hans = models.CharField(
|
|
|
|
max_length=32, null=True, db_column="name_zhcn")
|
2020-07-05 23:40:12 +08:00
|
|
|
is_special = models.BooleanField(
|
|
|
|
default=False, db_column="special")
|
|
|
|
created_at = models.DateTimeField(
|
|
|
|
auto_now_add=True, db_column="created")
|
|
|
|
created_by = models.ForeignKey(
|
|
|
|
"User", on_delete=models.PROTECT,
|
|
|
|
db_column="createdby", related_name="created_countries")
|
|
|
|
updated_at = models.DateTimeField(
|
2020-08-09 16:34:47 +08:00
|
|
|
auto_now=True, db_column="updated")
|
2020-07-05 23:40:12 +08:00
|
|
|
updated_by = models.ForeignKey(
|
|
|
|
"User", on_delete=models.PROTECT,
|
|
|
|
db_column="updatedby", related_name="updated_countries")
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""Returns the string representation of this country."""
|
2020-07-16 23:13:15 +08:00
|
|
|
return self.code.__str__() + " " + self.name.__str__()
|
2020-07-05 23:40:12 +08:00
|
|
|
|
2020-07-14 22:01:32 +08:00
|
|
|
@property
|
2020-08-13 08:05:35 +08:00
|
|
|
def name(self) -> str:
|
|
|
|
"""The country name in the current language."""
|
2020-07-21 21:23:54 +08:00
|
|
|
return get_multi_lingual_attr(self, "name", "en")
|
2020-07-14 22:01:32 +08:00
|
|
|
|
2020-07-16 23:13:15 +08:00
|
|
|
@name.setter
|
2020-08-13 08:05:35 +08:00
|
|
|
def name(self, value: str) -> None:
|
2020-07-21 21:23:54 +08:00
|
|
|
set_multi_lingual_attr(self, "name", value)
|
2020-07-14 22:01:32 +08:00
|
|
|
|
2020-07-05 23:40:12 +08:00
|
|
|
class Meta:
|
2020-08-10 23:35:21 +08:00
|
|
|
db_table = "country"
|
2020-07-05 23:40:12 +08:00
|
|
|
|
|
|
|
|
2020-08-02 09:53:26 +08:00
|
|
|
class User(DirtyFieldsMixin, models.Model):
|
2020-07-05 23:40:12 +08:00
|
|
|
"""A user."""
|
2020-08-10 23:35:21 +08:00
|
|
|
id = models.PositiveIntegerField(primary_key=True, db_column="sn")
|
2020-07-05 23:40:12 +08:00
|
|
|
login_id = models.CharField(max_length=32, unique=True, db_column="id")
|
|
|
|
password = models.CharField(max_length=32, db_column="passwd")
|
|
|
|
name = models.CharField(max_length=32)
|
|
|
|
is_disabled = models.BooleanField(
|
|
|
|
default=False, db_column="disabled")
|
|
|
|
is_deleted = models.BooleanField(
|
|
|
|
default=False, db_column="deleted")
|
|
|
|
language = models.CharField(max_length=6, null=True, db_column="lang")
|
2020-08-10 02:39:05 +08:00
|
|
|
visits = models.PositiveSmallIntegerField(default=0)
|
2020-07-05 23:40:12 +08:00
|
|
|
visited_at = models.DateTimeField(null=True, db_column="visited")
|
|
|
|
visited_ip = models.GenericIPAddressField(null=True, db_column="ip")
|
2020-07-16 22:25:33 +08:00
|
|
|
visited_host = models.CharField(
|
|
|
|
max_length=128, null=True, db_column="host")
|
2020-07-05 23:40:12 +08:00
|
|
|
visited_country = models.ForeignKey(
|
|
|
|
Country, on_delete=models.PROTECT, null=True,
|
|
|
|
db_column="ct", to_field="code", related_name="users")
|
|
|
|
created_at = models.DateTimeField(
|
|
|
|
auto_now_add=True, db_column="created")
|
|
|
|
created_by = models.ForeignKey(
|
|
|
|
"self", on_delete=models.PROTECT,
|
|
|
|
db_column="createdby", related_name="created_users")
|
|
|
|
updated_at = models.DateTimeField(
|
2020-08-10 02:39:05 +08:00
|
|
|
auto_now_add=True, db_column="updated")
|
2020-07-05 23:40:12 +08:00
|
|
|
updated_by = models.ForeignKey(
|
|
|
|
"self", on_delete=models.PROTECT,
|
|
|
|
db_column="updatedby", related_name="updated_users")
|
2020-08-10 23:35:21 +08:00
|
|
|
REQUIRED_FIELDS = ["id", "name"]
|
2020-07-05 23:40:12 +08:00
|
|
|
USERNAME_FIELD = "login_id"
|
|
|
|
|
2020-08-13 15:43:04 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.current_user = None
|
|
|
|
|
2020-07-05 23:40:12 +08:00
|
|
|
@property
|
2020-08-13 08:05:35 +08:00
|
|
|
def is_anonymous(self) -> bool:
|
2020-07-05 23:40:12 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
2020-08-13 08:05:35 +08:00
|
|
|
def is_authenticated(self) -> bool:
|
2020-07-05 23:40:12 +08:00
|
|
|
return True
|
|
|
|
|
|
|
|
def set_password(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def check_password(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""Returns the string representation of this user."""
|
2020-08-09 20:27:05 +08:00
|
|
|
return "%s (%s)" % (
|
2020-07-05 23:40:12 +08:00
|
|
|
self.name.__str__(), self.login_id.__str__())
|
|
|
|
|
2020-08-13 15:43:04 +08:00
|
|
|
def save(self, force_insert=False, force_update=False, using=None,
|
|
|
|
update_fields=None):
|
2020-08-09 22:41:22 +08:00
|
|
|
if self.pk is None:
|
|
|
|
self.pk = new_pk(User)
|
2020-08-13 15:43:04 +08:00
|
|
|
if self.current_user is not None:
|
|
|
|
self.created_by = self.current_user
|
|
|
|
if self.current_user is not None:
|
|
|
|
self.updated_by = self.current_user
|
2020-08-09 22:41:22 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
super(User, self).save(
|
|
|
|
force_insert=force_insert, force_update=force_update,
|
|
|
|
using=using, update_fields=update_fields)
|
2020-08-10 02:39:05 +08:00
|
|
|
User.objects.filter(pk=self.pk).update(updated_at=Now())
|
2020-08-09 22:41:22 +08:00
|
|
|
|
2020-07-05 23:40:12 +08:00
|
|
|
class Meta:
|
|
|
|
db_table = "users"
|
|
|
|
app_label = "mia_core"
|
2020-08-09 20:22:37 +08:00
|
|
|
|
2020-08-09 22:41:22 +08:00
|
|
|
def set_digest_password(self, login_id, password):
|
|
|
|
self.password = self.md5(
|
|
|
|
F"{login_id}:{settings.DIGEST_REALM}:{password}")
|
|
|
|
|
|
|
|
@staticmethod
|
2020-08-13 08:05:35 +08:00
|
|
|
def md5(value: str) -> str:
|
2020-08-09 22:41:22 +08:00
|
|
|
m = hashlib.md5()
|
|
|
|
m.update(value.encode("utf-8"))
|
|
|
|
return m.hexdigest()
|
|
|
|
|
2020-08-13 08:05:35 +08:00
|
|
|
def is_in_use(self) -> bool:
|
2020-08-09 20:22:37 +08:00
|
|
|
"""Returns whether this user is in use.
|
|
|
|
|
|
|
|
Returns:
|
2020-08-13 08:05:35 +08:00
|
|
|
True if this user is in use, or False otherwise.
|
2020-08-09 20:22:37 +08:00
|
|
|
"""
|
|
|
|
for table in connection.introspection.table_names():
|
|
|
|
if self._is_in_use_with(F"SELECT * FROM {table}"
|
|
|
|
" WHERE createdby=%s OR updatedby=%s"):
|
|
|
|
return True
|
|
|
|
if self._is_in_use_with(
|
|
|
|
F"SELECT * FROM {table}"
|
|
|
|
" WHERE created_by_id=%s OR updated_by_id=%s"):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-08-13 08:05:35 +08:00
|
|
|
def _is_in_use_with(self, sql: str) -> bool:
|
2020-08-09 20:22:37 +08:00
|
|
|
"""Returns whether this user is in use with a specific SQL statement.
|
|
|
|
|
|
|
|
Args:
|
2020-08-13 08:05:35 +08:00
|
|
|
sql: The SQL query statement
|
2020-08-09 20:22:37 +08:00
|
|
|
|
|
|
|
Returns:
|
2020-08-13 08:05:35 +08:00
|
|
|
True if this user is in use, or False otherwise.
|
2020-08-09 20:22:37 +08:00
|
|
|
"""
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
try:
|
|
|
|
cursor.execute(sql, [self.pk, self.pk])
|
|
|
|
except OperationalError:
|
|
|
|
return False
|
2020-08-17 09:29:03 +08:00
|
|
|
except ProgrammingError:
|
|
|
|
return False
|
2020-08-09 20:22:37 +08:00
|
|
|
if cursor.fetchone() is None:
|
|
|
|
return False
|
|
|
|
return True
|