From 144365cfa682509b38860bab6bec4b86c6dc98ae Mon Sep 17 00:00:00 2001 From: hydr063n Date: Wed, 13 May 2020 16:51:57 +0200 Subject: [PATCH] feature - add flags to leaderboard (#6509) * add flags to leaderboard * remove conversion to list * switch csv to python file and clean up flag logic Co-authored-by: Dan Lipert Co-authored-by: Aditya Anand M C --- app/marketing/country_codes.py | 272 ++++++++++++++++++++++++++ app/marketing/views.py | 16 ++ app/retail/templates/leaderboard.html | 4 +- 3 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 app/marketing/country_codes.py diff --git a/app/marketing/country_codes.py b/app/marketing/country_codes.py new file mode 100644 index 00000000000..0e52f2b65e4 --- /dev/null +++ b/app/marketing/country_codes.py @@ -0,0 +1,272 @@ +COUNTRY_DATA =[ + "us","United States", + "ad","Andorra", + "ae","United Arab Emirates", + "af","Afghanistan", + "ag","Antigua and Barbuda", + "ai","Anguilla", + "ah","Artsakh", + "ak","Abkhazia", + "al","Albania", + "am","Armenia", + "an","Netherlands Antilles", + "ao","Angola", + "aq","Antarctica", + "ar","Argentina", + "as","American Samoa", + "at","Austria", + "au","Australia", + "aw","Aruba", + "ax","Åland Islands", + "az","Azerbaijan", + "ba","Bosnia and Herzegovina", + "bb","Barbados", + "bd","Bangladesh", + "be","Belgium", + "bf","Burkina Faso", + "bg","Bulgaria", + "bh","Bahrain", + "bi","Burundi", + "bj","Benin", + "bl","Saint Barthélemy", + "bm","Bermuda", + "bn","Brunei Darussalam", + "bo","Bolivia", + "bq","Bonaire, Sint Eustatius and Saba", + "br","Brazil", + "bs","Bahamas", + "bt","Bhutan", + "bv","Bouvet Island", + "bw","Botswana", + "by","Belarus", + "bz","Belize", + "ca","Canada", + "cc","Cocos (Keeling) Islands", + "cd","Congo, The Democratic Republic Of The", + "cf","Central African Republic", + "cg","Congo", + "ch","Switzerland", + "ci","Côte D'Ivoire", + "ck","Cook Islands", + "cl","Chile", + "cm","Cameroon", + "cn","China", + "co","Colombia", + "cr","Costa Rica", + "cu","Cuba", + "cv","Cape Verde", + "cw","Curaçao", + "cx","Christmas Island", + "cy","Cyprus", + "cz","Czech Republic", + "de","Germany", + "dj","Djibouti", + "dk","Denmark", + "dm","Dominica", + "do","Dominican Republic", + "dz","Algeria", + "ec","Ecuador", + "ee","Estonia", + "eg","Egypt", + "eh","Western Sahara", + "er","Eritrea", + "es","Spain", + "et","Ethiopia", + "eu","Europe", + "fi","Finland", + "fj","Fiji", + "fk","Falkland Islands (Malvinas)", + "fm","Micronesia, Federated States Of", + "fo","Faroe Islands", + "fr","France", + "ga","Gabon", + "gb","United Kingdom", + "gd","Grenada", + "ge","Georgia", + "gf","French Guiana", + "gg","Guernsey", + "gh","Ghana", + "gi","Gibraltar", + "gl","Greenland", + "gm","Gambia", + "gn","Guinea", + "gp","Guadeloupe", + "gq","Equatorial Guinea", + "gr","Greece", + "gs","South Georgia and the South Sandwich Islands", + "gt","Guatemala", + "gu","Guam", + "gw","Guinea-Bissau", + "gy","Guyana", + "hk","Hong Kong", + "hm","Heard and McDonald Islands", + "hn","Honduras", + "hr","Croatia", + "ht","Haiti", + "hu","Hungary", + "ic","Canary Islands", + "id","Indonesia", + "ie","Ireland", + "il","Israel", + "im","Isle of Man", + "in","India", + "io","British Indian Ocean Territory", + "iq","Iraq", + "ir","Iran, Islamic Republic Of", + "is","Iceland", + "it","Italy", + "je","Jersey", + "jm","Jamaica", + "jo","Jordan", + "jp","Japan", + "ke","Kenya", + "kg","Kyrgyzstan", + "kh","Cambodia", + "ki","Kiribati", + "km","Comoros", + "kn","Saint Kitts And Nevis", + "kp","Korea, Democratic People's Republic Of", + "kr","Korea, Republic of", + "kw","Kuwait", + "ky","Cayman Islands", + "kz","Kazakhstan", + "la","Lao People's Democratic Republic", + "lb","Lebanon", + "lc","Saint Lucia", + "li","Liechtenstein", + "lk","Sri Lanka", + "lr","Liberia", + "ls","Lesotho", + "lt","Lithuania", + "lu","Luxembourg", + "lv","Latvia", + "ly","Libya", + "ma","Morocco", + "mc","Monaco", + "md","Moldova, Republic of", + "me","Montenegro", + "mf","Saint Martin", + "mg","Madagascar", + "mh","Marshall Islands", + "mk","Macedonia, the Former Yugoslav Republic Of", + "ml","Mali", + "mm","Myanmar", + "mn","Mongolia", + "mo","Macao", + "mp","Northern Mariana Islands", + "mq","Martinique", + "mr","Mauritania", + "ms","Montserrat", + "mt","Malta", + "mu","Mauritius", + "mv","Maldives", + "mw","Malawi", + "mx","Mexico", + "my","Malaysia", + "mz","Mozambique", + "na","Namibia", + "nc","New Caledonia", + "ne","Niger", + "nf","Norfolk Island", + "ng","Nigeria", + "ni","Nicaragua", + "nl","Netherlands", + "no","Norway", + "np","Nepal", + "nr","Nauru", + "nu","Niue", + "ny","Northern Cyprus", + "nz","New Zealand", + "om","Oman", + "pa","Panama", + "pe","Peru", + "pf","French Polynesia", + "pg","Papua New Guinea", + "ph","Philippines", + "pk","Pakistan", + "pl","Poland", + "pm","Saint Pierre And Miquelon", + "pn","Pitcairn", + "pr","Puerto Rico", + "ps","Palestine, State of", + "pt","Portugal", + "pw","Palau", + "py","Paraguay", + "qa","Qatar", + "re","Réunion", + "ro","Romania", + "rs","Serbia", + "ru","Russian Federation", + "rw","Rwanda", + "sa","Saudi Arabia", + "sb","Solomon Islands", + "sc","Seychelles", + "sd","Sudan", + "se","Sweden", + "sg","Singapore", + "sh","Saint Helena", + "si","Slovenia", + "sj","Svalbard And Jan Mayen", + "sk","Slovakia", + "sl","Sierra Leone", + "sm","San Marino", + "sn","Senegal", + "so","Somalia", + "sr","Suriname", + "ss","South Sudan", + "st","Sao Tome and Principe", + "sv","El Salvador", + "sx","Sint Maarten", + "sy","Syrian Arab Republic", + "sz","Swaziland", + "tc","Turks and Caicos Islands", + "td","Chad", + "tf","French Southern Territories", + "tg","Togo", + "th","Thailand", + "tj","Tajikistan", + "tk","Tokelau", + "tl","Timor-Leste", + "tm","Turkmenistan", + "tn","Tunisia", + "to","Tonga", + "tr","Turkey", + "tt","Trinidad and Tobago", + "tv","Tuvalu", + "tw","Taiwan, Republic Of China", + "tz","Tanzania, United Republic of", + "ua","Ukraine", + "ug","Uganda", + "um","United States Minor Outlying Islands", + "us","United States", + "uy","Uruguay", + "uz","Uzbekistan", + "va","Holy See (Vatican City State)", + "vc","Saint Vincent And The Grenadines", + "ve","Venezuela, Bolivarian Republic of", + "vg","Virgin Islands, British", + "vi","Virgin Islands, U.S.", + "vn","Vietnam", + "vu","Vanuatu", + "wf","Wallis and Futuna", + "ws","Samoa", + "xk","Kosovo", + "ye","Yemen", + "yt","Mayotte", + "za","South Africa", + "zm","Zambia", + "zw","Zimbabwe", + ] + + +FLAG_API_LINK = 'https://countryflags.io' + +FLAG_ERR_MSG = f'Failed to map country name, \ + See {FLAG_API_LINK}/#countries \ + for list of supported countries.' + +FLAG_SIZE = 32 # (32 | 64) +FLAG_STYLE = 'flat' # (flat | shiny) + +COUNTRY_NAMES = COUNTRY_DATA[1::2] +COUNTRY_CODES = COUNTRY_DATA[0::2] \ No newline at end of file diff --git a/app/marketing/views.py b/app/marketing/views.py index efd6e7c4966..9c5ae82427e 100644 --- a/app/marketing/views.py +++ b/app/marketing/views.py @@ -50,6 +50,8 @@ from marketing.management.commands.new_bounties_email import get_bounties_for_keywords from marketing.models import AccountDeletionRequest, EmailSubscriber, Keyword, LeaderboardRank from marketing.utils import delete_user_from_mailchimp, get_or_save_email_subscriber, validate_slack_integration +from marketing.country_codes import COUNTRY_NAMES, COUNTRY_CODES, \ + FLAG_API_LINK, FLAG_ERR_MSG, FLAG_STYLE, FLAG_SIZE from quests.models import Quest from retail.emails import ALL_EMAILS, render_new_bounty, render_nth_day_email_campaign from retail.helpers import get_ip @@ -790,6 +792,18 @@ def leaderboard(request, key=''): for tech in techs: technologies.add(tech) + flags = [] + if key == 'countries': + for item in items[0:limit]: + country = item.at_ify_username + code = 'us' + try: + country_index = COUNTRY_NAMES.index(country) + code = COUNTRY_CODES[country_index] + except: + print(f'Error: {FLAG_ERR_MSG}') + flags.append(f'{FLAG_API_LINK}/{code}/{FLAG_STYLE}/{FLAG_SIZE}.png') + if amount: amount_max = amount[0][0] top_earners = ranks.order_by('-amount')[0:5].values_list('github_username', flat=True) @@ -852,7 +866,9 @@ def leaderboard(request, key=''): next_update = timezone.now() + timezone.timedelta(days=1) context = { + 'key': key, 'items': items[0:limit], + 'dual_list': zip(items[0:limit], flags), 'nav': 'home', 'cht': cht, 'titles': titles, diff --git a/app/retail/templates/leaderboard.html b/app/retail/templates/leaderboard.html index d41df75d387..dc5a5ed3e78 100644 --- a/app/retail/templates/leaderboard.html +++ b/app/retail/templates/leaderboard.html @@ -153,12 +153,14 @@

{% trans "None found" %}

- {% for item in items %} + {% for item, flag in dual_list %} # {{forloop.counter}} {% if not is_linked_to_profile %} + {% elif key == "countries" %} + {% endif %}
{% if not is_linked_to_profile %}