| 
1 |  | -from django.http import HttpResponse  | 
2 | 1 | from datetime import datetime  | 
3 | 2 | from calendar import timegm  | 
 | 3 | + | 
4 | 4 | import logging  | 
5 |  | -import requests  | 
6 | 5 | 
 
  | 
 | 6 | +from IGitt.GitHub.GitHubUser import GitHubToken, GitHubUser  | 
 | 7 | + | 
 | 8 | +from django.views.generic import TemplateView  | 
 | 9 | + | 
 | 10 | +from community.views import get_header_and_footer  | 
 | 11 | +from data.models import Contributor  | 
7 | 12 | from .students import get_linked_students  | 
8 |  | -from .gitorg import get_logo  | 
9 | 13 | from .task import get_tasks  | 
10 | 14 | 
 
  | 
11 | 15 | STUDENT_URL = (  | 
 | 
15 | 19 | )  | 
16 | 20 | 
 
  | 
17 | 21 | 
 
  | 
18 |  | -def index(request):  | 
19 |  | -    logger = logging.getLogger(__name__ + '.index')  | 
20 |  | -    try:  | 
21 |  | -        get_tasks()  | 
22 |  | -    except FileNotFoundError:  | 
23 |  | -        logger.info('GCI data not available')  | 
24 |  | -        s = ['GCI data not available']  | 
25 |  | -    else:  | 
26 |  | -        s = gci_overview()  | 
27 |  | - | 
28 |  | -    return HttpResponse('\n'.join(s))  | 
29 |  | - | 
30 |  | - | 
31 |  | -def gci_overview():  | 
32 |  | -    logger = logging.getLogger(__name__ + '.gci_overview')  | 
33 |  | -    linked_students = list(get_linked_students())  | 
34 |  | -    if not linked_students:  | 
35 |  | -        logger.info('No GCI students are linked')  | 
36 |  | -        return ['No GCI students are linked']  | 
37 |  | - | 
38 |  | -    org_id = linked_students[0]['organization_id']  | 
39 |  | -    org_name = linked_students[0]['organization_name']  | 
40 |  | -    s = []  | 
41 |  | -    s.append('<link rel="stylesheet" href="static/main.css">')  | 
42 |  | - | 
43 |  | -    favicon = get_logo(org_name, 16)  | 
44 |  | -    with open('_site/favicon.png', 'wb') as favicon_file:  | 
45 |  | -        favicon_file.write(favicon)  | 
46 |  | - | 
47 |  | -    org_logo = get_logo(org_name)  | 
48 |  | -    with open('_site/org_logo.png', 'wb') as org_logo_file:  | 
49 |  | -        org_logo_file.write(org_logo)  | 
50 |  | - | 
51 |  | -    s.append('<link rel="shortcut icon" type="image/png" '  | 
52 |  | -             'href="static/favicon.png"/>')  | 
53 |  | -    s.append('<img src="static/org_logo.png" alt="'+org_name+'">')  | 
54 |  | -    s.append('<h2>Welcome</h2>')  | 
55 |  | -    s.append('Hello, world. You are at the {org_name} community GCI website.'  | 
56 |  | -             .format(org_name=org_name))  | 
57 |  | -    s.append('Students linked to %s issues:<ul class="students">' % org_name)  | 
58 |  | -    for student in linked_students:  | 
59 |  | -        student_id = student['id']  | 
60 |  | -        username = student['username']  | 
61 |  | - | 
62 |  | -        r = requests.get('https://api.github.com/users/{}'.format(username))  | 
63 |  | - | 
64 |  | -        if r.status_code == 404:  | 
65 |  | -            continue  | 
66 |  | - | 
67 |  | -        student_url = STUDENT_URL.format(org_id=org_id,  | 
68 |  | -                                         student_id=student_id,  | 
69 |  | -                                         )  | 
70 |  | -        s.append('<li class="student">'  | 
71 |  | -                 'STUDENT ID: <a href="{student_url}">{student_id}</a><br />'  | 
72 |  | -                 '<div class="github-card" data-github="{username}" '  | 
73 |  | -                 'data-width="400" data-theme="default"></div>'  | 
74 |  | -                 .format(student_url=student_url, student_id=student_id,  | 
75 |  | -                         username=username))  | 
76 |  | - | 
77 |  | -    timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')  | 
78 |  | -    s.append('</ul><i id="time" class="timestamp" data-time="{unix}">'  | 
79 |  | -             'Last updated: {timestamp} '  | 
80 |  | -             '(<span id="ago" class="timeago"></span>)</i>'  | 
81 |  | -             .format(unix=timegm(datetime.utcnow().utctimetuple()),  | 
82 |  | -                     timestamp=timestamp))  | 
83 |  | - | 
84 |  | -    s. append( '<script src="//cdn.jsdelivr.net/gh/lepture/[email protected]'  | 
85 |  | -             '/jsdelivr/widget.js"></script>')  | 
86 |  | -    s.append('<script src="static/timeago.js"></script>')  | 
87 |  | -    s.append('<script>loadTimeElements()</script>')  | 
88 |  | - | 
89 |  | -    return s  | 
 | 22 | +class GCIStudentsList(TemplateView):  | 
 | 23 | +    template_name = 'gci_students.html'  | 
 | 24 | + | 
 | 25 | +    def get_all_students(self):  | 
 | 26 | +        logger = logging.getLogger(__name__ + '.gci_overview')  | 
 | 27 | +        linked_students = list(get_linked_students())  | 
 | 28 | +        data = {  | 
 | 29 | +            'students': list(),  | 
 | 30 | +            'error': None  | 
 | 31 | +        }  | 
 | 32 | +        if not linked_students:  | 
 | 33 | +            error_message = 'No GCI students are linked'  | 
 | 34 | +            logger.info(error_message)  | 
 | 35 | +            data['error'] = error_message  | 
 | 36 | +            return data  | 
 | 37 | +        org_id = linked_students[0]['organization_id']  | 
 | 38 | +        for student in linked_students:  | 
 | 39 | +            student_id = student['id']  | 
 | 40 | +            username = student['username']  | 
 | 41 | +            contributors = Contributor.objects.filter(login=username)  | 
 | 42 | +            if contributors.exists():  | 
 | 43 | +                contrib = contributors.first()  | 
 | 44 | +                student['url'] = STUDENT_URL.format(org_id=org_id,  | 
 | 45 | +                                                    student_id=student_id)  | 
 | 46 | +                student['name'] = contrib.name  | 
 | 47 | +                student['bio'] = contrib.bio  | 
 | 48 | +                student['public_repos'] = contrib.public_repos  | 
 | 49 | +                student['public_gists'] = contrib.public_gists  | 
 | 50 | +                student['followers'] = contrib.followers  | 
 | 51 | +                data['students'].append(student)  | 
 | 52 | +            else:  | 
 | 53 | +                logger.warning("GCI Student {} doesn't exists! Please check"  | 
 | 54 | +                               ' the username.'.format(username))  | 
 | 55 | +        return data  | 
 | 56 | + | 
 | 57 | +    def get_gci_tasks_and_students(self):  | 
 | 58 | +        logger = logging.getLogger(__name__ + '.index')  | 
 | 59 | +        gci_students = {  | 
 | 60 | +            'data': {},  | 
 | 61 | +            'error': None  | 
 | 62 | +        }  | 
 | 63 | +        try:  | 
 | 64 | +            get_tasks()  | 
 | 65 | +        except FileNotFoundError:  | 
 | 66 | +            logger.info('GCI data not available')  | 
 | 67 | +            error_message = ('No GCI data is available. Please create a'  | 
 | 68 | +                             ' tasks.yaml file containing GCI tasks related'  | 
 | 69 | +                             ' data in it.')  | 
 | 70 | +            gci_students['error'] = error_message  | 
 | 71 | +        else:  | 
 | 72 | +            data = self.get_all_students()  | 
 | 73 | +            if data['error']:  | 
 | 74 | +                gci_students['error'] = data['error']  | 
 | 75 | +            else:  | 
 | 76 | +                gci_students['data'] = data['students']  | 
 | 77 | +        return gci_students  | 
 | 78 | + | 
 | 79 | +    def get_data_updated_time(self):  | 
 | 80 | +        return timegm(datetime.utcnow().utctimetuple())  | 
 | 81 | + | 
 | 82 | +    def get_context_data(self, **kwargs):  | 
 | 83 | +        context = super().get_context_data(**kwargs)  | 
 | 84 | +        context = get_header_and_footer(context)  | 
 | 85 | +        context['gci_students'] = self.get_gci_tasks_and_students()  | 
 | 86 | +        context['updated_time'] = self.get_data_updated_time()  | 
 | 87 | +        return context  | 
0 commit comments