Skip to content

Commit

Permalink
change to rest
Browse files Browse the repository at this point in the history
  • Loading branch information
octavioamu committed Apr 11, 2019
1 parent cf58dbb commit 8c4f877
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 108 deletions.
3 changes: 2 additions & 1 deletion app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@
re_path(r'', include(wagtail_urls)),

# users
path('users',dashboard.views.users, name='users'),
path('users',dashboard.views.users_directory, name='users_directory'),
url(r'^api/v0.1/users_fetch/', dashboard.views.users_fetch, name='users_fetch'),
]

if settings.ENABLE_SILK:
Expand Down
58 changes: 58 additions & 0 deletions app/assets/v2/js/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
let users = [];
let usersPage = 1;
let usersNumPages = '';
let usersHasNext = false;

Vue.mixin({
methods: {
fetchUsers: function(newPage) {
var vm = this;

if (newPage) {
vm.usersPage = newPage;
}

var getNotifications = fetchData (`/api/v0.1/users_fetch/?page=${vm.usersPage}`, 'GET');

$.when(getNotifications).then(function(response) {
newNotifications = newData(response.data, vm.notifications);
newNotifications.forEach(function(item) {
vm.notifications.push(item);
});

vm.usersNumPages = response.num_pages;
vm.usersHasNext = response.has_next;
vm.numNotifications = response.count;

vm.checkUnread();
if (vm.usersHasNext) {
vm.usersPage = ++vm.usersPage;

} else {
vm.usersPage = 1;
}
});
}

}

});

if (document.getElementById('gc-users-directory')) {
var app = new Vue({
delimiters: [ '[[', ']]' ],
el: '#gc-users-directory',
data: {
users,
usersPage,
usersNumPages,
usersHasNext
},
mounted() {
this.fetchUsers();
},
created() {
// this.sendState();
}
});
}
213 changes: 108 additions & 105 deletions app/dashboard/templates/dashboard/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,120 +58,122 @@
{% include 'shared/top_nav.html' with class='d-md-flex' %}
{% include 'shared/nav.html' %}
</div>
<div class="container-fluid">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#usersFilters" aria-controls="usersFilters" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="usersFilters">
<div class="navbar-nav mr-4">
<label for="">{% trans 'Sort by:' %}</label>
<select name="" id="sort_order">
<option value="-created_on">{% trans 'Newest' %}</option>
<option value="created_on">{% trans 'Oldest' %}</option>
<option value="-popularity">{% trans 'All Time Popularity' %}</option>
<option value="-popularity_week">{% trans 'Trending' %}</option>
<option value="price_finney">{% trans 'Low price' %}</option>
<option value="-price_finney">{% trans 'High price' %}</option>
<option value="name">{% trans 'Name' %}</option>
<option value="-rarity">{% trans 'Rarity' %}</option>
</select>
<div class="" id="gc-users-directory">
<div class="container-fluid">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#usersFilters" aria-controls="usersFilters" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="usersFilters">
<div class="navbar-nav mr-4">
<label for="">{% trans 'Sort by:' %}</label>
<select name="" id="sort_order">
<option value="-created_on">{% trans 'Newest' %}</option>
<option value="created_on">{% trans 'Oldest' %}</option>
<option value="-popularity">{% trans 'All Time Popularity' %}</option>
<option value="-popularity_week">{% trans 'Trending' %}</option>
<option value="price_finney">{% trans 'Low price' %}</option>
<option value="-price_finney">{% trans 'High price' %}</option>
<option value="name">{% trans 'Name' %}</option>
<option value="-rarity">{% trans 'Rarity' %}</option>
</select>

</div>
<div class="navbar-nav">
<select class="keywords-filter form-control" multiple="multiple" style="width:300px; height: 37px;"></select>
</div>
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
<div class="navbar-nav">
<select class="keywords-filter form-control" multiple="multiple" style="width:300px; height: 37px;"></select>
</div>
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
</div>
<div class="container">
<div class="py-5 mt-5">
<div class="grid-4">
{% for user in users %}
<div class="card shadow-sm border-0">
<img class="rounded-circle shadow mx-auto mt-n5" width="140" height="140" src="{% if user.avatar and user.active_avatar.avatar_url %}{{ user.active_avatar.avatar_url }}{% else %}{{ user.avatar_url }}{% endif %}" />
<div class="card-body">

<h5 class="text-center">
{% if user.data.name %}
{{ user.data.name }}
{% else %}
@{{ user.handle }}
{% endif %}
</h5>
<a href="{% url "profile" user %}" class="text-center d-block">
@{{ user.handle }}
</a>
{% if user.keywords %}
<div>
{% for keyword in user.keywords %}
<span class="badge badge--greenlight">{{ keyword }}</span>
{% endfor %}
</div>
{% endif %}
<div class="">
<a href="{{ user.data.html_url }}?tab=repositories" target="_blank" rel="noopener noreferrer" >
<i class="fab fa-github"></i>
</nav>
</div>
<div class="container">
<div class="py-5 mt-5">
<div class="grid-4">
{% for user in users %}
<div class="card shadow-sm border-0">
<img class="rounded-circle shadow mx-auto mt-n5" width="140" height="140" src="{% if user.avatar and user.active_avatar.avatar_url %}{{ user.active_avatar.avatar_url }}{% else %}{{ user.avatar_url }}{% endif %}" />
<div class="card-body">

