def convert_table(table_name): db = mydb.MyDB() db2 = mydb.MyDB('default2') sql = ''' SELECT * FROM {table_name} LIMIT 1 '''.format(table_name=table_name) rs = db2.SqlQuery(sql, {}) if not rs: return columns = list(rs[0].keys()) if 'id' not in columns: sql = ''' SELECT * FROM {table_name} '''.format(table_name=table_name) rs = db2.SqlQuery(sql, {}) for r in rs: convert_row(db, table_name, columns, r) return rid = -10 while True: rid, rs_len = convert_batch(db, table_name, columns, rid) if not rs_len: break
def highload_import(request): sql = ''' SELECT ID, UNIX_TIMESTAMP(post_date) utime, post_title, post_content FROM wp_posts WHERE post_status = 'publish' ORDER BY id ''' sql_tmpl = ''' INSERT INTO message (id, id_parent, title, time, text, author, category, allow, attach, ip) VALUES (@id@, @id_parent@, @title@, @time@, @text@, @author@, @category@, @allow@, @attach@, @ip@) ''' sql_tmpl2 = ''' insert into blog(id, message_id) values (@id@, @message_id@) ''' db2 = mydb.MyDB('default2') rs = db2.SqlQuery(sql, {}) db = mydb.MyDB() for r in rs: _id = get_id() db.SqlQuery(sql_tmpl2, { 'id': r['ID'], 'message_id': _id }, True) db.SqlQuery(sql_tmpl, { 'id': _id, 'id_parent': 0, 'title': r['post_title'], 'time': r['utime'], 'text': r['post_content'], 'author': 'my_fess', 'category': None, 'allow': 'yes', 'attach': 'no', 'ip': None }, True) res = json_dumps({'bom': 'bom'}) return HttpResponse(res, content_type='application/json')
def set_friend_status(params, request): friend_id = params.get('friend_id') assert friend_id status = params.get('status') if status != 'friend': status = 'deleted' res = { 'friend_status': status } user = auth.MyUser(request, force=True) if user.set_sid: res['sid'] = user.set_sid db = mydb.MyDB() db.SqlQueryRecord(db.sql('sport/friend_set_status'), { 'member_id': user.get_user_id(), 'friend_id': friend_id, 'status': status }) return res
def get_friends_challenges_map(member_id): """ Get challenges with friends of user Args: member_id: int, user id Returns: dict: challenges map key: int, challenge id value: dict: user id: int, user id name: str, social network name """ db = mydb.MyDB() rs = db.SqlQuery(db.sql('sport/challenges_list_friends'), { 'friends': get_friends_list(member_id) }) ds = {} for r in rs: ds[r['challenge_id']] = r.get('friends') or [] return ds
def get_message(params, request): """ Получение сообщения для формы редактирования сообщения """ mid = int(params['id']) user = auth.MyUser(request) db = mydb.MyDB() if not user.is_editor(): raise Exception('У вас нет прав изменять это сообщение.') rs = db.SqlQuery(db.sql('message_get'), {'id': mid}) if len(rs) != 1: raise Exception('Сообщение не найдено') m = rs[0] m['text'] = m['text'].replace('[br]', '\n') m['can_delete'] = user.is_editor() m['is_comment'] = m['id_parent'] is not None and m['id_parent'] != 0 m['is_news'] = not m['is_comment'] m['is_board_theme'] = False m['is_blog_post'] = m['is_blog_post'] return m
def message(request, page=1): """ Построение ленты """ if 'highload.org' in request.META['HTTP_HOST']: return message_highload(request, page) db = mydb.MyDB() context = get_default_context(request) page = int(page) start = (page - 1) * consts.COUNT_MESSAGES_PAGE cats = get_news_cats('news') sql = db.sql('news_count').format(cats=cats) count_messages = rs = db.SqlQueryScalar(sql, {'cats': cats}) sql = db.sql('news_list').format(cats=cats) rs = db.SqlQuery(sql, { 'start': start, 'count': consts.COUNT_MESSAGES_PAGE, 'cats': cats }) news = [] for r in rs: news.append(get_message_text(request, r)) context['news'] = news context['PAGE_SELECT'] = create_page_select(count_messages, consts.COUNT_MESSAGES_PAGE, page, 'news/all/') return render(request, 'app/messages/messages.html', context)
def get_url_comment(comment_id): db = mydb.MyDB() p_id = get_main_parent_id(comment_id) if p_id == -1: return -1 parent = db.SqlQueryRecord(db.sql('message_comments_parent'), { 'pid': p_id, 'cid': comment_id }) page = int(parent['_count'] // consts.COUNT_COMMENTS_PAGE) + 1 r = {'type': '', 'url': ''} if parent['news']: r['type'] = 'news' r['url'] = 'news/{}/{}/gotocomment/{}/'.format(p_id, page, comment_id) elif parent['teachers']: r['type'] = 'teacher' r['url'] = 'teachers/{}/{}/gotocomment/{}/'.format( p_id, page, comment_id) if parent['photos_user_id']: u_id = parent['photos_user_id'] user_photos = db.SqlQueryScalar(db.sql('message_photos_before'), { 'user_id': u_id, 'id': p_id }) r['type'] = 'photo' r['url'] = 'photos/{}/{}/new/-1/all/1/{}/gotocomment/{}/'.format( u_id, user_photos, page, comment_id) return r
def comments_mod(request): """ Модерирование комментариев """ db = mydb.MyDB() context = get_default_context(request) user = auth.MyUser(request) if not user.is_editor(): return render(request, 'app/static/403.html', context) _sql = db.sql('mod_comments') sql = _sql.format(cols='count(*) cnt', orderby='') context['COUNT_COMMENTS'] = db.SqlQueryScalar(sql) sql = _sql.format(cols='*', orderby='ORDER BY m.time DESC LIMIT 30') rs = db.SqlQuery(sql) comments = [] for r in rs: m = get_message_text(request, r, is_comment=True) m['parent'] = get_url_comment(r['id']) comments.append(m) context['comments'] = comments return render(request, 'app/mod/comments.html', context)
def restore_pass(params, request): """ Заявка на восстановлние пароля """ secret = uuid.uuid4() user = get_user_by(email=params['email']) member_id = user['id'] if member_id < 1: return {'msg': 'Email не найден'} db = mydb.MyDB() db.SqlQuery(db.sql('auth_restore'), { 'member_id': member_id, 'secret': secret }, True) email = user['email'] if not email or email == -1: raise Exception('Пустой Email') tpl = render_to_string('app/email/restore_pass.html') tpl = tpl.format(LOGIN=user['name'], DOMEN=consts.DOMEN, NAV_CAPTION=consts.NAV_CAPTION, SECRET=secret) subject = 'Восстановление пароля' my_mail(subject, tpl, email) return {'result': True}
def blog_post(request, post_id, page=1, gotocomment=None): db = mydb.MyDB() context = get_default_context(request) mid = db.SqlQueryScalar(db.sql('blog_post'), {'id': post_id}) rs = db.SqlQuery(db.sql('message_full'), {'mid': mid}) if len(rs) != 1: return render(request, 'app/static/unknow.html', context) context['msg'] = get_message_text(request, rs[0], is_comment=False, blog=True) context['msg']['en'] = 'blog' context['msg']['ru'] = 'блог' context['msg']['url_pageless'] = 'post{}'.format(post_id) context['LEFT_MENU'] = False context['ADDITIONAL_PARAMS'] = ''' 'comments_page': {}, '''.format(page) context['COMMENT_ID'] = gotocomment context['NAV_CAPTION'] = 'HighLoad.org' context['HIGHLOAD'] = True return render(request, 'app/highload/full_message.html', context)
def restore_change_pass(params, request): """ Смена пароля """ db = mydb.MyDB() secret = uuid.UUID(params['secret']) new_pass = params['password'] if len(new_pass) < 4: return {'msg': 'Пароль не может быть короче 4 символов.'} new_pass = md5(new_pass) rs = db.SqlQuery(db.sql('auth_secret_check'), {'secret': secret}) if not rs: return { 'msg': 'Неверная ссылка для восстановления, возможно она устарела, получите новую.' } mid = rs[0]['member_id'] rs = db.SqlQuery(db.sql('auth_change_pass'), { 'mid': mid, 'password': new_pass }) if not rs: raise Exception('Не удалось сменить пароль') tpl = render_to_string('app/email/password_changed.html') tpl = tpl.format(LOGIN=rs[0]['name'], DOMEN=consts.DOMEN, NAV_CAPTION=consts.NAV_CAPTION) subject = 'Пароль изменён' my_mail(subject, tpl, rs[0]['email']) return {'result': True}
def delete_message(params, request): user = auth.MyUser(request) mid = int(params['id']) if not user.is_editor(): raise Exception('Недостаточно прав чтобы удалить сообщение') db = mydb.MyDB() db.SqlQuery(db.sql('message_delete'), {'id': mid}, True) return {'result': True, 'message_id': mid}
def delete_teacher(params, request): user = auth.MyUser(request) if not user.is_editor(): raise Exception('Недостаточно прав чтобы удалить сообщение') db = mydb.MyDB() db.SqlQuery(db.sql('teachers_delete'), {'id': int(params['id'])}, True) return {'result': True}
def auth(auth_login): db = mydb.MyDB() sid = md5(get_rand_string()) db.SqlQuery(db.sql('auth_sign_in'), { 'cookie_ID': sid, 'auth_login': auth_login }, True) return sid
def teachers_mod(request): db = mydb.MyDB() context = get_default_context(request) user = auth.MyUser(request) if not user.is_editor(): return render(request, 'app/static/403.html', context) context['teachers'] = db.SqlQuery(db.sql('mod_teachers')) return render(request, 'app/mod/teachers.html', context)
def cache_check(method_name, params): db = mydb.MyDB() sql = db.sql('cache_check').format(CACHE_INTERVAL=consts.CACHE_INTERVAL) rs = db.SqlQuery(sql, { 'method': method_name, 'params': json_dumps(params) }) if rs: return True, rs[0]['value'] return False, None
def teacher_read(params, request): db = mydb.MyDB() if not params.get('id'): raise Exception( 'Невозможно прочитать преподавателя без идентификатора') rs = db.SqlQuery(db.sql('teachers_read'), {'id': int(params['id'])}) if len(rs) == 1: return rs[0] raise Exception('Преподователь не найден')
def registraion(params, request): """ Регистрация нового пользователя """ db = mydb.MyDB() new_pass = params['password'] email = params['email'] login = params['login'] if '@' not in parseaddr(email)[1]: return {'msg': 'Вы ввели неверный email.'} if len(new_pass) < 4: return {'msg': 'Пароль не может быть короче 4 символов.'} new_pass = md5(new_pass) pattern = re.compile('^[a-zA-Z0-9-_]+$') if not pattern.match(login): msg = ('В имене пользователя используются недопустимые символы. ' 'Используйте латинские буквы, цифры, подчеркивание или тире') return {'msg': msg} email_count = db.SqlQueryScalar(db.sql('auth_check_email'), {'email': email}) if email_count: return { 'msg': 'Пользователь с указанным email-ом уже зарегистрирован.' } login_count = db.SqlQueryScalar(db.sql('auth_name_check'), {'login': login}) if login_count: return {'msg': 'Пользователь с указанным именем уже зарегистрирован.'} rs = db.SqlQuery(db.sql('auth_registraion'), { 'name': login, 'password': new_pass, 'email': email, }) if not rs: raise Exception('Не удалось зарегистрироваться') tpl = render_to_string('app/email/registered.html') tpl = tpl.format(LOGIN=rs[0]['name'], DOMEN=consts.DOMEN, NAV_CAPTION=consts.NAV_CAPTION) subject = 'Вы зарегистрировались' my_mail(subject, tpl, rs[0]['email']) return {'result': True}
def cache_add(method_name, params, result): """ Добавление/обновления параметров вызова и результата в кэш """ db = mydb.MyDB() db.SqlQuery(db.sql('cache_add'), { 'value': result, 'method': method_name, 'params': json_dumps(params) }, True)
def board_import_users(request): rid = -100 sql = ''' SELECT id, name, password, email, avatar FROM ipbmembers WHERE id > @id@ ORDER BY id LIMIT 1000 ''' sql_tmpl = ''' INSERT INTO members (id, name, password, email, avatar) VALUES (@id@, @name@, @password@, @email@, @avatar@) ''' db = mydb.MyDB() while True: #connections['default4'].close() db2 = mydb.MyDB('default4') rs = db2.SqlQuery(sql, {'id': rid}) if not rs: return rid = rs[-1]['id'] for r in rs: db.SqlQuery(sql_tmpl, { 'id': r['id'], 'name': r['name'], 'password': r['password'], 'email': r['email'], 'avatar': (None if r['avatar'] == 'noavatar' else r['avatar']) }, True) res = json_dumps({'bom': 'bom'}) return HttpResponse(res, content_type='application/json')
def get_msg_pages_count(id_parent): db = mydb.MyDB() comments_count = db.SqlQueryScalar(db.sql('comments_count'), {'id_parent': id_parent}) pages_count = 0 if comments_count % consts.COUNT_COMMENTS_PAGE != 0: pages_count = (((comments_count - (comments_count % consts.COUNT_COMMENTS_PAGE)) // consts.COUNT_COMMENTS_PAGE) + 1) else: pages_count = comments_count // consts.COUNT_COMMENTS_PAGE return pages_count
def get_type(type_id, type_text): db = mydb.MyDB() if type_id != 0 and type_id != -1: return int(type_id) if type_text == '': return None rs = db.SqlQuery(db.sql('edu_files_type_get'), {'type': type_text}) if rs: return int(rs[0]['id']) last_id = db.SqlQuery(db.sql('edu_files_type_insert'), {'type': type_text}) return last_id
def get_main_EN_from_ID(ID): db = mydb.MyDB() try: ID = int(ID) rs = db.SqlQuery(db.sql('message_category'), {'id': ID}) if not rs: return 'ERROR' r = rs[0] if r['id_parent'] != 0: return get_main_EN_from_ID(r['id_parent']) except Exception: return 'Ошибка в категории' return r['EN']
def get_blog_rss(request): db = mydb.MyDB() rs = db.SqlQuery(db.sql('blog_posts'), { 'start': 0, 'count': 10, 'username': None }) items = '' for r in rs: t = get_message_text(request, r, is_comment=False, blog=True, preview=False) _time = datetime.datetime.fromtimestamp( r['time']).strftime('%a, %d %b %Y %H:%M:%S %z') s = ''' <item> <title>{title}</title> <link>http://highload.org/post{post_id}</link> <guid>http://highload.org/post{post_id}</guid> <description><![CDATA[{text}]]></description> <author>my_fess</author> <pubDate>{pub_date}</pubDate> </item> '''.format(title=r['title'], text=t['message_text'], post_id=r['blog_post_id'], pub_date=_time) items += s res = '''<?xml version="1.0" encoding="UTF-8"?>''' res += ''' <rss version="2.0"> <channel> <title>HighLoad.org</title> <link>http://highload.org</link> <description>HighLoad.org – блог о высоких нагрузках</description> </channel> {items} </rss> '''.format(items=items) return HttpResponse(res, content_type='text/xml')
def get_files_for_edu_main(request, subject=None, type_id=None): context = get_default_context(request) db = mydb.MyDB() # Заполняем шаблон правильным случаем, в зависимости от переданных параметров bread_crumbs = [{'text': consts.NAV_CAPTION, 'link': '/'}] context['RIGHT_MENU'] = False if not subject: bread_crumbs.append({'text': 'Файлы для учёбы', 'last': True}) context['BREAD_CRUMBS'] = json_dumps(bread_crumbs) context['SUBJECT_ID'] = 'null' context['TYPE_ID'] = 'null' elif not type_id: subject_name = db.SqlQueryScalar(db.sql('edu_files_subject_get_by_id'), {'id': int(subject)}) bread_crumbs.append({ 'text': 'Файлы для учёбы', 'link': '/files_for_edu/' }) bread_crumbs.append({'text': subject_name, 'last': True}) context['BREAD_CRUMBS'] = json_dumps(bread_crumbs) context['SUBJECT_ID'] = int(subject) context['TYPE_ID'] = 'null' context['RIGHT_MENU'] = True else: names = db.SqlQuery(db.sql('edu_files_subject_type_get'), { 'sid': int(subject), 'tid': int(type_id) }) bread_crumbs.append({ 'text': 'Файлы для учёбы', 'link': '/files_for_edu/' }) bread_crumbs.append({ 'text': names[0]['subject'], 'link': '/files_for_edu/{}'.format(int(subject)) }) bread_crumbs.append({'text': names[0]['type'], 'last': True}) context['BREAD_CRUMBS'] = json_dumps(bread_crumbs) context['SUBJECT_ID'] = int(subject) context['TYPE_ID'] = int(type_id) context['ENABLE_ADD_FILE'] = consts.ENABLE_ADD_FILE return render(request, 'app/files_for_edu/main.html', context)
def convert_batch(db, table_name, columns, rid): connections['default2'].close() db2 = mydb.MyDB('default2') sql = ''' SELECT * FROM {table_name} WHERE id > @id@ ORDER BY id LIMIT 1000 '''.format(table_name=table_name) rs = db2.SqlQuery(sql, {'id': rid}) if not rs: return None, 0 for r in rs: convert_row(db, table_name, columns, r) return rs[-1]['id'], len(rs)
def get_busy_days(member_id): db = mydb.MyDB() where = ''' AND p.part_type = 'registered' AND p.user_id = @member_id@ ''' sql = db.sql('sport/challenges_list_my') sql = sql.format(where=where, cte='', join='') rs = db.SqlQuery(sql, {'member_id': member_id, 'filters': []}) dates = [] for r in rs: dt = datetime.datetime.strptime(r['data']['date'], '%Y-%m-%d') dates.append(dt) return dates
def get_board_theme_comments(request, theme_id, page=1): context = get_default_context(request) db = mydb.MyDB() title = db.SqlQueryScalar(db.sql('board_theme_title'), {'id': theme_id}) bread_crumbs = [{'text': consts.NAV_CAPTION, 'link': '/'}] bread_crumbs.append({'text': 'Форум', 'link': '/board/'}) bread_crumbs.append({'text': title, 'last': True}) context['BREAD_CRUMBS'] = json_dumps(bread_crumbs) context['THEME_ID'] = theme_id context['THEME_TITLE'] = title context['ADDITIONAL_PARAMS'] = ''' 'comments_page': {}, '''.format(page) return render(request, 'app/board/theme.html', context)
def get_subject(subject_id, subject_text): db = mydb.MyDB() if subject_id != 0 and subject_id != -1: return int(subject_id) if subject_text == '': return None rs = db.SqlQuery(db.sql('edu_files_subject_get'), {'subject': subject_text}) if rs: return int(rs[0]['id']) last_id = db.SqlQueryScalar(db.sql('edu_files_subject_insert'), {'subject': subject_text}) return last_id
def edu_files_sub_list(params): db = mydb.MyDB() rs = db.SqlQuery(db.sql('edu_files_subject_types_list'), {'subject': params['subject_id']}) d = [[r['id'], r['type']] for r in rs] s = [ { 'n': 'id', 't': 'Число целое' }, { 'n': 'type', 't': 'Строка' }, ] return get_records_set_json(s, d)