Thursday, 14 January 2021

TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_4__.auth is not a function

 So I got this issue while following a tutorial on how to set Authentication on a Firebase + Vue project here https://www.youtube.com/watch?v=XtbYBoKb2zY

On the section about setting up the register page.  

ERROR

Setting up the Register page as Following is giving me an error.

1 2 3 4 5 6 7 8 9 10 11 12 13 export default { methods: { async pressed(){ try { const user = firebase.auth().createUserWithEmailAndPassword(this.email) console.log(user) this.$router.replace({name: "secret"}) } catch(err){ console.log(err) } } },
1 2 3 4 5 6 7 8 9 10 11 12 13 # ERROR TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_4__.auth is not a function at _callee$ (Register.vue?0103:24) at tryCatch (runtime.js?96cf:63) at Generator.invoke [as _invoke] (runtime.js?96cf:293) at Generator.eval [as next] (runtime.js?96cf:118) at asyncGeneratorStep (asyncToGenerator.js?1da1:3) at _next (asyncToGenerator.js?1da1:25) at eval (asyncToGenerator.js?1da1:32) at new Promise (<anonymous>) at eval (asyncToGenerator.js?1da1:21) at VueComponent.pressed (Register.vue?0103:22)


The issue was actually in my main.js where I had

1 import firebase from "firebase/app"

I changed this to

1 import firebase from "firebase/app"

and it worked. :)

Tuesday, 5 January 2021

Improving my PythonAnywhere Deployment

At the moment I have a disconnected Deployment process.  Where the site I’m looking at locally does not reflect what is on live.

So let’s make a note of some techniques that could improve my flow. 


A staging site - although my hosting package doesn’t support having a staging area but I could use another PythonAnywhere free account as a staging area.

Using Fabric to automate deployment.

Some reading material on this.

http://www.jeffknupp.com/blog/2013/12/18/starting-a-django-16-project-the-right-way/

https://www.pythonanywhere.com/forums/topic/1067/


The information in the blog above that I’m interested in is.

  1. Automated deployment and testing (using Fabric)
  2. Automatic database migrations (using South)



Also I found this youtube video on deploying on PythonAnywhere which looks pretty good. 


https://www.youtube.com/watch?v=Y4c4ickks2A - this is good for deployment but doesn’t have anything on automated future deployment. 


Use cloud flare for the domain and point the name record 

Thursday, 3 December 2020

Django how to limit the Template view depending on the Users Groups.

 What we need to do is check the Users group list from inside the ' if user.is_authenticated' statement in the template. And to do this we need to harness the power of Django's 'template tags'

Create the folder and following pages in your main app folder - in my case ‘mysite’

1 2 3 -- templatetags ---- __init__.py ---- mysite_templatetag.py

__init__.py will be a blank file .

in mysite_templatetag.py I have .

1 2 3 4 5 6 7 from django import template register = template.Library() @register.filter(name='has_group') def has_group(user, group_name): return user.groups.filter(name=group_name).exists()

 

And then in the template base.html

1 2 3 4 5 6 7 {% load mysite_templatetag %} ... {% if request.user|has_group:"Coordinator" %} <li class="nav-item"> <a class="nav-link" href="{% url 'users:user_list_view' %}">Your Surveyors</a> </li> {% endif %}


According to some instructions that should be it; however I was getting the error


template tags '%_extras' is not a registered tag library. must be one of: …


To solve this .

  1. The server will need to be rebooted.

  2. Make sure the files are in the right place

  3. I added the following to my settings.py ‘TEMPLATES’ object.



    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'mysite','templates'), ], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], 'libraries':{ 'mysite_templatetag': 'mysite.templatetags.mysite_templatetag', } }, }, ]



Reebooted the server and now it works.

Monday, 30 November 2020

Django restricting a Class Based View page depending on a User attribute / Group etc.

The following code is an example of how you can make it so users get redirected depending on conditions taken from their User model . 


This code is to go in a Class Based View. 

1 2 3 4 def dispatch(self, request, *args, **kwargs): if request.user.username == 'david.smith': return redirect('home_page') return super().dispatch(request, *args, **kwargs)



