Python Pyrad dictionary error - python

I setup some RADIUS backend to allow AD authentication via the 'admin' of django. Alltough i got a problem with some dictionaries, i really don't know what i'm doing wrong. This is the error i got:
IOError at /admin/
Errno 2] No such file or directory: '/home/pl/dictionary.compat'
I installed pyrad, so it should be there and i've got a 'dic't file in the following style (/home/pl/dict):
#
# Version $Id: dictionary,v 1.1.1.1 2002/10/11 12:25:39 wichert Exp $
.....
$INCLUDE dictionary.compat # compability issues
$INCLUDE dictionary.acc
$INCLUDE dictionary.ascend
$INCLUDE dictionary.bay
....
The code i use in the RADIUS backend:
srv = Client(server=settings.RADIUS_SERVER,
secret=settings.RADIUS_SECRET,
dict=Dictionary("/home/pl/dict"))
Any ideas?

The $INCLUDE directive in the configuration files is intended to add definitions from another dictionary file. Unless the extra dictionary files are found, the dictionary object cannot be created.
My advice is:
- if you don't have the extra dictionary files: comment out/remove the $INCLUDE lines
- if you have the extra dictionary files: copy them to the right location

Related

Name error when trying to import API key from .env

I am trying to store my API Keys in a .env file
I created the file as a File containing settings for editor file type. Stored my APIKeys
TWILIO_ACCOUNT_SID=***
TWILIO_AUTH_TOKEN=***
TWIML_APPLICATION_SID=***
TWILIO_API_KEY=***
TWILIO_API_SECRET=***
Installed decouple, imported and used config to retrieve my API tokens in my settings.py file
from decouple import config
...
TWILIO_ACCOUNT_SID = config(TWILIO_ACCOUNT_SID)
TWILIO_AUTH_TOKEN = config(TWILIO_AUTH_TOKEN)
TWIML_APPLICATION_SID = config(TWIML_APPLICATION_SID)
TWILIO_API_KEY = config(TWILIO_API_KEY)
TWILIO_API_SECRET = config(TWILIO_API_SECRET)
I am however getting the error message:
TWILIO_ACCOUNT_SID = config(TWILIO_ACCOUNT_SID)
NameError: name 'TWILIO_ACCOUNT_SID' is not defined
You don't need to use the decouple library to read your environment variables.
Firstly download the .env plug-in supporter for PyCharm (if that's what you're using)
https://www.codestudyblog.com/cs2112pyc/1224021812.html
This will allow you to set and get variables from your file. Make sure your configuration has the correct .env file set.
my .env file has the variable set to:
TWILIO_ACCOUNT_SID=SUPER SECRET KEY
Then all you need to is:
import os
twilio_key = os.environ.get('TWILIO_ACCOUNT_SID')
print(twilio_key)
>>>SUPER SECRET KEY
Process finished with exit code 0

FileNotFoundError: A json file not found after pushing it to heroku

