PDA

View Full Version : Tweaking/Coding SRL Stats.



Wizzup?
12-27-2010, 10:07 AM
Work in progress

Do you know a bit of HTML and want to see if you can help make SRL Stats better?

First, get the source code:



git clone git://github.com/MerlijnWajer/SRL-Stats.git srlstats


Go to srlstats/templates.

Example template:


$ cat user.html
{% extends "base.html" %}
{% block title %}
User Page: {{ user.name }}
{% endblock %}

{% block content %}
<h1>
User: {{ user.name }}
</h1>
<p>{{ user.name}} has committed a total time of
{{ _import['datetime'].timedelta(minutes=ttc) }}
in {{ tc }} commits. </p>
{% if user.scripts %}
<p> Scripts: <p/>
<ul>
{% for script in user.scripts %}
<li>
<a href="{{baseurl}}/script/{{ script.id }}">
{{ script.name }}
</a></li>
{% endfor %}
</ul>
{% else %}
<p> {{ user.name }} does not own any scripts. </p>
{% endif %}

<p>
You can view all the commits of this user
<a href="{{baseurl}}/user/{{user.id}}/commits">
here
</a>
</p>.
{% endblock %}


You can simply change the HTML or text to make it look better, correct typos, etc.


More advanced editing

This requires some python knowledge.

As you can see, it isn't just html. SRL Stats uses Jinja2: http://jinja.pocoo.org/
It might help to familiarize yourself a bit with jinja.

Every template has certain variables exported to it. In this case, the variables ``user'' is exported. The variable user has an attribute ``scripts'' which automatically fetches all the scripts that the user owns. These are then looped over and printed.

The user also has an id, name, etc.
For an overview of all the attributes and relations, see classes.py:
class User(Base):
"""
The User object represents a user.
It has a name, password and date registered.
Associated with the user are 'scripts' and 'commits'.

Scripts consists of the scripts owned by the user,
Commits consists of the commits submitted by the user.
"""
__tablename__ = 'users'

id = Column(Integer, primary_key=True)
name = Column(String(20), unique=True)
password = Column(String(64), nullable=False)
mail = Column(String(40), nullable=True)
registertime = Column(DateTime, default=func.now())
# scripts = user owned scripts
# commits = user owned commits

def __init__(self, name, password, mail=None):
self.name = name
self.password = password
self.mail = mail

def __repr__(self):
return 'User(%s)' % self.name


In this case, we can't see the scripts attribute declaration yet. (Although the comment "#scripts = user owned scripts" does cover it)
That is because it is created later on: (look at the owner = line, with backref='scripts')

class Script(Base):
"""
The Script object represents a script.
It has a name, and an owner. The owner is a User instance.
"""
__tablename__ = 'scripts'

id = Column(Integer, primary_key=True)
owner_id = Column(Integer, ForeignKey('users.id'))
name = Column(String(40), unique=True)

owner = relationship(User, backref=backref('scripts', order_by=id))
variables = relationship('Variable', secondary=script_variables,
backref='scripts')
# commits = commits to script

def __init__(self, name):
self.name = name

def __repr__(self):
return 'Script(%s : %s)' % (self.name, self.owner)


TODO