#!/usr/bin/python
#
# Use as e.g.
# ./export_website_groups.py > /mit/asa/resources/group-include.html

import codecs
import datetime
import os
import sys

if __name__ == '__main__':
    cur_file = os.path.abspath(__file__)
    django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..'))
    sys.path.append(django_dir)
    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from django.template import Template, Context
import groups.models

TMPL = Template(u"""
<!--
Automatically generated by asadb/util/export_website_groups.py on {{date}}.
Do not edit; instead ask a DB maintainer (asa-db@) to re-run that script (or edit script as necessary).

A more recent version may be in /mit/asa-db/data/db-dump/website_groups.html.
Includes groups with status is_active=True (ie, Suspended and Active groups).
-->
{% for group in groups %}
<tr><td>
{% if group.website_url %}
<a href="{{group.website_url}}">{{group.name}}</a>
{% else %}
{{group.name}}
{% endif %}
</td><td>{{group.description}}</td></tr>
{% endfor %}
""")

if __name__ == '__main__':
    ctx = Context({
        'groups': groups.models.Group.objects.filter(group_status__is_active=True),
        'date': datetime.datetime.now(),
    })
    out = codecs.getwriter('utf-8')(sys.stdout)
    out.write(TMPL.render(ctx))