This example would redirect the user 'david.smith'.  This next example would redirect if the User does not belong to the group that we want. 


def dispatch(self, request, *args, **kwargs): if request.user.groups.filter(name = 'Coordinator').exists(): return super().dispatch(request, *args, **kwargs) return redirect('home_page')


Monday, 23 November 2020

PythonAnywhere Internal Server Error ager '/favicon.ico' call added.

 After adding my Favicon pack to the Static files in PythonAnywhere ( using the 'Files' link; and then adding the files in mysite/static folder ) 
And then when accessing the site the next time I was seeing the following message on the homepage. 


Internal Server Error


On checking the error.log ( go to > Webb > Scroll down to 'Error.log' ) 
I could see the message ' Internal Server Error: /favicon.ico' 


So my next port of call was to check that the favicon.ico could be seen at the same URL ( relatively ) as the one that I have working on my local server.  It was working though, so no issue there. 


So my next port of call was to check that the favicon.ico could be seen at the same URL ( relatively ) as the one that I have working on my local server.  It was working though, so no issue there. 

After revisiting the issue I realised it was the message above the 'Internal Server Error' that was most useful . 


raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

ValueError: Missing staticfiles manifest entry for 'images/favicon.ico'

2020-11-23 14:39:05,951: Internal Server Error: /favicon.ico


Missing staticfiles manifest entry in Django issue


Which led me to adding the following change which worked. 

OPEN 
/mysite/settings/base.py

FIND

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'


and changed this to . 

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'



It might not be recommended but for the small site I'm working on this fix will be fine.  And felt it's worth noting for anyone else who stumbles over the same issue. 

Monday, 16 November 2020

Visual Studio Code - "go to definition" not working .

 This was happening on VSC "Version: 1.51.1" 

For example if I was to create the following test.py code 

<code>

def my_function():
  print("Hello from a function")

my_function()

</code>

Then I should be able to right click 'my_function' and get an option 'go to definition' which will take to to the above function.  Unfortunately I was getting a 'no definition' available message. 

To fix this open VSC . 

use Command + Shift + P ( Mac ) 








Search for 'settings.json' 

And add this to your code 

<code>

{
// Defines type of the language server.
"python.jediEnabled": false,
"python.languageServer": "Microsoft"
}

<code> 

Restart VSC .  This then resolved my issue for the code example given above.

 
However I discovered another issue 'Visual Studio Code unresolved import python ' 

Which I would get when clicking on imported code , for example if I clicked on 'selenium' here. 

from selenium import webdriver

The solution for me here was to click on 'Python 3.8.2 64-bit' and replace it for another one. 


Monday, 21 September 2020

Python Anywhere issue with Disk Space Quota Limit.

The official help page for this on Python can be found at https://help.pythonanywhere.com/pages/DiskQuota/

Here's my notes on what I've tried so far. 

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


4.0K /home/mysite/.bashrc4.0K /home/mysite/.gitconfig4.0K /home/mysite/.profile4.0K /home/mysite/.pythonstartup.py4.0K /home/mysite/.viminfo4.0K /home/mysite/README.txt4.0K /tmp8.0K /home/mysite/.local8.0K /home/mysite/.vimrc17M /home/mysite/.cache160M /home/mysite/aroundaboutslaundry321M /home/mysite/.virtualenvs


An easy fix is if the Cache is bloated. 

rm -rf ~/.cache

rm -rf ~/.cache/*

Or lets clean up Unused files. 

rm -rf /tmp/*
In my case this neither of these helped.    


CLEAN UP ANY UNUSED VIRTUALENVS

How see old virtual environments ( see what virtualenvs I have) 



lsvirtualenv

Running this command showed me that I only had the one Environment working . 




Uninstall python packages you don't need anymore

To see what packages you have take a look at your requirements.txt page or run 

pip list

I then ran through a handful that I believed I'm not using ; although I may need to install them again at a later date. 

To remove here is an example


pip uninstall django-cripsy-forms

And then Remove from requirements.txt.  



From here I could now install the package I wanted, which was 'wagtail' 

pip install wagtail