Thursday 20 August 2020

Adding Mobile Phone Check and UK Landline check together.

 In the current project I'm looking at I have two input fields for telephone numbers. 

1. UK Mobile Phone Number
2. UK Landline Number 

They each have their own regex checks .  


It's been requested that we should have just the one input field; this could be a UK landline OR UK Mobile .  So I'd like to merge the 2 regexes together.   Which is real easy but I found googling the exact answer very difficult.   So I thought I'd note it here. 

So here's the two seperate Regex's

Regex for UK Mobile Numbers. 

^(\+44\s?7(\d ?){3}|\(?07(\d ?){3}\)?)\s?(\d ?){3}\s?(\d ?){3}

Regex for UK Landlne Phone Numbers 

^\(?0( *\d\)?){9,10}$

So to use both we can add the OR statement which is '|' 

And to group the two sides together using the '(' and ')' 

which gives us the following combined statement. 

(^(\+44\s?7(\d ?){3}|\(?07(\d ?){3}\)?)\s?(\d ?){3}\s?(\d ?){3})|(^\(?0( *\d\)?){9,10}$)





Monday 17 August 2020

Calling API from another Docker container returning a 'Cannot assign requested address' Error

This article is to document an issue I had recently that was caused by having two docker installation on my local computer and using them as API’s to each other. Doing this means you cannot call localhost like we would have previously. I found the issue quite hard to search for because at first I was unaware it was the Docker containers that were at fault. At first I had Drupal 7 website running locally and it would call my API ( Django ) , which was on another server.

However it made sense for development to have both things on my local environment; they would both be Docker installations. On doing so I'd set the Django API up on http://localhost:8000/ which I could see worked and could connect to using Postman. However when I tried to make the request through Drupal I would get the error 'Cannot assign requested address' Basically You cant use localhost for something within a container…

Each container thinks it is an entire server in its own right, and sees ‘localhost’ as only the container, and not the host. To fix this I did the following . On the Django ( API ) side open docker-compose.yml

Add "host.docker.internal" to the list of ALLOWED_HOSTS

for example.

1 - 'ALLOWED_HOST={"hosts": ["localhost", "api.localhost", "api.docker.localhost", "host.docker.internal"]}'

This was diagnosed by viewing the logs of the fafeapi container with docker-compose logs -f fafeapi  and seeing:
Also thank you to this post https://medium.com/it-dead-inside/docker-containers-and-localhost-cannot-assign-requested-address-6ac7bc0d042b

Wednesday 5 August 2020

Getting my Wagtail / Django project working on PythonAnywhere

On PythonAnywhere you have some instructions on how to set up a Django project; for the record these are. 

In brief, the differences are:

  • The version of Django that you've written your code for might not be the version we have installed by default; the best way to use the exact version you want is to set up a virtualenv.
  • manage.py runserver and localhost:8000 won't work on PythonAnywhere, because our console servers aren't accessible from the outside world.
  • Instead, go to the Web tab (using the button near the top right) and start a new web app using manual configuration. Click through to the link to edit your wsgi file and uncomment the section for Django. From then on, your Django site will be live at your-username.pythonanywhere.com.
  • You'll need to hit the Reload button to reload your web app, whenever you want to see the effects of code changes on your site.

That's ultra-brief overview. For detailed instructions, check out the Following the Django Tutorial on PythonAnywhere wiki page.


And after Googling for 'How to set up a Wagtail Project on Python Anywhere' I found the following instructions on Github https://github.com/texperience/wagtail-pythonanywhere-quickstart

Using these articles the following procedure worked for me.   My project here by the way is called 'sb_goals' 


>> Open a bash console on PythonAnywhere 

mkvirtualenv wagtail-django-project --python=/usr/bin/python3.7

>> Clone the site into my project. 

git clone https://github.com/deejayM/django_sb_goals.git


cd mysite


pip install django


Next up is to install any Requirements which I didn’t have .

pip install -r requirements.txt


However I didn’t have a requirements.txt file created, so I needed to go and create one of those first off. 

And you can do this from your root folder ( that has the manage.py file in it ) with the following :



pip freeze > requirements.txt

Then add that file to PythonAnywhere; I opened up the file using VIM and pasted the generated code in.  

vim requirements.txt 


i ( to insert in VIM )
ctrl + v ( to paste )
ESC
:wq

At this point you can now hopefully run Migrate command.


python manage.py migrate



I had a few issues here; which I believe were caused by me hitting disk quota limits.  This following command will show you how and where your resources are being used up.

