|
| 1 | +from django.http import HttpResponse |
| 2 | +import requests |
| 3 | +import json |
| 4 | + |
| 5 | +from .students import get_students, get_linked_students |
| 6 | +from .gitorg import get_logo |
| 7 | +from gci.config import get_api_key |
| 8 | + |
| 9 | + |
| 10 | +def index(request): |
| 11 | + try: |
| 12 | + client = get_api_key('GCI') |
| 13 | + api_key_available = True |
| 14 | + except BaseException: |
| 15 | + client = None |
| 16 | + api_key_available = False |
| 17 | + |
| 18 | + s = [] |
| 19 | + |
| 20 | + if api_key_available: |
| 21 | + linked_students = list(get_linked_students(get_students())) |
| 22 | + org_name = linked_students[0]['organization_name'] |
| 23 | + favicon = get_logo(org_name, 16) |
| 24 | + with open('_site/favicon.png', 'wb') as favicon_file: |
| 25 | + favicon_file.write(favicon) |
| 26 | + |
| 27 | + org_logo = get_logo(org_name) |
| 28 | + with open('_site/org_logo.png', 'wb') as org_logo_file: |
| 29 | + org_logo_file.write(org_logo) |
| 30 | + s.append('<link rel="../shortcut icon" type="image/png" ' |
| 31 | + 'href="static/favicon.png"/>') |
| 32 | + s.append('<img src="../static/org_logo.png" alt="'+org_name+'">') |
| 33 | + else: |
| 34 | + s.append( |
| 35 | + 'WARNING: API key not available. Expect limited functionality') |
| 36 | + |
| 37 | + s.append('<title>Community website</title>') |
| 38 | + s.append('<ul><li><a href="/gci/">Google Code-in</a>' |
| 39 | + '<li><a href="/activity/">GitHub activity</a></ul>') |
| 40 | + |
| 41 | + if api_key_available: |
| 42 | + api_data_dump = json.loads( |
| 43 | + requests.get('https://gci-leaders.netlify.com/data.json').content) |
| 44 | + for item in api_data_dump: |
| 45 | + if item['name'] == org_name: |
| 46 | + org_twitter_handle = item['twitter_url'].split( |
| 47 | + 'twitter.com/')[-1] |
| 48 | + if org_twitter_handle is not None: |
| 49 | + s.append('<a class="twitter-timeline" data-height="1000" ' |
| 50 | + 'data-link-color="#2B7BB9" ' |
| 51 | + 'href="https://twitter.com/{twitter_handle}">' |
| 52 | + 'Tweets by {twitter_handle}</a> <script async ' |
| 53 | + 'src="https://platform.twitter.com/widgets.js" ' |
| 54 | + 'charset="utf-8"></script>'.format( |
| 55 | + twitter_handle=org_twitter_handle)) |
| 56 | + |
| 57 | + return HttpResponse('\n'.join(s)) |
0 commit comments