@author: LiuZhi @time: 2019-01-11 23:54 @contact: [email protected] @software: PyCharm """ import math from datetime import datetime from base.base_extend import db from models.consts import K_POST from models.utils import incr_key from base.mc import cache, rdb from flask_app.utils import get_config PER_PAGE = get_config().PER_PAGE MC_KEY_STATS_N = 'like_n:%s:%s:%s' # action_type, target_id, target_kind MC_KEY_BY_UESR_STATS_N = 'like_n_by_user:%s:%s:%s' # action_type, user_id, target_kind MC_KEY_ACTION_ITEMS = 'action_items:%s:%s:%s:%s' # action_type, target_id, target_kind, page MC_KEY_ACTION_ITEM_BY_UESR = 'action_item_by_user:%s:%s:%s:%s' # action_type, user_id, target_id, target_kind MC_KEY_ACTION_ITEMS_BY_UESR = 'action_items_by_user:%s:%s:%s:%s' # action_type, user_id, target_kind, page class BaseMixin: @classmethod def get_db_props(cls, kwargs): """ 获取模型类的props,kwargs为要创建模型记录的数据, 通过cls._db_columns获取不存储进MySQL的字段,把不存储进MySQL的数据序列化成一个字典返回
@author: LiuZhi @time: 2019-01-18 21:34 @contact: [email protected] @software: PyCharm """ import smtplib from flask_mail import sanitize_addresses from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from flask_app.utils import get_config CONFIG = get_config() FROM_USER, EXMAIL_PASSWORD = CONFIG.FROM_USER, CONFIG.EXMAIL_PASSWORD def send_mail_task(msg): send_to = list(sanitize_addresses(msg.send_to)) part = MIMEText(msg.html, 'html') mp = MIMEMultipart('alternative') mp['Subject'] = msg.subject mp['From'] = FROM_USER mp['To'] = ','.join(send_to) mp.attach(part) s = smtplib.SMTP_SSL("smtp.qq.com", port=465) s.login(FROM_USER, EXMAIL_PASSWORD)
@contact: [email protected] @software: PyCharm redis 数据库相关 """ import copy import json from datetime import datetime from walrus import Database as _Database from sqlalchemy.ext.serializer import loads, dumps from flask_app.utils import get_config from .local_cache import lc REDIS_URL = get_config().REDIS_URL class Database(_Database): def get2(self, name): rs = super().get(name) return loads(rs) def set2(self, name, value, ex=None, px=None, nx=False, xx=False): value = dumps(value) return super().set(name, value, ex=ex, px=px, nx=nx, xx=xx) rdb = Database.from_url(REDIS_URL)
from sqlalchemy import (Column, DateTime, Integer, event, inspect) from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base from sqlalchemy.orm.interfaces import MapperOption from sqlalchemy.orm.attributes import get_history from sqlalchemy.ext.declarative import declared_attr from dogpile.cache.region import make_region from dogpile.cache.api import NO_VALUE from flask import abort from flask_sqlalchemy import (SQLAlchemy, Model, BaseQuery, DefaultMeta, _QueryProperty) from flask_security import Security from flask_app.utils import get_config from .redis_db import PropsMixin, PropsItem REDIS_URL = get_config().REDIS_URL # 缓存服务地址 def md5_key_mangler(key): if key.startswith('SELECT '): key = hashlib.md5(key.encode('ascii')).hexdigest() return key def memoize(obj): """ py3 functools.lru_cache 的实现 """ cache = obj.cache = {} @functools.wraps(obj)
import os from sqlalchemy import func as alchemyFn from flask_security import SQLAlchemyUserDatastore, UserMixin, RoleMixin import requests from base.base_extend import db from base.utils import generate_id from base.mixin import BaseMixin from .contact import Contact, userFollowStats from base.utils import cached_hybrid_property from flask_app.utils import get_config UPLOAD_FOLDER = get_config().UPLOAD_FOLDER roles_users = db.Table( 'roles_users', db.Column('user_id', db.Integer(), db.ForeignKey('users.id')), db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))) class BranSQLAlchemyUserDatastore(SQLAlchemyUserDatastore): def get_user_name(self, identifier): return self._get_user(identifier, 'name') def get_user_email(self, identifier): return self._get_user(identifier, 'email') def _get_user(self, identifier, attr):