du -hs /tmp ~/.[!.]* ~/* /var/www/


Which in my case showed me that my .cache folder was pretty big. To clear it use

rm -rf ~/.cache


NOTE :  I've had additional similar issue on disk Quota limits since and have written some note on this here https://littleripplesproject.blogspot.com/2020/09/python-anywhere-issue-with-disk-space.html


The above issue in turn resulted in seeing the following type of errors when re-running the migrate.  The list was much longer than this , but this is an example


python anywhere modulenotfounderror: no module named 'crispy_forms'

ModuleNotFoundError: No module named 'rest_framework'


You can resolve these with the following, not that some needed to pre preceeded by 'django-' and some you just enter the module name.

pip install django-crispy_forms

Finally I was able to continue with . 

python manage.py migrate

python manage.py createsuperuser

Back in the PythonAnywhere console we need to create a New Web App . 



We need hange the 'virtualenv' to the one we've set up, this is done in the Web App section on the control panel . 

This way our account will see all the Installed packages. 




We'll also need to add our URL to ALLOWED HOSTS in the settings file, we can edit that with VIM . 

mysite/settings/base.py


vim settings.py
i
# TO BE ADDED SOMEWHERE

ALLOWED_HOSTS = ['mysite.pythonawhere.com']

[ESC]
:wq!





Here are some screenshots of the help instructions from the PA website. 





Further Troubleshooting : 



Here's an example of my WSGI file that works. 

# This file contains the WSGI configuration required to serve up your # web application at http://aroundaboutslaundry.eu.pythonanywhere.com/ # It works by setting the variable 'application' to a WSGI handler of some # description. # # The below has been auto-generated for your Django project import os import sys # add your project directory to the sys.path project_home = '/home/myaccountname/myappprojectname' if project_home not in sys.path: sys.path.insert(0, project_home) # set environment variable to tell django where your settings.py is os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings.base' # serve django via WSGI from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
One thing to notice here is that os.environ['DJANGO_SETTINGS_MODULE'] is set to 'mysite.settings.base'.
Whereas Django projects will have this set to 'mysite.settings' . This is because Wagtail has the settings in base.py in the Settings folder. Next Issue .

Error: The SECRET_KEY setting must not be empty

To fix this I opened mysite/settings/base.py file and added 

SECRET_KEY = "myuniquesecretkey1234"


Next Issue . 


raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry
The solution for this is to run 

python manage.py collectstatic

Although doing so for me gave me another 'disk quota exceeded' message. 

You could go back to the section above where you remove some more unneeded packages from your installation.  But for me I decided to pay the monthly fee, as I'd been spending too much time setting up. 


>> After doing so and 'reloading' my Web app.  I know have a site that I can see :)





Tuesday 4 August 2020

1. ModuleNotFoundError: No module named 'pip._internal.cli.main'

When changing my IDE halfway through a project I got this error message when trying to 'runserver' in Terminal.

'ModuleNotFoundError: No module named 'wagtailmenus''

So I tried 
'pip install wagtailmenus' 

And received the following message. 

'
ModuleNotFoundError: No module named 'pip._internal.cli.main''


These are the commands I used to sort this out. 

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3 get-pip.py --force-reinstall pip uninstall pip python3 get-pip.py
pip install wagtailmenus
Now I can

python3 manage.py runserver

And my site is now working locally .

AWS lightsail for Django and Wagtail project.

I was looking for somewhere to host my code so that I could use it from wherever I want.  Just for myself at this stage and as AWS were offering a free month.   So I thought I’d review it here.
Unfortunately though I didn’t get to the point I wanted to with it , so have now ditched it.   I think it’d be worth other developers considering my issues and finding resolutions for these before you end up wasting your time as well.
https://aws.amazon.com/getting-started/hands-on/deploy-python-application/
and the first steps here.  
https://www.youtube.com/watch?v=-rO67fTzVBc

The initial part of setting up a Django project on Lightsail was really straightforward, using the instructions here. 

1. Create a Lightsail instance

2. Deploy a project - I deployed my own custom Django code here. 

3. Using the ‘connect SSH’ I could git clone the project I was working on from Github

4.However for the moment I had to disable everything ‘Wagtail’ in my project to get it working. 

5. In the instruction above and following ‘Test the application’    


At this point I can get a website that I can see and access from anywhere using


‘cd /opt/bitnami/apps/django/django_projects/django_sb_goals/ && python3 manage.py runserver 0.0.0.0:8000”


However only while I have the SSH window open on my computer, once I shut this down it stops working.


The solution for which I was hoping would be by following the instructions ‘Host the application using Apache ‘ .  I reviewed these instructions a number of times and could not get them to work.


I also spent the best part of a working day Googling and searching for a solution .  With no success.


The other problem - Getting Wagtail to work .  As I could not ‘pip install wagtail’ I would have needed to change a fair bit of code.  Considering this and the first issue, and that I’d already wasted a lot of Dev time I decided to abandon using AWS lightsail as my hosting solution.


I’d be interested to find out the resolutions to these issues if anyone knows why I struggled, or other hosting recommendations.