import re
 from django.core.exceptions import ObjectDoesNotExist
 
+    
 class Duration(object):
     """Represent a time duration"""
     def __init__(self, *args, **kwargs):
 
 
 from django.contrib.auth.models import User
 from telemeta.models.core import *
-import django.db.models
 from django.core.exceptions import ObjectDoesNotExist
 from django.utils.translation import ugettext_lazy as _
+import django.db.models
 
 
 class Revision(ModelCore):
 class UserProfile(django.db.models.Model):
     "User profile extension"
     
-    user            = django.db.models.ForeignKey(User, unique=True)
+    user            = ForeignKey(User, unique=True)
     institution     = CharField(_('institution'))
     function        = CharField(_('function'))
     address         = TextField(_('address'))
     telephone       = CharField(_('function'))
     expiration_date = DateField(_('expiration_date'))
     
+    class Meta(MetaCore):
+        db_table = 'profiles'
+        
 
--- /dev/null
+{% extends "telemeta_default/profile_detail.html" %}
 
 {% else %}
 {{ user.username }}.
 {% endif %}
-{% trans "Account" %} |
+<a href="{% url telemeta-profile-detail user.username %}">{% trans "Account" %}</a> |
 <a href="{% url telemeta-help %}">{% trans "Help" %}</a> | 
 <a href="{% url telemeta-logout %}">{% trans "Sign out" %}
 <img src="images/logout.png" style="vertical-align:middle" /></a>
 
         {% endif %}
         </td>
         <td>{{ r.revision.element_type }}</td>
-        <td>{{ r.revision.user.username }}</td>
+        <td>{% if r.revision.user %}{{ r.revision.user.username }}{% endif %}</td>
+        
      </tr>
     {% endfor %}
     </table>
   <h2>{{ playlist.playlist.title }}</h2>
   <div style="float:right">
   <a href="{% url telemeta-playlist-csv-export playlist.playlist.public_id %}" class="component_icon button icon_csv">CSV</a>
-  <a href="#" id="{{playlist.playlist.public_id}}" onclick="playlist.remove(this.id);return false;" class="component_icon button icon_cancel"> {% trans "Delete" %}</a>
+  <a href="#" id="{{playlist.playlist.public_id}}" onclick="playlist.remove(this.id);return false;" class="component_icon button icon_cancel">{% trans "Delete" %}</a>
   </div>
   
   
                 {% endif %}
             </td>
             <td>
-            <a href="#" onclick="playlistUtils.removeResource('{{resource.public_id}}');return false;" class="component_icon button icon_cancel"> {% trans "Delete" %}</a>
+            <a href="#" onclick="playlistUtils.removeResource('{{resource.public_id}}');return false;" class="component_icon button icon_cancel"></a>
             </td>
         </tr>
     {% endfor %}
 
--- /dev/null
+{% extends "telemeta/base.html" %}
+{% load i18n %}
+{% load telemeta_utils %}
+
+{% block head_title %}{% trans "User Profile" %} : {{ user.username }}{% endblock %}
+
+{% block submenu %}
+    <div>
+    {% block tools %}
+    {% if user.is_authenticated %}
+      {% trans "Edit" %}
+     {% endif %}
+    {% endblock tools %}
+    </div>
+{% endblock %}
+
+{% block content %}
+    <h3>{% trans "User profile" %} : {{ user.username }}</h3>
+    <div style="padding-top: 1em;">
+     <dl class="listing">
+      <dt>{% trans "First Name" %}</dt><dd>{{ usr.first_name }}</dd>
+      <dt>{% trans "Last Name" %}</dt><dd>{{ usr.last_name }}</dd>
+      <dt>{% trans "Email" %}</dt><dd>{{ usr.email }}</dd>
+      
+      <dt>{% trans "Institution" %}</dt><dd>{% if profile %}{{ profile.institution }}{% endif %}</dd>
+      <dt>{% trans "Function" %}</dt><dd>{% if profile %}{{ profile.function }}{% endif %}</dd>
+      <dt>{% trans "Address" %}</dt><dd>{% if profile %}{{ profile.address }}{% endif %}</dd>
+      <dt>{% trans "Telephone" %}</dt><dd>{% if profile %}{{ profile.telephone }}{% endif %}</dd>
+      <dt>{% trans "Expiration date" %}</dt><dd>{% if profile %}{{ profile.expiration_date }}{% endif %}</dd>
+      
+      <dt>{% trans "Is admin" %}</dt><dd>{{ usr.is_staff }}</dd>
+      <dt>{% trans "Is superuser" %}</dt><dd>{{ usr.is_superuser }}</dd>
+      <dt>{% trans "Last login" %}</dt><dd>{{ usr.last_login }}</dd>
+     </dl>
+    </div>
+{% endblock %}
+
 
     url(r'^oai/.*$', web_view.handle_oai_request, name="telemeta-oai"),
 
     # Authentication
-     url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'telemeta/login.html'},
+    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'telemeta/login.html'},
         name="telemeta-login"),
     url(r'^logout/$', web_view.logout, name="telemeta-logout"),
     
+    # Profile
+    url(r'^accounts/(?P<username>[A-Za-z0-9._-]+)/profile/$', web_view.profile_detail, name="telemeta-profile-detail"),
+    
     # JSON RPC
     url(r'^json/browse/', 'jsonrpc.views.browse', name="jsonrpc_browser"), # for the graphical browser/web console only, omissible
     url(r'^json/$', jsonrpc_site.dispatch, name='jsonrpc_mountpoint'),
 
         description = 'Please login or contact the website administator to get private access.'
         messages.error(request, title)
         return render(request, 'telemeta/messages.html', {'description' : description})
+    
+    @method_decorator(login_required)
+    def profile_detail(self, request, username, template='telemeta/profile_detail.html'):
+        user = User.objects.get(username=username)
+        try:
+            profile = user.get_profile()
+        except:
+            profile = None
+        return render(request, template, {'profile' : profile, 'usr': user})