Skip to content

Commit

Permalink
feat: added suggestions on circles and changed error msg gh issue bit…
Browse files Browse the repository at this point in the history
  • Loading branch information
clehre committed Jul 3, 2024
1 parent fc2d29e commit 4892a53
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 57 deletions.
10 changes: 8 additions & 2 deletions web/src/p2k16/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,21 @@ def find_account_by_id(_id) -> Optional['Account']:

@staticmethod
def get_by_id(_id) -> "Account":
return Account.query.filter(Account.id == _id).one()
account = Account.query.filter(Account.id == _id).one_or_none()
if not account:
raise P2k16UserException("User not found")
return account

@staticmethod
def find_account_by_username(username) -> Optional['Account']:
return Account.query.filter(func.lower(Account.username) == func.lower(username)).one_or_none()

@staticmethod
def get_by_username(username) -> "Account":
return Account.query.filter(func.lower(Account.username) == func.lower(username)).one()
account = Account.query.filter(func.lower(Account.username) == func.lower(username)).one_or_none()
if not account:
raise P2k16UserException(f"User: {username} was not found")
return account

@staticmethod
def find_account_by_email(email) -> Optional['Account']:
Expand Down
110 changes: 57 additions & 53 deletions web/src/p2k16/web/static/admin-circle-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h1>{{ ctrl.circleName }}
<label for="name" class="col-sm-3 control-label">Name</label>
<div class="col-sm-4">
<input class="form-control" type="text" name="name" id="name" placeholder="name"
ng-model="ctrl.addCircleForm.name"/>
ng-model="ctrl.addCircleForm.name" />
</div>
</div>
<div class="form-group">
Expand All @@ -25,7 +25,7 @@ <h1>{{ ctrl.circleName }}
<div class="checkbox">
<label for="comment-required-for-membership">
<input type="checkbox" id="comment-required-for-membership"
ng-model="ctrl.addCircleForm.commentRequiredForMembership">
ng-model="ctrl.addCircleForm.commentRequiredForMembership">
Comment required for memberships
</label>
<span class="help-block">
Expand All @@ -40,12 +40,12 @@ <h1>{{ ctrl.circleName }}
<div class="col-sm-4">
<label class="radio-inline">
<input type="radio" name="management-style" id="ADMIN_CIRCLE" value="ADMIN_CIRCLE"
ng-model="ctrl.addCircleForm.managementStyle">
ng-model="ctrl.addCircleForm.managementStyle">
Admin circle
</label>
<label class="radio-inline">
<input type="radio" name="management-style" id="SELF_ADMIN" value="SELF_ADMIN"
ng-model="ctrl.addCircleForm.managementStyle" ng-click="ctrl.selfAdminSelected()">
ng-model="ctrl.addCircleForm.managementStyle" ng-click="ctrl.selfAdminSelected()">
Self admin
</label>
</div>
Expand All @@ -54,8 +54,8 @@ <h1>{{ ctrl.circleName }}
<label for="admin-circle" class="col-sm-3 control-label">Admin circle</label>
<div class="col-sm-4">
<input class="form-control" type="text" id="admin-circle" placeholder="Admin circle"
ng-model="ctrl.addCircleForm.adminCircle"
ng-disabled="ctrl.addCircleForm.managementStyle != 'ADMIN_CIRCLE'"/>
ng-model="ctrl.addCircleForm.adminCircle"
ng-disabled="ctrl.addCircleForm.managementStyle != 'ADMIN_CIRCLE'" />
<span class="help-block">
Members in this circle will control the created circle.
</span>
Expand All @@ -65,7 +65,7 @@ <h1>{{ ctrl.circleName }}
<label for="username" class="col-sm-3 control-label">Initial member</label>
<div class="col-sm-4">
<input class="form-control" type="text" id="username" placeholder="Username"
ng-model="ctrl.addCircleForm.username" ng-disabled="ctrl.addCircleForm.managementStyle != 'SELF_ADMIN'"/>
ng-model="ctrl.addCircleForm.username" ng-disabled="ctrl.addCircleForm.managementStyle != 'SELF_ADMIN'" />
<span class="help-block">
This member will be the one that controls the circle.
</span>
Expand All @@ -74,9 +74,8 @@ <h1>{{ ctrl.circleName }}
<div class="form-group">
<label for="comment" class="col-sm-3 control-label">Membership comment</label>
<div class="col-sm-4">
<input class="form-control" type="text" id="comment" placeholder="Comment"
ng-model="ctrl.addCircleForm.comment"
ng-disabled="ctrl.addCircleForm.managementStyle != 'SELF_ADMIN' || !ctrl.addCircleForm.commentRequiredForMembership"/>
<input class="form-control" type="text" id="comment" placeholder="Comment" ng-model="ctrl.addCircleForm.comment"
ng-disabled="ctrl.addCircleForm.managementStyle != 'SELF_ADMIN' || !ctrl.addCircleForm.commentRequiredForMembership" />
</div>
</div>
<div class="form-group">
Expand All @@ -88,26 +87,26 @@ <h1>{{ ctrl.circleName }}