<h5 class="text-center">
{% if user.data.name %}
{{ user.data.name }}
{% else %}
@{{ user.handle }}
{% endif %}
</h5>
<a href="{% url "profile" user %}" class="text-center d-block">
@{{ user.handle }}
</a>
{% if user.data.blog %}
<a href="{{ user.data.blog|add_url_schema }}" target="_blank" rel="noopener noreferrer" >
<i class="fas fa-home"></i>
</a>
{% endif %}
{% if user.data.email %}
<a href="mailto:{{user.data.email | obfuscate}}">
<i class="far fa-envelope"></i>
</a>
{% endif %}
</div>
{% if user.show_job_status %}
<div class="my-2 font-smaller-3" id="job_status">
{% if user.resume %}
<a href="{{ MEDIA_URL }}{{user.resume}}" download data-toggle="tooltip" title="Download resume">
<i class="fa fa-briefcase mr-2" aria-hidden="true"></i>{{ user.job_status_verbose }}
</a>
{% else %}
<i class="fa fa-briefcase mr-2" aria-hidden="true"></i>{{ user.job_status_verbose }}
{% if user.keywords %}
<div>
{% for keyword in user.keywords %}
<span class="badge badge--greenlight">{{ keyword }}</span>
{% endfor %}
</div>
{% endif %}
</div>
{% endif %}
<small>
Joined: <time datetime="{{ user.created_on }}" title="{{ user.created_on }}">{{ user.created_on|date:"Y/m/d"}}</time>
</small>
<div class="">
<a href="{{ user.data.html_url }}?tab=repositories" target="_blank" rel="noopener noreferrer" >
<i class="fab fa-github"></i>
</a>
{% if user.data.blog %}
<a href="{{ user.data.blog|add_url_schema }}" target="_blank" rel="noopener noreferrer" >
<i class="fas fa-home"></i>
</a>
{% endif %}
{% if user.data.email %}
<a href="mailto:{{user.data.email | obfuscate}}">
<i class="far fa-envelope"></i>
</a>
{% endif %}
</div>
{% if user.show_job_status %}
<div class="my-2 font-smaller-3" id="job_status">
{% if user.resume %}
<a href="{{ MEDIA_URL }}{{user.resume}}" download data-toggle="tooltip" title="Download resume">
<i class="fa fa-briefcase mr-2" aria-hidden="true"></i>{{ user.job_status_verbose }}
</a>
{% else %}
<i class="fa fa-briefcase mr-2" aria-hidden="true"></i>{{ user.job_status_verbose }}
{% endif %}
</div>
{% endif %}
<small>
Joined: <time datetime="{{ user.created_on }}" title="{{ user.created_on }}">{{ user.created_on|date:"Y/m/d"}}</time>
</small>


</div>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
</div>
</div>
</div>


Expand All @@ -194,6 +196,7 @@ <h5 class="text-center">
data: document.keywords
})
</script>
<script src="{% static "v2/js/users.js" %}"></script>

</body>
</html>
52 changes: 50 additions & 2 deletions app/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ def onboard(request, flow):
return TemplateResponse(request, 'ftux/onboard.html', params)


def users(request):
"""Handle displaying the users."""
def users_directory(request):
"""Handle displaying users directory page."""

# q = request.GET.get('q')
order_by = request.GET.get('order_by', '-created_on')
Expand All @@ -757,6 +757,54 @@ def users(request):
}
return TemplateResponse(request, 'dashboard/users.html', params)


@require_GET
def users_fetch(request):
"""Handle displaying users."""
# order_by = request.GET.get('order_by', '-created_on')
# query_kwargs = {

# }
# user_list = Profile.objects.all()
# users = user_list.order_by(order_by).cache()

# params = {
# 'active': 'users',
# 'title': 'Users',
# 'meta_title': "",
# 'meta_description': "",
# 'users': users,
# 'keywords': json.dumps([str(key) for key in Keyword.objects.all().values_list('keyword', flat=True)]),
# }


limit = int(request.GET.get('limit', 10))
page = int(request.GET.get('page', 1))
order_by = request.GET.get('order_by', '-created_on')
user_list = Profile.objects.all()
users = user_list.order_by(order_by).cache()
# all_notifs = Notification.objects.filter(to_user_id=request.user.id).order_by('-id')
params = dict()
all_pages = Paginator(users, limit)
if page <= 0 or page > all_pages.num_pages:
page = 1
all_users = []
# all_users = all_pages.page(page)
for user in all_pages.page(page):
print(user)
profile_json = {}
if user.avatar_baseavatar_related.exists():
profile_json['avatar_id'] = user.avatar_baseavatar_related.first().pk
profile_json['avatar_url'] = user.avatar_baseavatar_related.first().avatar_url
profile_json = user.to_standard_dict()

This comment has been minimized.

Copy link
@owocki

owocki May 23, 2019

Contributor

i wish we had done a better job of scrubbing this standard dict of private info (slack token, email, github access token, etc) before launching @danlipert @octavioamu

This comment has been minimized.

Copy link
@octavioamu

octavioamu May 23, 2019

Author Contributor

Yes, 100% agree with you @owocki In general I think we are failing hard in review/test work. Something Im keeping for the retro actually.

This comment has been minimized.

Copy link
@danlipert

danlipert May 24, 2019

Contributor

Yeah this definitely sucks - @octavioamu we need to be careful using terms like user when we refer to a model instance of Profile, I think this can lead to a lot of confusion as to what is happening given there is a User model already. Also, I think writing tests for this would have helped prevent this

all_users.append(profile_json)
params['data'] = json.dumps(all_users)
params['has_next'] = all_pages.page(page).has_next()
params['count'] = all_pages.count
params['num_pages'] = all_pages.num_pages
return JsonResponse(params, status=200, safe=False)


def dashboard(request):
"""Handle displaying the dashboard."""

Expand Down

0 comments on commit 8c4f877

Please sign in to comment.