Skip to content

Commit

Permalink
porting to python3
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhigh committed Feb 16, 2024
1 parent 5a499bf commit e78bbd3
Show file tree
Hide file tree
Showing 26 changed files with 685 additions and 332 deletions.
22 changes: 16 additions & 6 deletions application/back_end/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ class KeUser(MyBaseModel): # kindleEar User
expiration_days = IntegerField(default=0) #账号超期设置值,0为永久有效
secret_key = CharField(default='')
kindle_email = CharField(default='')
email = CharField(default='') #可能以后用于重置密码之类的操作
email = CharField(default='') #用于重置密码之类的操作
enable_send = BooleanField(default=False)
send_days = JSONField(default=JSONField.list_default)
send_time = IntegerField()
send_time = IntegerField(default=0)
timezone = IntegerField(default=0)
book_type = CharField(default='epub') #mobi,epub
device = CharField(default='')
expires = DateTimeField(null=True) #超过了此日期后账号自动停止推送

book_title = CharField()
book_title = CharField(default='KindleEar')
title_fmt = CharField(default='') #在元数据标题中添加日期的格式
author_format = CharField(default='') #修正Kindle 5.9.x固件的bug【将作者显示为日期】
book_mode = CharField(default='') #书籍模式,'periodical'|'comic',漫画模式可以直接全屏
remove_hyperlinks = CharField(default='') #去掉文本或图片上的超链接{'' | 'image' | 'text' | 'all'}
time_fmt = CharField(default='%Y-%m-%d')
oldest_article = IntegerField(default=7)
book_language = CharField() #自定义RSS的语言
book_language = CharField(default='en') #自定义RSS的语言
enable_custom_rss = BooleanField(default=True)

share_links = JSONField(default=JSONField.dict_default) #evernote/wiz/pocket/instapaper包含子字典,微博/facebook/twitter等仅包含0/1
Expand Down Expand Up @@ -98,6 +98,16 @@ def get_cover_data(self):
def get_extra_css(self):
dbItem = UserBlob.get_or_none((UserBlob.user == self.name) & (UserBlob.name == 'css'))
return dbItem.data.decode('utf-8') if dbItem else ''

#根据设置,获取发送邮件的配置数据
def get_send_mail_service(self):
srv = self.send_mail_service
adminName = os.getenv('ADMIN_NAME')
if self.name == adminName or srv.get('service') != 'admin':
return srv
else:
dbItem = KeUser.get_or_none(KeUser.name == adminName)
return dbItem.send_mail_service if dbItem else {}

#用户的一些二进制内容,比如封面之类的
class UserBlob(MyBaseModel):
Expand Down Expand Up @@ -199,8 +209,8 @@ def categories(self):

#Buffer for category of shared rss [for kindleear.appspot.com only]
class SharedRssCategory(MyBaseModel):
name = CharField()
last_updated = DateTimeField(index=True) #for sort
name = CharField(unique=True)
last_updated = DateTimeField()

class LastDelivered(MyBaseModel):
user = CharField()
Expand Down
9 changes: 5 additions & 4 deletions application/back_end/send_mail_adpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os, datetime, zipfile
from ..utils import local_time, ke_decrypt
from ..base_handler import save_delivery_log
from .db_models import KeUser

try:
from google.appengine.api import mail as gae_mail
Expand Down Expand Up @@ -57,15 +58,13 @@ def send_to_kindle(user, title, attachment, fileWithTime=True):
def send_mail(user, to, subject, body, attachments=None, html=None):
if not isinstance(to, list) and (',' in to):
to = to.split(',')
sm_service = user.send_mail_service
sm_service = user.get_send_mail_service()
srv_type = sm_service.get('service', 'gae')
data = {'sender': os.getenv('SRC_EMAIL'), 'to': to, 'subject': subject, 'body': body}
if attachments:
data['attachments'] = attachments
if html:
if isinstance(html, str):
html = html.encode("utf-8")
data['html'] = html
data['html'] = html.encode("utf-8") if isinstance(html, str) else html

default_log.info(f'Sending email using service : {srv_type}')
if srv_type == 'gae':
Expand All @@ -91,6 +90,7 @@ def send_mail(user, to, subject, body, attachments=None, html=None):
#html: 邮件正文的HTML内容
#attachments: 附件文件名和二进制内容,[(fileName, content),...]
#body: 可选的额外文本内容
#返回'ok'表示成功,否则返回错误描述字符串
def send_html_mail(user, to, subject, html, attachments=None, body=None):
if not body or not isinstance(body, str):
body = "Deliver from KindlerEar, refers to html part."
Expand All @@ -103,6 +103,7 @@ def send_html_mail(user, to, subject, html, attachments=None, body=None):

size = len(html or body) + sum([len(c) for f, c in (attachments or [])])
save_delivery_log(user, subject, size, status=status, to=to)
return status

