Skip to content

Commit

Permalink
Impr: Move account config to individual page (TFcis#69)
Browse files Browse the repository at this point in the history
* Impr: Move account config to individual page

* Refactor(handlers/acct.py): Use a more concise approach for counting AC count

---------

Co-authored-by: LifeAdventurer <life0adventurer@gmail.com>
  • Loading branch information
tobiichi3227 and LifeAdventurer authored May 25, 2024
1 parent 642c011 commit 6d5147d
Showing 4 changed files with 202 additions and 115 deletions.
23 changes: 19 additions & 4 deletions src/handlers/acct.py
Original file line number Diff line number Diff line change
@@ -47,13 +47,26 @@ async def get(self, acct_id):

isadmin = self.acct.is_kernel()
rate_data['rate'] = math.floor(rate_data['rate'])
rate_data['ac_pro_cnt'] = sum(t.get('rate') == 100 for t in ratemap.values())

# force https, add by xiplus, 2018/8/24
acct.photo = re.sub(r'^http://', 'https://', acct.photo)
acct.cover = re.sub(r'^http://', 'https://', acct.cover)

await self.render('acct', acct=acct, rate=rate_data, prolist=prolist2, isadmin=isadmin)


class AcctConfigHandler(RequestHandler):
@reqenv
async def get(self, acct_id):
acct_id = int(acct_id)
err, acct = await UserService.inst.info_acct(acct_id)
if err:
self.error(err)
return

await self.render('acct-config', acct=acct, isadmin=self.acct.is_kernel())

@reqenv
@require_permission([UserConst.ACCTTYPE_USER, UserConst.ACCTTYPE_KERNEL])
async def post(self):
@@ -63,10 +76,13 @@ async def post(self):
name = self.get_argument('name')
photo = self.get_argument('photo')
cover = self.get_argument('cover')
acct_id = self.get_argument('acct_id')

err, _ = await UserService.inst.update_acct(
self.acct.acct_id, self.acct.acct_type, self.acct.acct_class, name, photo, cover
)
if acct_id != str(self.acct.acct_id):
self.error('Eacces')
return

err, _ = await UserService.inst.update_acct(self.acct.acct_id, self.acct.acct_type, self.acct.acct_class, name, photo, cover)
if err:
self.error(err)
return
@@ -93,7 +109,6 @@ async def post(self):

self.error('Eunk')


class SignHandler(RequestHandler):
@reqenv
async def get(self):
171 changes: 171 additions & 0 deletions src/static/templ/acct-config.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{{ htmlgen.set_page_title("Edit Account") }}

{% if acct.acct_id != acct_id and not isadmin %}
You don't have permission.
{% else %}
<script id="contjs" type="text/javascript">
function init() {
let j_profile = $('#profile');
let j_reset = $('#reset');
let acct_id = "{{ acct.acct_id }}";

j_profile.find('button.submit').on('click', function(e) {
let name = j_profile.find('input.name').val();
let photo = j_profile.find('input.photo').val();
let cover = j_profile.find('input.cover').val();

try {
new URL(photo);
} catch {
if (photo != "") {
alert("Photo url is invalid");
return;
}
}

try {
new URL(cover);
} catch {
if (cover != "") {
alert("Cover url is invalid");
return;
}
}

$.post('/oj/be/acctedit', {
'reqtype': 'profile',
'acct_id': acct_id,
'name': name,
'photo': photo,
'cover': cover
}, function(res) {
let msg = 'Unknown';

if(res[0] == 'E') {
if(res == 'Enamemin') {
msg = 'Name length < min';
} else if (res == 'Enamemax') {
msg = 'Name length > max';
}

j_profile.find('div.print').print(res);

} else {
j_profile.find('div.print').print('Success', true);
index.reload();
}
});

});

j_reset.find('button.submit').on('click', function(e) {
let old = j_reset.find('input.old').val();
let pw = j_reset.find('input.pw').val();
let repeat = j_reset.find('input.repeat').val();

if (pw != repeat) {
j_reset.find('div.print').print('Repeat incorrect');
return;
}

$.post('/oj/be/acctedit', {
'reqtype': 'reset',
'acct_id': acct_id,
'old': old,
'pw': pw,
}, function(res) {
let msg = 'Unknown';

if (res[0] == 'E') {
if (res == 'Epwmin') {
msg = 'Password length < min';
} else if (res == 'Epwmax') {
msg = 'Password length > max';
} else if (res == 'Epwold') {
msg = 'Previous incorrect';
}

j_reset.find('div.print').print(msg);
} else {
j_reset.find('div.print').print('Success', true);
j_reset.find('input').val('');
}
});
});
}
</script>

<div class="row">
<div class="col-6">
<span>
<h1>Edit User <a href="/oj/acct/{{ acct.acct_id }}/">{{ acct.name }}</a></h1>
</span>

{% if acct.acct_id == acct_id %}
<form id="profile">
<div class="mb-1">
<label class="form-label">Name</label>
<input class="name form-control" type="text" value="{{ acct.name }}" placeholder="name">
</div>

<div class="mb-1">
<label class="form-label">Email(WIP)</label>
<input class="name form-control" type="text" value="WIP (Email)" placeholder="email" disabled>
</div>

<div class="mb-1">
<label class="form-label">Motto(WIP)</label>
<input class="name form-control" type="text" value="WIP (Motto)" placeholder="motto" disabled>
</div>

<div class="mb-1">
<label class="form-label">Photo</label>
<input class="photo form-control" type="text" value="{{ acct.photo }}" placeholder="photo">
</div>

<div class="mb-1">
<label class="form-label">Cover</label>
<input class="cover form-control" type="text" value="{{ acct.cover }}" placeholder="cover">
</div>

<div class="mb-1">
<label class="form-label">Theme (WIP)</label>
<select class="form-select" disabled>
<option value="dark">Dark Theme</option>
<option value="light">Light Theme</option>
<option value="classic">TOJ Classic Theme</option>
</select>
</div>

<button type="button" class="btn btn-primary submit">更新</button>
<div class="print"></div>
</form>
{% end %}

{% if acct.acct_id == acct_id or isadmin %}
<form id="reset">
<h3>更改密碼</h3>

<div class="mb-1">
<label class="form-label">Current Password</label>
<input class="old form-control" type="password">
</div>

<div class="mb-1">
<label class="form-label">New Password</label>
<input class="pw form-control" type="password">
</div>

<div class="mb-1">
<label class="form-label">New Password Confirmation</label>
<input class="repeat form-control" type="password">
</div>

<button type="button" class="btn btn-warning submit">確認</button>
<div class="print"></div>
</form>
{% end %}
</div>
</div>

{% end %}
118 changes: 9 additions & 109 deletions src/static/templ/acct.html
Original file line number Diff line number Diff line change
@@ -55,77 +55,8 @@

<script id="contjs" type="text/javascript" photo="{{ acct.photo }}" cover="{{ acct.cover }}">
function init() {
var photo = $('#contjs').attr('photo');
var cover = $('#contjs').attr('cover');
var j_profile = $('#profile');
var j_reset = $('#reset');

j_profile.find('button.submit').on('click', function(e) {
var name = j_profile.find('input.name').val();
var photo = j_profile.find('input.photo').val();
var cover = j_profile.find('input.cover').val();

$.post('/oj/be/acct', {
'reqtype': 'profile',
'acct_id': {{ acct.acct_id }},
'name': name,
'photo': photo,
'cover': cover
}, function(res) {
var msg = 'Unknown';

if(res[0] == 'E') {
if(res == 'Enamemin') {
msg = 'Name length < min';
} else if (res == 'Enamemax') {
msg = 'Name length > max';
}

// j_profile.find('div.print').print(msg);
j_profile.find('div.print').print(res);

} else {
j_profile.find('div.print').print('Success', true);
index.reload();
}
});

});

j_reset.find('button.submit').on('click', function(e) {
var old = j_reset.find('input.old').val();
var pw = j_reset.find('input.pw').val();
var repeat = j_reset.find('input.repeat').val();

if (pw != repeat) {
j_reset.find('div.print').print('Repeat incorrect');
return;
}

$.post('/oj/be/acct', {
'reqtype': 'reset',
'acct_id': {{ acct.acct_id }},
'old': old,
'pw': pw,
}, function(res) {
var msg = 'Unknown';

if(res[0] == 'E') {
if (res == 'Epwmin') {
msg = 'Password length < min';
} else if (res == 'Epwmax') {
msg = 'Password length > max';
} else if (res == 'Epwold') {
msg = 'Previous incorrect';
}

j_reset.find('div.print').print(msg);
} else {
j_reset.find('div.print').print('Success', true);
j_reset.find('input').val('');
}
});
});
let photo = $('#contjs').attr('photo');
let cover = $('#contjs').attr('cover');

if (photo == '') {
photo = 'https://www.gravatar.com/avatar/{{ acct.acct_id }}?d=identicon&f=y&s=480;'
@@ -161,6 +92,10 @@ <h3>基本情報</h3>
<td>Score</td>
<td style="text-align: right;">{{ rate['rate'] }}</td>
</tr>
<tr>
<td>Accepted</td>
<td style="text-align: right;">{{ rate['ac_pro_cnt'] }}</td>
</tr>
<tr>
<td>AC Ratio</td>
<td style="text-align: right;">
@@ -178,47 +113,12 @@ <h3>基本情報</h3>
</tr>
</tbody>
</table>

{% if acct.acct_id == acct_id %}
<div class="mb-1">
<label class="form-label">Name</label>
<input class="name form-control" type="text" value="{{ acct.name }}" placeholder="name">
</div>

<div class="mb-1">
<label class="form-label">Photo</label>
<input class="photo form-control" type="text" value="{{ acct.photo }}" placeholder="photo">
</div>

<div class="mb-1">
<label class="form-label">Cover</label>
<input class="cover form-control" type="text" value="{{ acct.cover }}" placeholder="cover">
</div>

<button type="button" class="btn btn-primary submit">更新</button>
<div class="print"></div>
<a class="btn btn-primary" href="/oj/acctedit/{{ acct_id }}/">Edit My Account</a>
{% elif isadmin %}
<a class="btn btn-primary" href="/oj/acctedit/{{ acct_id }}/">Edit Account Password</a>
{% end %}
</form>
{% if (acct.acct_id == acct_id) or (isadmin) %}
<form id="reset">
<h3>更改密碼</h3>

<div class="mb-1">
<input class="old form-control" type="password" placeholder="previous">
</div>

<div class="mb-1">
<input class="pw form-control" type="password" placeholder="password">
</div>

<div class="mb-1">
<input class="repeat form-control" type="password" placeholder="repeat">
</div>

<button type="button" class="btn btn-warning submit">確認</button>
<div class="print"></div>
</form>
{% end %}
</div>
<div class="col-lg-9">
<table class="table" style="font-size: 130%;" border="1px" bordercolor="gray">
5 changes: 3 additions & 2 deletions src/url.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# from auto import AutoHandler
from handlers.acct import AcctHandler, SignHandler
from handlers.acct import AcctHandler, AcctConfigHandler, SignHandler
from handlers.api import ApiHandler
from handlers.board import BoardHandler
from handlers.bulletin import BulletinHandler, BulletinSub
@@ -43,7 +43,8 @@ def get_url(db, rs):
(r'/board/(\d+)', BoardHandler, args),
(r'/sign', SignHandler, args),
(r'/acct/(\d+)', AcctHandler, args),
(r'/acct', AcctHandler, args),
(r'/acctedit', AcctConfigHandler, args),
(r'/acctedit/(\d+)', AcctConfigHandler, args),
(r'/proset', ProsetHandler, args),
(r'/pro/(\d+)/(.+)', ProStaticHandler, args),
(r'/pro/(\d+)', ProHandler, args),

0 comments on commit 6d5147d

Please sign in to comment.