I am working on a website. For this website I have to create a table which shows the time of the meeting and the participants of it. I am writing the code of site using Django. For the table I have a data on a .json file. When I run the server locally it works fine and when I push it to heroku it raises FileNotFoundError. I found this question which faces with the similar problem but the answer shows how to log something, but I need .json file for data. My django app directory:
66 admin.py
100 apps.py
<DIR> migrations
60 models.py
3,332 schedule_of_teams.json
<DIR> static
<DIR> templates
63 tests.py
333 urls.py
807 views.py
So I have a view function in views.py file:
def schedule_of_teams(request):
with open('./schedule_of_teams.json',encoding='utf-8') as f:
context = json.load(f)
context['title'] = 'Yarış cədvəli'
return render(request,'schedule_of_teams.html',context)
And open context manager raises the error:
FileNotFoundError at /schedule_of_teams/
[Errno 2] No such file or directory './schedule_of_teams.json/'
So why is this happening? Why it works fine when I run python manage.py runserver with localhost but when I push to heroku it raises this exception? Is heroku ignores .json files? By the way, I do not have *.json on my .gitignore file it commits the file.
What I have tried:
I tried to change the directory of the file like moving it to a folder data/ or firstly instead of open('./schedule_of_teams.json) I wrote open('schedule_of_teams.json') but that didn't work either.
I am literally so confused I asked this question in Python Discord Server but got no respond. One of the solutions is creating a database but I do not want to do it, since this data is just a list which has 16 dictionaries and each has 4 keys.

GAE: Read static file

I'm using app engine and I don't know how to read a static file from my project. I have a file structure that globally looks like this:
- html (static folder)
- staticfile1.html
- staticfile2.html
- script
- main.py
app.yaml
In my app.yaml I already set the application_readably attribute to true for the html dir:
- url: /html
static_dir: html
application_readable: true
I tried several methods to access the staticfile1.html but either of them returns this error:
[Errno 13] file not accessible: u'/html/staticfile1.html'
At the moment my code looks like this:
INDEX_HTML = open('/html'+self.request.path).read()
self.response.out.write(INDEX_HTML)
I hope someone knows how I can read the static file.
Thanks in advance.
You'll probably need to use relative paths based on the current module's __file__ attribute. e.g. from main.py, you'd do something like:
import os
_HERE = os.path.basename(__file__)
_HTML_DIR = os.path.join(_HERE, os.pardir, 'html')

Cannot use environment variables for settings in Django

In trying to find a place to store and save settings beyond settings.py and the database, I used an environment.json for environment variables. I import these in settings.py.
My problem is that when I try to change or store new values in my environment, env, settings.py does not notice the change - perhaps because the time and number of times settings.py is read by Django.
Is there a way I would be able to use my environment variables the way I want like attempted below?
# settings.py
import json
with open('/home/dotcloud/environment.json') as f:
env = json.load(f)
EMAIL_HOST = env.get('EMAIL_PORT', '500')
# views.py
import json
def site_configuration(request):
with open('/home/dotcloud/environment.json') as f:
env = json.load(f)
if request.method == 'POST':
os.environ['EMAIL_PORT'] = request.POST['email_port']
return render(request, ...)
# python manage.py shell demo
>>> import json
>>> with open('/home/dotcloud/environment.json') as f:
... env = json.load(f)
...
>>> project_settings.EMAIL_PORT
'500'
>>> env['EMAIL_PORT']
Traceback (most recent call last):
File "<console>", line 1, in <module>
KeyError: 'EMAIL_PORT'
>>> env['EMAIL_PORT'] = "123"
>>> env['EMAIL_PORT']
'123'
>>> project_settings.EMAIL_PORT
'500'
>>> project_settings.EMAIL_PORT == env['EMAIL_PORT']
False'
And if not, how else could I store changeable settings that are retrieved by settings.py somewhere in my Django project?
You might want to look into foreman (GitHub) or honcho (GitHub). Both of these look for a .env file in your current directory from which to load local environment variables.
My .env looks like this for most projects (I use dj-database-url for database configuration):
DATABASE_URL=sqlite://localhost/local.db
SECRET_KEY=<a secret key>
DEBUG=True
In your settings.py file, you can load these settings from os.environ like this:
import os
DEBUG = os.environ.get('DEBUG', False)
If there are required settings, you can assert their presence before trying to set them:
assert 'SECRET_KEY' in os.environ, 'Set SECRET_KEY in your .env file!'
SECRET_KEY = os.environ['SECRET_KEY']
I've been using this method of handling local settings for the last few projects I've started and I think it works really well. One caveat is to never commit your .env to source control. These are local settings that exist only for the current configuration and should be recreated for a different environment.
I see the question changed slightly, the original answers are still below but this one has a slightly different answer:
First, make sure you are using the right settings.py (print 'This file is being loaded' should do the trick).
Second, personally I would advise against using json files for config since it is less dynamic than Python files, but it should work regardless.
My recommended way of doing something like this:
create a base_settings.py file with your standard settings
create a settings.py which will be your default settings import. This file should have a from base_settings import * at the top to inherit the base settings.
If you want to have a custom settings file, dotcloud_settings.py for example, simply add the from dotcloud_settings import settings (or base_settings) and set the environment variable DJANGO_SETTINGS_MODULE to dotcloud_settings or your_project.dotcloud_settings depending on your setup.
Do note that you should be very careful with importing Django modules from these settings files. If any module does a from django.conf import settings it will stop parsing your settings after that point.
As for using json files, roughly the same principle of course:
Once again, make sure you don't have anything that imports django.conf.settings here
Make all of the variables within your json file global to your settings file:
import json
with open('/home/dotcloud/environment.json') as f:
env = json.load(f)
# A little hack to make all variables within our env global
globals().update(env)
Regardless though, I'd recommend turning this around and letting the settings file import this module instead.
Also, Django doesn't listen to environment variables by default (besides the DJANGO_SETTINGS_MODULE so that might be the problem too.

How do I set up gaeunit 2.0a with my Django app?

I am trying to set up Google App Engine unit testing for my web application. I downloaded the file from here.
I followed the instructions in the readmen by copying the directory gaeunit into the directory with the rest of my apps and registering 'gaeunit' in settings.py. This didn't seem sufficient to actually get things going. I also stuck url('^test(.*)', include('gaeunit.urls')) into my urls.py file.
When I go to the url http://localhost:8000/test, I get the following error:
[Errno 2] No such file or directory: '../../gaeunit/test'
Any suggestions? I'm not sure what I've done wrong. Thanks!
You can copy url but in your urls.py include
(r'^test', include('gaeunit.urls')),
Also you will have to change test path in gaeunit.py
_LOCAL_DJANGO_TEST_DIR = 'test'
And in your main web test files under test folder include following line for django test setup
self.application = django.core.handlers.wsgi.WSGIHandler()
According to the instructions on the main page you should add the URL to app.yaml, not urls.py.
- url: /test.*
script: gaeunit.py

Categories