New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix of TypeError: string indices must be integers, not str #923
Conversation
The get_country_statistic("") method is returning a string. To access the 'localitities' key, we should first convert the string to a dictionary - context['locality_count'] = get_country_statistic("")['localities'] + context['locality_count'] = json.loads(get_country_statistic(""))['localities']
LGTM |
this indeed is an error, however we should focus on fixing the main cause of this error seems like the root cause of this error is that the as a quick fix, maybe we could just create the ideally, we would probably do something like:
|
I agree, I sent this quick fix, as I was trying to quickly send this fix.
I would be glad to work on that point if you give me enough pointers on how to proceed, what to update/change, etc... And talking about deploying a local version of the project, I tried this. You'll let me know what you think. |
for this pull request you could just update it in a way to create for other issues we should connect in the https://gitter.im/healthsites/healthsites and have a broader discussion about the development/deployment setup in the project |
This is where I am: updating the file else:
# get cache
filename = os.path.join(
settings.CLUSTER_CACHE_DIR,
'world_statistic'
)
try:
file = open(filename, 'r')
data = file.read()
output = json.loads(data)
except IOError as e:
try:
# cluster cache directory does not exists, create it
os.makedirs(settings.CLUSTER_CACHE_DIR)
# query for each of attribute
healthsites = get_heathsites_master()
output = get_statistic(healthsites)
output = json.dumps(output, cls=DjangoJSONEncoder)
file = open(filename, 'w')
file.write(output) # python will convert \n to os.linesep
file.close() # you can omit in most cases as the destructor will call it
output = json.loads(output)
except Exception as e:
pass You mentioned the idea of using Django system checks, so I tried this:
import os
from django.core.checks import register, Tags, Info
from django.conf import settings
@register(Tags.compatibility)
def check_cluster_cache_dir_is_installed(app_configs, **kwargs):
"Check that cluster cache dir is created."
checks = []
if not os.path.exists(settings.CLUSTER_CACHE_DIR):
msg = ("The cluster cache directory does not exists. This folder is necessary for ... "
"You can create one from the command line as eg: `mkdir cache`.")
checks.append(Info(msg, id='localities.I001'))
return checks
from __future__ import absolute_import
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
from . import checks The displayed warning will be like:
I guess, the 2 can't go togheter. If we create the cluster cache directory in case it's missing, there is no need for the system check. And if we do the system check, we should let the user create the cluster cache directory himself. What do you think? |
you are on the right track, but maybe we should be careful when creating the directory, the statement
Regarding your other comments, I would first like to have a broader discussion on how to handle basic project setup and update the documentation. So for this pull request just focus on fixing the initial problem and don't add anything else. |
Haha, I was on the way :) https://github.com/healthsites/healthsites/pull/927/files |
fixed by #927 |
The get_country_statistic("") method is returning a string. To access the 'localitities' key, we should first convert the string to a python dictionary