def save(): if not request.user.role('admin'): return False data = request.forms v = Validator(data) v.field("id").integer(nullable=True) v.field("title").required() v.field("text").required() v.field("datetime").datetime(format='%d.%m.%Y %H:%M') if v.is_valid(): data = v.valid_data news = request.db(News).get_by_id(data.get("id")) or News() news.title = data.get("title") news.text = data.get("text") news.datetime = data.get("datetime") request.db.add(news) request.db.commit() return {"status": "ok", "redirect": '/admin/news'} else: return {"status": "fail", "errors": v.errors}
def send(): data = request.forms v = Validator(data) v.field("name").required() v.field("email_phone").required() v.field("reason").integer() receivers = [c.cnt_value for c in request.db(Contacts).list_by_type('feedback')] if v.is_valid(): data = v.valid_data reason = translate('site.contacts.reason'+str(data.get("reason")), language=get_default_language().lng_code) text = """\ Контактное лицо: %s <br/> Email или телефон: %s <br/> Причина: %s <br/> <br/> %s """ % (data.get('name'), data.get('email_phone'), reason, data.get('text')) msg = MIMEText(text.encode('utf-8'), 'html') sender = data.get('email') receiver = ', '.join(receivers) msg['Subject'] = 'Письмо от пользователя %s' % data.get('name') msg['From'] = 'Обратная связь' msg['To'] = receiver s = smtplib.SMTP('localhost') s.sendmail(sender, receiver, msg.as_string()) s.quit() return {"status": "ok", "message": translate('site.feedback.message_sent', default="Повiдомлення вiдправленно.")} else: return {"status": "fail", "errors": translate('site.feedback.fill_all_fields', default="Заповнiть, будь ласка, усi поля.")}
def save(): if not request.user.role('admin'): return False data = request.forms v = Validator(data) v.field("id").required().integer() if v.is_valid(): data = v.valid_data dictionary = request.db(Dictionary).get_by_id(data.get("id")) dictionary.text = data.get("text") request.db.add(dictionary) request.db.flush() request.db.commit() update_translation(dictionary) return {"status": "ok", "dictionary": dictionary.as_dict(translated=True)} else: return {"status": "fail", "errors": v.errors}
def save_object(): if not request.user.role('admin'): return False data = request.forms photos = request.files.getall('file') v = Validator(data) v.fields("file.id").integer(nullable=True) v.fields("file.visible").boolean() v.fields("file.order").integer() if v.is_valid(): data = v.valid_data about = request.db(Content).get_by_alias('about') if not about: about = Content() about.con_alias = 'about' about.con_name = 'about' about.con_text = data.get("text") request.db.add(about) request.db.commit() action = data.getall('file.action') visible = data.getall('file.visible') order = data.getall('file.order') for i, id in enumerate(data.getall('file.id')): if action[i]: if action[i] == 'new': file = request.db(File).create('photo.jpg', module=module.name, entity=about) file.create_dir() file.update_uniq_id() photo = photos.pop(0) image_resize(photo).convert('RGB').save(file.get_fullpath()) else: file = request.db(File).get_by_id(id) file.visible = visible[i] file.order = order[i] if action[i] == 'delete': file.remove_files() request.db.delete(file) else: request.db.add(file) request.db.commit() return {"status": "ok", "reload": 'true'} else: return {"status": "fail", "errors": v.errors}
def login_native(): data = request.forms v = Validator(data) v.field("email").required() v.field("password").required() if v.is_valid(): data = v.valid_data user = request.db().query(User).filter(User.email == data.get('email')).first() if not user or not verify_password(data.get('password'), user.password): v.add_error('email', 'Неправильный email или пароль', 'wrong_login') else: auth(user) return {"status": "ok", "reload": True} return {"status": "fail", "errors": v.errors}
def seo_save(): if not request.user.role('admin'): return False data = request.forms v = Validator(data) v.field("seou_url").required() v.field("seou_id").integer(nullable=True) v.fields("seot_id").integer(nullable=True) seo = request.db(SEOUrl).get_by_url(v.valid_data.get("seou_url").strip()) if seo is not None and seo.seou_id != v.valid_data.seou_id: v.add_error("seou_url", "Такая ссылка уже существует") if v.is_valid(): seo = request.db(SEOUrl).get_by_id(data.seou_id) if seo is None: seo = SEOUrl() seo.seou_url = data.get('seou_url').strip() tag_ids = data.getall('seot_id') tag_text = data.getall('seot_text') request.db.add(seo) request.db.commit() for i, text in enumerate(tag_text): text = text.strip() id = tag_ids[i] tag = request.db(SEOUrl).get_by_id(id) if tag is None: tag = SEOTag() tag.seou_id = seo.seou_id if text == '': if id is not None: request.db.delete(tag) else: tag.seot_text = text request.db.add(tag) request.db.commit() return {"status": "ok"} else: return {"status": "fail", "errors": v.errors}
def save_object(): if not request.user.role("admin"): return False data = request.forms data["image"] = request.files.get("image") v = Validator(data) v.field("image").image() img = data.get("image") if v.is_valid() and img is not None: path = os.path.abspath(files_dir + "/upload/") image_name = "image." + str(uuid.uuid4()) + ".png" if not os.path.exists(path): os.makedirs(path) image_path = os.path.join(path, image_name) image = image_resize(img) image.save(image_path) return {"status": "ok", "url": "/file/upload/" + image_name, "width": image.size[0], "height": image.size[1]} else: return {"status": "fail", "errors": v.errors}
def save(): if not request.user.role('admin'): return False data = request.forms v = Validator(data) data['lng_code'] = data['lng_code'].upper() v.field("lng_id").integer(nullable=True) v.field("lng_code").required() v.field("lng_name").required() lang = request.db(Language).get_by_code(data.get("lng_code")) if lang is not None and lang.lng_id != data['lng_id']: v.add_error('lng_name', 'Язык с таким кодом уже существует', 'language.exists') if v.is_valid(): data = v.valid_data lang = request.db(Language).get_by_id(data.get("lng_id")) if lang is None: lang = Language() lang.lng_code = data.get("lng_code") lang.lng_name = data.get("lng_name") request.db.add(lang) request.db.commit() return {"status": "ok", "language": lang.as_dict(translated=True)} else: return {"status": "fail", "errors": v.errors}
def save_content(): if not request.user.role('admin'): return False data = request.forms v = Validator(data) v.field("text").required() v.field("id").integer(nullable=True).required() if v.is_valid(): data = v.valid_data content = request.db(Content).get_by_id(data.get("id")) content.text = data.get("text") request.db.add(content) request.db.commit() return {"status": "ok", "redirect": '/admin/content'} else: return {"status": "fail", "errors": v.errors}
def registrate(): data = request.forms data['photo'] = request.files.get('photo') v = Validator(data) v.field("first_name").required() v.field("last_name").required() v.field("email").required().email() v.field("password").required().length(min=5, message="Длина пароля не менее %(min)d символов") v.field("photo").image() if data.get("password") != data.get("repassword"): v.add_error('password', 'Пароли не совпадают', 'wrong_repassword') if(v.is_valid()): data = v.valid_data password_hash = hash_password(data.get('password')) user = request.db(User).get_by_email(data['email']) if user: v.add_error('email', 'Электронный адрес уже используется.', 'email_is_used_already') else: user = User() user.email = data['email'] user.first_name = data['first_name'] user.last_name = data['last_name'] user.password = password_hash request.db.add(user) request.db.commit() img = data.get("photo") if img is not None: path = profile_path.format(id=user.id) photo_name = 'photo_'+str(user.id)+"_"+str(uuid.uuid4())+".png" thumbnail_name = photo_name.rstrip(".png")+".thumbnail.png" if not os.path.exists(path): os.makedirs(path) remove_similar(path, photo_name) image_path = os.path.join(path, photo_name) thumbnail_path = os.path.join(path, thumbnail_name) photo = image_thumbnail(img, width=200, height=200, fill='cover') photo.save(image_path) img.file.seek(0) thumbnail = image_thumbnail(img, width=50, height=50, fill='cover') thumbnail.save(thumbnail_path) user.photo = "/file"+image_path.replace(files_dir, '').replace("\\", '/') user.photo_s = "/file"+thumbnail_path.replace(files_dir, '').replace("\\", '/') request.db.commit() auth(user) return {"status": "ok", "reload": True} return {"status": "fail", "errors": v.errors}