|
1 |
| -from django.http import HttpResponse |
2 |
| -from django.views.generic.base import TemplateView |
| 1 | +import logging |
| 2 | + |
| 3 | +import requests |
3 | 4 |
|
4 | 5 | from trav import Travis
|
5 | 6 |
|
| 7 | +from django.http import HttpResponse |
| 8 | +from django.views.generic.base import TemplateView |
| 9 | + |
6 | 10 | from .git import (
|
7 | 11 | get_deploy_url,
|
8 | 12 | get_org_name,
|
9 | 13 | get_owner,
|
10 | 14 | get_upstream_deploy_url,
|
| 15 | + get_remote_url |
11 | 16 | )
|
| 17 | +from data.models import Team |
| 18 | +from gamification.models import Participant as GamificationParticipant |
| 19 | +from meta_review.models import Participant as MetaReviewer |
| 20 | + |
| 21 | + |
| 22 | +def initialize_org_context_details(): |
| 23 | + org_name = get_org_name() |
| 24 | + org_details = { |
| 25 | + 'name': org_name, |
| 26 | + 'blog_url': f'https://blog.{org_name}.io/', |
| 27 | + 'twitter_url': f'https://twitter.com/{org_name}_io/', |
| 28 | + 'facebook_url': f'https://www.facebook.com/{org_name}Analyzer', |
| 29 | + 'repo_url': get_remote_url().href, |
| 30 | + 'docs': f'https://{org_name}.io/docs', |
| 31 | + 'newcomer_docs': f'https://{org_name}.io/newcomer', |
| 32 | + 'coc': f'https://{org_name}.io/coc', |
| 33 | + 'logo_url': (f'https://api.{org_name}.io/en/latest/_static/images/' |
| 34 | + f'{org_name}_logo.svg'), |
| 35 | + 'gitter_chat': f'https://gitter.im/{org_name}/{org_name}/', |
| 36 | + 'github_core_repo': f'https://github.com/{org_name}/{org_name}/', |
| 37 | + 'licence_type': 'GNU AGPL v3.0' |
| 38 | + } |
| 39 | + return org_details |
| 40 | + |
| 41 | + |
| 42 | +def get_header_and_footer(context): |
| 43 | + context['isTravis'] = Travis.TRAVIS |
| 44 | + context['travisLink'] = Travis.TRAVIS_BUILD_WEB_URL |
| 45 | + context['org'] = initialize_org_context_details() |
| 46 | + print('Running on Travis: {}, build link: {}'.format(context['isTravis'], |
| 47 | + context['travisLink'] |
| 48 | + )) |
| 49 | + return context |
12 | 50 |
|
13 | 51 |
|
14 | 52 | class HomePageView(TemplateView):
|
15 | 53 | template_name = 'index.html'
|
16 | 54 |
|
17 |
| - def get_context_data(self, **kwargs): |
18 |
| - context = super().get_context_data(**kwargs) |
19 |
| - context['isTravis'] = Travis.TRAVIS |
20 |
| - context['travisLink'] = Travis.TRAVIS_BUILD_WEB_URL |
| 55 | + def get_team_details(self, org_name): |
| 56 | + teams = [ |
| 57 | + f'{org_name} newcomers', |
| 58 | + f'{org_name} developers', |
| 59 | + f'{org_name} admins' |
| 60 | + ] |
| 61 | + team_details = {} |
| 62 | + for team_name in teams: |
| 63 | + team = Team.objects.get(name=team_name) |
| 64 | + team_details[team_name] = team.contributors.count() |
| 65 | + return team_details |
| 66 | + |
| 67 | + def get_quote_of_the_day(self): |
| 68 | + |
| 69 | + try: |
| 70 | + qod = requests.get('http://quotes.rest/qod?category=inspire') |
| 71 | + qod.raise_for_status() |
| 72 | + except requests.HTTPError as err: |
| 73 | + error_info = f'HTTPError while fetching Quote of the day! {err}' |
| 74 | + logging.error(error_info) |
| 75 | + return |
21 | 76 |
|
22 |
| - print('Running on Travis: {}, build link: {}'.format( |
23 |
| - context['isTravis'], |
24 |
| - context['travisLink'])) |
| 77 | + qod_data = qod.json() |
| 78 | + return { |
| 79 | + 'quote': qod_data['contents']['quotes'][0]['quote'], |
| 80 | + 'author': qod_data['contents']['quotes'][0]['author'], |
| 81 | + } |
25 | 82 |
|
| 83 | + def get_top_meta_review_users(self, count): |
| 84 | + participants = MetaReviewer.objects.all()[:count] |
| 85 | + return participants |
| 86 | + |
| 87 | + def get_top_gamification_users(self, count): |
| 88 | + return enumerate(GamificationParticipant.objects.all()[:count]) |
| 89 | + |
| 90 | + def get_context_data(self, **kwargs): |
| 91 | + context = super().get_context_data(**kwargs) |
| 92 | + context = get_header_and_footer(context) |
| 93 | + org_name = context['org']['name'] |
| 94 | + context['org']['team_details'] = dict(self.get_team_details(org_name)) |
| 95 | + about_org = (f'{org_name} (always spelled with a lowercase c!) is one' |
| 96 | + ' of the welcoming open-source organizations for' |
| 97 | + f' newcomers. {org_name} stands for “COde AnaLysis' |
| 98 | + ' Application” as it works well with animals and thus is' |
| 99 | + ' well visualizable which makes it easy to memorize.' |
| 100 | + f' {org_name} provides a unified interface for linting' |
| 101 | + ' and fixing the code with a single configuration file,' |
| 102 | + ' regardless of the programming languages used. You can' |
| 103 | + f' use {org_name} from within your favorite editor,' |
| 104 | + ' integrate it with your CI and, get the results as JSON' |
| 105 | + ', or customize it to your needs with its flexible' |
| 106 | + ' configuration syntax.') |
| 107 | + context['org']['about'] = about_org |
| 108 | + context['quote_details'] = self.get_quote_of_the_day() |
| 109 | + context['top_meta_review_users'] = self.get_top_meta_review_users( |
| 110 | + count=5) |
| 111 | + context['top_gamification_users'] = self.get_top_gamification_users( |
| 112 | + count=5) |
26 | 113 | return context
|
27 | 114 |
|
28 | 115 |
|
|
0 commit comments