<table class="table" ng-if="ctrl.circle.id">
<tbody>
<tr>
<th class="col-sm-4">Comment required</th>
<td class="col-sm-8">{{ ctrl.circle.commentRequiredForMembership | yesno }}</td>
</tr>
<tr>
<th class="col-sm-4">Management style</th>
<td class="col-sm-8">{{ ctrl.circle.managementStyle }}</td>
</tr>
<tr>
<th class="col-sm-4">Can I manage this circle?</th>
<td class="col-sm-8">{{ p2k16.canAdminCircle(ctrl.circle.id) | yesno }}</td>
</tr>
<tr ng-show="ctrl.circle.managementStyle == 'ADMIN_CIRCLE'">
<th>Admin circle</th>
<td><a ng-href="#!admin/circle/{{ ctrl.adminCircle.id }}">{{ ctrl.adminCircle.name }}</a></td>
</tr>
<tr>
<th>Description</th>
<td>{{ ctrl.circle.description }}</td>
</tr>
<tr>
<th class="col-sm-4">Comment required</th>
<td class="col-sm-8">{{ ctrl.circle.commentRequiredForMembership | yesno }}</td>
</tr>
<tr>
<th class="col-sm-4">Management style</th>
<td class="col-sm-8">{{ ctrl.circle.managementStyle }}</td>
</tr>
<tr>
<th class="col-sm-4">Can I manage this circle?</th>
<td class="col-sm-8">{{ p2k16.canAdminCircle(ctrl.circle.id) | yesno }}</td>
</tr>
<tr ng-show="ctrl.circle.managementStyle == 'ADMIN_CIRCLE'">
<th>Admin circle</th>
<td><a ng-href="#!admin/circle/{{ ctrl.adminCircle.id }}">{{ ctrl.adminCircle.name }}</a></td>
</tr>
<tr>
<th>Description</th>
<td>{{ ctrl.circle.description }}</td>
</tr>
</tbody>
</table>

Expand All @@ -122,18 +121,24 @@ <h2>Add member</h2>
<label for="username" class="col-sm-3">Username</label>
<div class="col-sm-4">
<input class="form-control" type="text" name="email" id="username" placeholder="username"
autocomplete="username" autocapitalize="off"
ng-model="ctrl.addMemberForm.username"/>
list="suggestions-list" ng-model="ctrl.addMemberForm.username" autocomplete="off" />
<datalist id="suggestions-list">
<option ng-repeat="suggestion in ctrl.userNames" value="{{suggestion}}"></option>
</datalist>
</div>
</div>

<div class="form-group">
<label for="comment" class="col-sm-3">Comment</label>
<div class="col-sm-4">
<textarea class="form-control" rows="3" name="comment" id="comment" placeholder="comment"
ng-model="ctrl.addMemberForm.comment"></textarea>
<p ng-if="ctrl.circle.commentRequiredForMembership">This circle requires a comment on memberships.</p>
ng-model="ctrl.addMemberForm.comment"></textarea>
<p ng-if="ctrl.circle.commentRequiredForMembership">
This circle requires a comment on memberships.
</p>
</div>
</div>

