|
| 1 | +import logging |
| 2 | +import json |
| 3 | +import getorg |
| 4 | +import os |
| 5 | + |
| 6 | +from django.core.management.base import BaseCommand |
| 7 | + |
| 8 | +from data.models import Contributor |
| 9 | +from activity.scraper import activity_json |
| 10 | + |
| 11 | + |
| 12 | +class Command(BaseCommand): |
| 13 | + help = 'Create a cluster map using contributors geolocation' |
| 14 | + |
| 15 | + def add_arguments(self, parser): |
| 16 | + parser.add_argument('output_dir', nargs='?', type=str) |
| 17 | + |
| 18 | + def handle(self, *args, **options): |
| 19 | + logger = logging.getLogger(__name__) |
| 20 | + output_dir = options.get('output_dir') |
| 21 | + |
| 22 | + if not output_dir: |
| 23 | + logger.debug('output_dir arg not provided! Setting output_dir' |
| 24 | + ' to cluster_map/') |
| 25 | + output_dir = 'cluster_map' |
| 26 | + |
| 27 | + class Location: |
| 28 | + |
| 29 | + def __init__(self, longitude, latitude): |
| 30 | + self.longitude = longitude |
| 31 | + self.latitude = latitude |
| 32 | + |
| 33 | + org_location_dict = {} |
| 34 | + |
| 35 | + for contrib in Contributor.objects.all(): |
| 36 | + if not contrib.login.startswith('testuser') and contrib.location: |
| 37 | + user_location = json.loads(contrib.location) |
| 38 | + location = Location(user_location['longitude'], |
| 39 | + user_location['latitude']) |
| 40 | + org_location_dict[contrib.login] = location |
| 41 | + logger.debug('{} location {} added on map'.format( |
| 42 | + contrib.login, user_location)) |
| 43 | + getorg.orgmap.output_html_cluster_map(org_location_dict, |
| 44 | + folder_name=output_dir) |
| 45 | + |
| 46 | + self.move_and_make_changes_in_files(output_dir) |
| 47 | + |
| 48 | + # Fetch data for activity graph to be displayed on home-page |
| 49 | + activity_json('static/activity-data.js') |
| 50 | + |
| 51 | + def move_and_make_changes_in_files(self, output_dir): |
| 52 | + # Move leaflet_dist folder to static folder |
| 53 | + leaflet_source_path = '{}/{}/leaflet_dist/'.format(os.getcwd(), |
| 54 | + output_dir) |
| 55 | + leaflet_destination_path = '{}/{}/leaflet_dist/'.format(os.getcwd(), |
| 56 | + 'static') |
| 57 | + os.renames(leaflet_source_path, leaflet_destination_path) |
| 58 | + # Move org_locations.js to static folder |
| 59 | + locations_source_path = '{}/{}/org-locations.js'.format(os.getcwd(), |
| 60 | + output_dir) |
| 61 | + locations_destination_path = '{}/{}/org-locations.js'.format( |
| 62 | + os.getcwd(), 'static') |
| 63 | + os.rename(locations_source_path, locations_destination_path) |
| 64 | + # Change map.html to support django |
| 65 | + with open('{}/map.html'.format(output_dir)) as f: |
| 66 | + django_supported_htmls = [] |
| 67 | + lines = f.readlines() |
| 68 | + for index in range(len(lines)): |
| 69 | + line = lines[index].strip() |
| 70 | + if line.__contains__('<html>'): |
| 71 | + django_supported_htmls.append('{% load staticfiles %}\n') |
| 72 | + django_supported_htmls.append(line+'\n') |
| 73 | + elif line.__contains__('</head>'): |
| 74 | + adjust_prop = ''' |
| 75 | + <style> |
| 76 | + #map { |
| 77 | + width: 60%; |
| 78 | + height: 300px; |
| 79 | + margin: auto; |
| 80 | + box-shadow: 0px 0px 25px 2px; |
| 81 | + } |
| 82 | + @media only screen and (max-width:750px){ |
| 83 | + #map { |
| 84 | + width: 90% |
| 85 | + } |
| 86 | + } |
| 87 | + </style>\n |
| 88 | + ''' |
| 89 | + meta_charset = '<meta charset="utf-8">' |
| 90 | + adjust_prop = adjust_prop.strip().replace(' ', '') |
| 91 | + django_supported_htmls.insert(6, meta_charset+'\n') |
| 92 | + django_supported_htmls.append(adjust_prop+'\n') |
| 93 | + django_supported_htmls.append(line+'\n') |
| 94 | + elif line.__contains__('https://'): |
| 95 | + line = line.replace('https:', '').replace(' />', '>') |
| 96 | + django_supported_htmls.append(line.strip()+'\n') |
| 97 | + elif line.__contains__('<link '): |
| 98 | + line = line.replace('href="', 'href="{% static \'') |
| 99 | + line = line.replace('.css', '.css\' %}').replace(' />', '>') |
| 100 | + django_supported_htmls.append(line+'\n') |
| 101 | + elif line.__contains__('<script '): |
| 102 | + line = line.replace('src="', 'src="{% static \'') |
| 103 | + line = line.replace('.js', '.js\' %}') |
| 104 | + if line.__contains__(' type="text/javascript"'): |
| 105 | + line = line.replace(' type="text/javascript"', '') |
| 106 | + django_supported_htmls.append(line+'\n') |
| 107 | + elif line.__contains__('Mouse over') or len(line) == 0: |
| 108 | + pass |
| 109 | + else: |
| 110 | + django_supported_htmls.append(line+'\n') |
| 111 | + # Add map.html to templates folder |
| 112 | + html_map_path = '{}/{}/map.html' |
| 113 | + html_map_source_path = html_map_path.format(os.getcwd(), |
| 114 | + output_dir) |
| 115 | + html_map_destination_path = html_map_path.format(os.getcwd(), |
| 116 | + 'templates') |
| 117 | + os.remove(html_map_source_path) |
| 118 | + with open(html_map_destination_path, 'w+') as f: |
| 119 | + f.writelines(django_supported_htmls) |
0 commit comments