Changeset bddf042 in OpenWorkouts-current


Ignore:
Timestamp:
Feb 10, 2019, 7:56:34 AM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
f29d4b4
Parents:
4226ce0
Message:

(#7) Allow users profiles to be accessed using a more friendly url:

https://openworkouts.org/profile/NICKNAME

IMPORTANT: This change adds a new index to the catalog, so ensure you
update any existing databases after pulling.

Enter pshell and run this code:

root._update_indexes()
for user in root.users:

root.reindex(user)

Location:
ow
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • ow/models/root.py

    r4226ce0 rbddf042  
    1111    get_catalog,
    1212    install_catalog,
     13    update_indexes,
    1314    reindex_object,
    1415    remove_from_catalog,
     
    4041        indexes = {
    4142            'email': CatalogFieldIndex('email'),
     43            'nickname': CatalogFieldIndex('nickname'),
    4244            'sport': CatalogFieldIndex('sport'),
    4345        }
    4446        return indexes
     47
     48    def _update_indexes(self):
     49        return update_indexes(self.catalog, self._get_catalog_indexes())
    4550
    4651    def reindex(self, obj):
     
    7782            # the catalog will return all users
    7883            res = self.query(Eq('email', email))
     84            if res:
     85                return next(res)
     86        return None
     87
     88    def get_user_by_nickname(self, nickname):
     89        if nickname is not None:
     90            # for some reason, when searching for None
     91            # the catalog will return all users
     92            res = self.query(Eq('nickname', nickname))
    7993            if res:
    8094                return next(res)
  • ow/templates/profile.pt

    r4226ce0 rbddf042  
    1717      <div class="user-profile-account">
    1818        <div>
    19           <tal:c tal:condition="getattr(context, 'picture', None)">
    20             <img tal:attributes="src request.resource_path(context, 'picture')"
     19          <tal:c tal:condition="getattr(user, 'picture', None)">
     20            <img tal:attributes="src request.resource_path(user, 'picture')"
    2121                 width="450" />
    2222          </tal:c>
    2323          <div>
    2424            <h2>
    25                 <tal:fullname tal:content="context.fullname"></tal:fullname>
     25                <tal:fullname tal:content="user.fullname"></tal:fullname>
    2626            </h2>
    2727            <p>
    28               <tal:has-nickname tal:condition="context.nickname">
    29                   <tal:nickname tal:content="context.nickname"></tal:nickname>
    30               </tal:has-nickname> |
    31               <span><tal:email tal:content="context.email"></tal:email></span>
     28              <tal:has-nickname tal:condition="user.nickname">
     29                <a href=""
     30                   tal:attributes="href request.resource_url(request.root, 'profile', user.nickname)"
     31                   tal:content="request.resource_url(request.root, 'profile', user.nickname)">
     32                </a> |
     33              </tal:has-nickname>
     34              <span><tal:email tal:content="user.email"></tal:email></span>
    3235            </p>
    3336            <div class="profile-bio">
    34               <p tal:repeat="paragraph getattr(context, 'bio', '').split('\n')"
     37              <p tal:repeat="paragraph getattr(user, 'bio', '').split('\n')"
    3538                 tal:content="paragraph"></p>
    3639            </div>
    3740            <ul class="workout-options">
    3841              <li><a href=""
    39                      tal:attributes="href request.resource_url(context, 'edit')"
     42                     tal:attributes="href request.resource_url(user, 'edit')"
    4043                     i18n:translate="">edit profile</a></li>
    4144              <li><a href=""
    42                      tal:attributes="href request.resource_url(context, 'passwd')"
     45                     tal:attributes="href request.resource_url(user, 'passwd')"
    4346                     i18n:translate="">change password</a></li>
    4447            </ul>
     
    103106              <ul class="workout-info">
    104107                <li>
    105                   <tal:c tal:content="workout.start_in_timezone(context.timezone)"></tal:c>
     108                  <tal:c tal:content="workout.start_in_timezone(user.timezone)"></tal:c>
    106109                </li>
    107110                <li>
     
    155158            <li>
    156159              <span><tal:t i18n:translate="">Gender:</tal:t></span>
    157               <tal:c tal:content="getattr(context, 'gender', '-')"></tal:c>
    158             </li>
    159             <li tal:define="birth_date getattr(context, 'birth_date', None)">
     160              <tal:c tal:content="getattr(user, 'gender', '-')"></tal:c>
     161            </li>
     162            <li tal:define="birth_date getattr(user, 'birth_date', None)">
    160163              <span><tal:t i18n:translate="">Birth date:</tal:t></span>
    161164              <tal:c tal:condition="birth_date"
     
    165168            <li>
    166169              <span><tal:t i18n:translate="">Height:</tal:t></span>
    167               <tal:c tal:content="getattr(context, 'height', '-')"></tal:c> meters
     170              <tal:c tal:content="getattr(user, 'height', '-')"></tal:c> meters
    168171            </li>
    169172            <li>
    170173              <span><tal:t i18n:translate="">Weight:</tal:t></span>
    171               <tal:c tal:content="getattr(context, 'weight', '-')"></tal:c> kg
     174              <tal:c tal:content="getattr(user, 'weight', '-')"></tal:c> kg
    172175            </li>
    173176          </ul>
     
    195198         switcher_selector: '.js-month-stats .js-switcher a',
    196199         is_active_class: 'is-active',
    197          urls: {"monthly": "${request.resource_url(context, 'monthly')}",
    198                 "weekly": "${request.resource_url(context, 'weekly')}"},
     200         urls: {"monthly": "${request.resource_url(user, 'monthly')}",
     201                "weekly": "${request.resource_url(user, 'weekly')}"},
    199202         current_month: "${current_month}",
    200203         current_week: "${current_week}",
  • ow/tests/models/test_root.py

    r4226ce0 rbddf042  
    3535        # a new OpenWorkouts instance has a catalog created automatically
    3636        assert isinstance(root.catalog, Catalog)
    37         assert len(root.catalog) == 2
    38         assert 'email' in root.catalog
    39         assert 'sport' in root.catalog
     37        assert len(root.catalog) == 3
     38        for key in ['email', 'nickname', 'sport']:
     39            assert key in root.catalog
    4040
    4141    def test_add_user_ok(self, root):
  • ow/tests/test_catalog.py

    r4226ce0 rbddf042  
    5656        changes = update_indexes(catalog, indexes)
    5757        assert changes['added'] == ['newindex']
    58         assert changes['removed'] == ['email', 'sport']
     58        assert changes['removed'] == ['email', 'nickname', 'sport']
    5959
    6060    def test_update_indexes_empty(self, root):
     
    6363        changes = update_indexes(catalog, indexes)
    6464        assert changes['added'] == []
    65         assert changes['removed'] == ['email', 'sport']
     65        assert changes['removed'] == ['email', 'nickname', 'sport']
    6666
    6767    def test_install_catalog(self):
  • ow/tests/views/test_user.py

    r4226ce0 rbddf042  
    270270        # profile page for the current day (no workouts avalable)
    271271        response = user_views.profile(john, request)
    272         assert len(response.keys()) == 4
     272        assert len(response.keys()) == 5
    273273        current_month = datetime.now(timezone.utc).strftime('%Y-%m')
     274        assert response['user'] == john
    274275        assert response['current_month'] == current_month
    275276        assert response['current_week'] is None
     
    284285        request.GET['month'] = 6
    285286        response = user_views.profile(john, request)
    286         assert len(response.keys()) == 4
     287        assert len(response.keys()) == 5
     288        assert response['user'] == john
    287289        assert response['current_month'] == '2015-06'
    288290        assert response['current_week'] is None
     
    299301        request.GET['week'] = 25
    300302        response = user_views.profile(john, request)
    301         assert len(response.keys()) == 4
     303        assert len(response.keys()) == 5
     304        assert response['user'] == john
    302305        assert response['current_month'] == '2015-06'
    303306        assert response['current_week'] == 25
     
    313316        request.GET['week'] = 26
    314317        response = user_views.profile(john, request)
    315         assert len(response.keys()) == 4
     318        assert len(response.keys()) == 5
     319        assert response['user'] == john
    316320        assert response['current_month'] == '2015-06'
    317321        assert response['current_week'] == 26
  • ow/views/user.py

    r4226ce0 rbddf042  
    44from decimal import Decimal
    55
    6 from pyramid.httpexceptions import HTTPFound
     6from pyramid.httpexceptions import HTTPFound, HTTPNotFound
    77from pyramid.view import view_config
    88from pyramid.security import remember, forget
     
    157157
    158158@view_config(
     159    context=OpenWorkouts,
     160    name='profile',
     161    permission='view',
     162    renderer='ow:templates/profile.pt')
     163@view_config(
    159164    context=User,
    160165    permission='view',
     
    166171    basic info, stats, etc
    167172    """
     173    if isinstance(context, OpenWorkouts):
     174        nickname = request.subpath[0]
     175        user = request.root.get_user_by_nickname(nickname)
     176        if user is None:
     177            return HTTPNotFound()
     178    else:
     179        user = context
    168180    now = datetime.now(timezone.utc)
    169181    year = int(request.GET.get('year', now.year))
    170182    month = int(request.GET.get('month', now.month))
    171183    week = request.GET.get('week', None)
    172     workouts = context.workouts(year, month, week)
     184    workouts = user.workouts(year, month, week)
    173185    totals = {
    174186        'distance': Decimal(0),
     
    186198
    187199    return {
     200        'user': user,
    188201        'workouts': workouts,
    189202        'current_month': '{year}-{month}'.format(
Note: See TracChangeset for help on using the changeset viewer.