<div class="form-group">
<div class="col-sm-4 col-sm-offset-2">
<button class="btn btn-default" ng-click="ctrl.addMember()">Add</button>
Expand All @@ -148,26 +153,25 @@ <h2>Members
</h2>
<table class="table">
<thead>
<tr>
<th class="col-sm-2">User</th>
<th class="col-sm-3">Created</th>
<th class="col-sm-3">Updated</th>
<th></th>
</tr>
<tr>
<th class="col-sm-2">User</th>
<th class="col-sm-3">Created</th>
<th class="col-sm-3">Updated</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat-start="m in ctrl.members | orderBy:'account_username'">
<td><a href="#!/admin/account/{{ m.account_id }}">{{ m.account_username }}</a></td>
<td>{{ m.createdAt | date:'medium' }}</td>
<td>{{ m.updatedAt | date:'medium' }}</td>
<td>
<a ng-click="ctrl.removeMember(m.account_id)">remove</a>
</td>
</tr>
<tr ng-repeat-end ng-if="m.comment">
<td></td>
<td colspan="3">{{ m.comment }}</td>
</tr>
<tr ng-repeat-start="m in ctrl.members | orderBy:'account_username'">
<td><a href="#!/admin/account/{{ m.account_id }}">{{ m.account_username }}</a></td>
<td>{{ m.createdAt | date:'medium' }}</td>
<td>{{ m.updatedAt | date:'medium' }}</td>
<td>
<a ng-click="ctrl.removeMember(m.account_id)" style="cursor: pointer;">remove</a>
</td>
<tr ng-repeat-end ng-if="m.comment">
<td style="color:lightgray">{{m.account_username}}</td>
<td colspan="3">{{ m.comment }}</td>
</tr>
</tbody>
<tfoot>
</tfoot>
Expand All @@ -190,4 +194,4 @@ <h3 class="modal-title" id="modal-title">Remove circle</h3>
No, wait I want to <a ng-click="ctrl.cancel()">ABORT!!11</a>.
</p>
</div>
</script>
</script>
41 changes: 39 additions & 2 deletions web/src/p2k16/web/static/p2k16/p2k16.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,14 +965,19 @@
$uibModal,
CoreDataService,
circles,
circle
circle,
$timeout
) {
const self = this;
self.isNew = !circle.id;
self.circleName = circle.id ? circle.name : "New circle";
self.addCircleForm = { commentRequiredForMembership: false };
self.addMemberForm = { username: "", comment: "" };

self.userNames = [];
const getAllUsers = () => CoreDataService.data_profile_list().then((data) => {
self.userList = data.data
self.userNames = Object.values(data.data).map(x => x.account.username);
})
const update = (data) => {
self.circle = data;
if (!self.members) self.members = [];
Expand All @@ -989,6 +994,7 @@
};

update(circle);
getAllUsers();

self.createCircle = () =>
CoreDataService.create_circle(self.addCircleForm).then((res) =>
Expand Down Expand Up @@ -1048,6 +1054,37 @@
self.addCircleForm.comment = "Initial admin";
}
};
self.suggestions = [];

self.updateSuggestions = () => {
if (!self.addMemberForm.username) {
self.suggestions = [];
return;
}
self.suggestions = self.filterUserByLetters(self.addMemberForm.username);
$scope.$apply();

};

self.selectSuggestion = (suggestion) => {
self.addMemberForm.username = suggestion;
self.suggestions = [];
};

self.updateSuggestions = () => {
if (!self.addMemberForm.username) {
self.suggestions = self.userNames;
return;
}
$timeout(() => {
self.filterUserByLetters(self.addMemberForm.username);
});
};
self.filterUserByLetters = (input) => {
self.suggestions = self.userNames.filter(x => x.toLowerCase().startsWith(input.toLowerCase()));

};

}

/**
Expand Down

0 comments on commit 4892a53

Please sign in to comment.