#SendGrid发送邮件
#sender:: 发送者地址
Expand Down
2 changes: 1 addition & 1 deletion application/lib/urlopener.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, host=None, timeout=30, headers=None, **kwargs):
self.timeout = timeout
self.addheaders = [("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")]
if headers:
for key,value in headers:
for key, value in (headers.items() if isinstance(headers, dict) else headers):
self.addheaders.append((key, value))
if 'user_agent' in kwargs:
self.addheaders.append(('User-Agent', kwargs.get('user_agent')))
Expand Down
19 changes: 19 additions & 0 deletions application/static/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,25 @@ table.pure-table thead {
border: none;
}

/* logs html */
.logs .status {
display: inline-block;
border-radius: 20px;
background-color: grey;
color: #fff;
text-align: center;
padding: 3px 15px;
white-space: nowrap;
}

.logs .status.success {
background-color: green;
}

.logs .status.error {
background-color: darkorange;
}

.width1_3 {
width: calc(100% / 3);
}
Expand Down
7 changes: 5 additions & 2 deletions application/static/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,8 @@ function AddAccount(name) {
var newName = $('#new_username').val();
var newPwd1 = $('#new_u_pwd1').val();
var newPwd2 = $('#new_u_pwd2').val();
var newEmail = $('#new_email').val();
var smService = $('#sm_service').val(); //send mail service
var expiration = $('#new_u_expiration').val();
if (!newName || !newPwd1 || !newPwd2) {
alert(i18n.namePwdEmpty);
Expand All @@ -983,13 +985,14 @@ function AddAccount(name) {
}

$.post("/admin", {actType: 'add', new_username: newName, new_u_pwd1: newPwd1, new_u_pwd2: newPwd2,
new_u_expiration: expiration}, function (data) {
new_u_expiration: expiration, new_email: newEmail, sm_service: smService}, function (data) {
if (data.status == "ok") {
$('#new_username').val('');
$('#new_u_pwd1').val('');
$('#new_u_pwd2').val('');
ShowSimpleModalDialog('<p>' + i18n.addAccountOk + '</p>');
$('#new_email').val('');
window.location.reload(true);
ShowSimpleModalDialog('<p>' + i18n.addAccountOk + '</p>');
} else if (data.status == i18n.loginRequired) {
window.location.href = '/login';
} else {
Expand Down
91 changes: 51 additions & 40 deletions application/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,66 +35,77 @@

{% if session.get('role') == 'admin' -%}
<form class="pure-form pure-form-aligned" action="" method="POST" style="margin-top:10px;" onsubmit="return false;">
<fieldset>
<legend><h3>{{_("Add Account")}}</h3></legend>
{% if actips -%}
<div class="notice-box">{{actips|safe}}</div>
{% endif -%}
<div class="pure-control-group">
<label>{{_("Username")}}</label>
<input type="text" name="new_username" id="new_username" class="pure-u-1 pure-u-sm-1-2" />
</div>
<div class="pure-control-group">
<label for="new_u_pwd1">{{_("Password")}}</label>
<input type="text" name="new_u_pwd1" id="new_u_pwd1" class="pure-u-1 pure-u-sm-1-2" />
</div>
<div class="pure-control-group">
<label for="new_u_pwd2">{{_("Confirm password")}}</label>
<input type="text" name="new_u_pwd2" id="new_u_pwd2" class="pure-u-1 pure-u-sm-1-2" />
</div>
<div class="pure-control-group">
<label for="new_u_expiration">{{_("Expiration")}}</label>
<select class="pure-u-1 pure-u-sm-1-2 bool" name="new_u_expiration" id="new_u_expiration">
<option value="0" selected="selected">{{_("Never expire")}}</option>
<option value="7">{{_("7 Days")}}</option>
<option value="30">{{_("1 Month")}}</option>
<option value="90">{{_("3 Months")}}</option>
<option value="180">{{_("6 Months")}}</option>
<option value="365">{{_("1 Year")}}</option>
<option value="730">{{_("2 Years")}}</option>
</select>
</div>
<div style="text-align:center;">
<button onclick="AddAccount('{{user.name}}')" class="pure-button pure-button-primary pure-input-rounded">{{_("Add")}}</button>
</div>
</fieldset>
<fieldset>
<legend><h3>{{_("Add Account")}}</h3></legend>
{% if actips -%}
<div class="notice-box">{{actips|safe}}</div>
{% endif -%}
<div class="pure-control-group">
<label for="new_username">{{_("Username")}}</label>
<input type="text" name="new_username" id="new_username" class="pure-u-1 pure-u-sm-1-2" />
</div>
<div class="pure-control-group">
<label for="new_u_pwd1">{{_("Password")}}</label>
<input type="text" name="new_u_pwd1" id="new_u_pwd1" class="pure-u-1 pure-u-sm-1-2" />
</div>
<div class="pure-control-group">
<label for="new_u_pwd2">{{_("Confirm password")}}</label>
<input type="text" name="new_u_pwd2" id="new_u_pwd2" class="pure-u-1 pure-u-sm-1-2" />
</div>
<div class="pure-control-group">
<label for="new_email">{{_("Email")}}</label>
<input type="email" name="new_email" id="new_email" class="pure-u-1 pure-u-sm-1-2" placeholder="{{_('Optional')}}" />
</div>
<div class="pure-control-group">
<label for="sm_service">{{_("Email service")}}</label>
<select class="pure-u-1 pure-u-sm-1-2 bool" name="sm_service" id="sm_service">
<option value="admin" selected="selected">{{_("Same as admin")}}</option>
<option value="independent">{{_("Independent")}}</option>
</select>
</div>
<div class="pure-control-group">
<label for="new_u_expiration">{{_("Expiration")}}</label>
<select class="pure-u-1 pure-u-sm-1-2 bool" name="new_u_expiration" id="new_u_expiration">
<option value="0" selected="selected">{{_("Never expire")}}</option>
<option value="7">{{_("7 Days")}}</option>
<option value="30">{{_("1 Month")}}</option>
<option value="90">{{_("3 Months")}}</option>
<option value="180">{{_("6 Months")}}</option>
<option value="365">{{_("1 Year")}}</option>
<option value="730">{{_("2 Years")}}</option>
</select>
</div>
<div style="text-align:center;">
<button onclick="AddAccount('{{user.name}}')" class="pure-button pure-button-primary pure-input-rounded">{{_("Add")}}</button>
</div>
</fieldset>
</form>

<div class="pure-form">
<legend><h3>{{_("Accounts")}}</h3></legend>
<table class="pure-table pure-table-horizontal usrtb" width="100%">
<colgroup>
<col width="12%">
<col width="22%">
<col width="22%">
<col width="22%">
<col width="22%">
<col width="15%">
<col width="15%">
<col width="30%">
<col width="20%">
<col width="20%">
</colgroup>
<thead>
<tr>
<th>{{_("No.")}}</th>
<th>{{_("Username")}}</th>
<th>{{_("AutoSend")}}</th>
<th>{{_("Email")}}</th>
<th>{{_("Expiration")}}</th>
<th>{{_("Operation")}}</th>
</tr>
</thead>
<tbody>
{% for u in users -%}
<tr>
<td>{{loop.index}}</td>
<td>{{u.name}}</td>
<td>{{_("Yes") if u.enable_send else _("No")}}</td>
<td>{{u.email}}</td>
{% if u.expiration_days == 0 -%}
<td>{{_("Never expire")}}</td>
{% elif u.expiration_days == 7 -%}
Expand Down
42 changes: 21 additions & 21 deletions application/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
{% block css -%}
<style type="text/css">
#note{
color:grey;
font-size:0.8em;
text-align:center;
color:grey;
font-size:0.8em;
text-align:center;
}
</style>
{% endblock -%}
Expand All @@ -20,23 +20,23 @@
{% endblock -%}

{% block content -%}
<div class="main" id="fs_login">
{% if tips -%}
<div class="notice-box">{{tips|safe}}</div>
{% endif -%}
<form action="/login" method="POST" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="u">{{_("Username")}}</label>
<input id="u" name="u" type="text" placeholder="{{ _('Username') }}" {% if userName %}value="{{ userName }}"{% endif %} class="pure-u-1 pure-u-sm-1-2" autofocus />
</div>
<div class="pure-control-group">
<label for="p">{{_("Password")}}</label>
<input id="p" name="p" type="password" placeholder="{{_('Password')}}" class="pure-u-1 pure-u-sm-1-2" />
</div>
<div class="pure-control-group" style="text-align:center;">
<button type="submit" class="pure-button pure-button-primary pure-input-rounded">{{_("Login")}}</button>
</div>
</form>
<p id="note">{{_("The website doesn't allow registration. You can ask the owner for an account.")}}</p>
<div class="main">
{% if tips -%}
<div class="notice-box">{{tips|safe}}</div>
{% endif -%}
<form action="/login" method="POST" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="u">{{_("Username")}}</label>
<input id="u" name="u" type="text" placeholder="{{ _('Username') }}" {% if userName %}value="{{ userName }}"{% endif %} class="pure-u-1 pure-u-sm-1-2" autofocus />
</div>
<div class="pure-control-group">
<label for="p">{{_("Password")}}</label>
<input id="p" name="p" type="password" placeholder="{{_('Password')}}" class="pure-u-1 pure-u-sm-1-2" />
</div>
<div class="pure-control-group" style="text-align:center;">
<button type="submit" class="pure-button pure-button-primary pure-input-rounded">{{_("Login")}}</button>
</div>
</form>
<p id="note">{{_("The website doesn't allow registration. You can ask the owner for an account.")}}</p>
</div>
{% endblock -%}
Loading

0 comments on commit e78bbd3

Please sign in to comment.