Friday 30 July 2021

Mamp Pro - What are my Mysql details for local connection.

 I couldn't find this easily by Googling, so I thought I'd leave this as a note to myself.  

This also applied to setting up a connection on Drupal 7 .  

What I needed was  Username 'root' and password 'root' .  I'd been using 'password' which had worked with something else I'd been working on recently . 

So here's what worked. 

Name: Mysite
Host: 127.0.0.1
Username: root
Password: root
Port: 8889



And in Drupal 7 ->


<?php

$databases['default']['default'] = array(
'database' => 'mysite',
'username' => 'root',
'password' => 'root',
'host' => 'localhost',
'port' => '3306',
'driver' => 'mysql',
'prefix' => '',
);

Wednesday 28 July 2021

How to add a Github personnel token on Mac OS Big Sur

 So when pushing to Github I've all of a sudden started to receive the following issue. 

remote: Password authentication is temporarily disabled as part of a brownout. Please use a personal access token instead.

remote: Please see https://github.blog/2020-07-30-token-authentication-requirements-for-api-and-git-operations/ for more information.

fatal: unable to access 'https://github.com/flowmoco/siaf.git/': The requested URL returned error: 403


The first thing you'll need to do is set up Personal Access Token in Github.  Following the instructions here to do that.   Add a Personal Access Token to Github

After that I have read that you can then open 'Keychain Access' and search for github. 


Next you should be able to right click that and delete it.   

Then you'll be able to 'git push' and you'll be asked for your Username and Token Access. 

However deleting the keychain for me didn't work.   If the same happens for you then this worked for me.  

Double click on the 'github.com' in the Name column. 

From here you can change the 'Kind' to 'access token' and the Password to the Keychain password that you've been given. 





Now when you go back to 'git push' it'll work :)

Mac OS M1 Chip and Docker issue - ERROR [internal] load metadata for docker.io/library/mysql:5.6

 So I expect you've found this post by looking for the Error. 

ERROR [internal] load metadata for docker.io/library/mysql:5.6

failed to solve with frontend dockerfile.v0: failed to create LLB definition: no match for platform in manifest sha256:

And then you've caught site of 'Mac OS M1' chip and thought 'Oh yeah I've got a Mac with this type of chip.   

So here's the thing; at the time of writing Docker does not fully support the M1 chip.  

You may want to make you have the right version of Docker For Apple Silicon M1 Chip

I managed to fix the issue above though with the following .  My code for the MySql container was 

mysql:
# image: mysql:5.6
build:
context: ./
dockerfile: ./Dockerfile-mysql
volumes:
- mysql-data:/var/lib/mysql:rw


and I changed this to 

mysql:
image: mysql:5.6
platform: linux/x86_64
build:
context: ./
dockerfile: ./Dockerfile-mysql
volumes:
- mysql-data:/var/lib/mysql:rw




Wednesday 21 July 2021

Wagtail - Disable deletion of Snippets that are assigned to Model

 This task ran me around in circles somewhat, until we found the solution which was literally 1 line of code. 
Hopefully if you've stumbled across this blog it may save you losing the time that I did on it. 


Let's have a closer look at the problem we're looking to fix here.  

So let's say we have a 'ElectricCarPage' and when creating the Page one of our fields is for 'Manufacturer' .  Which we are using Snippets for.   
On the  'ElectricCarPage'  we have this set as 'required' .  So one has to be added, however there is a way that the Administration could leave our Page without a Manufacturer ( and possible breaking the site ) .  And that would be to do through the Snippets admin and remove the Snippet here 


The Solution 
___________

So let's cut straight to the chase and give you the solution and then I'll go through the other methods that I'd been through and their gotchas. 

+ Add  'on_delete=models.PROTECT'  to the Manufacturer field in the 'ElectricCarPage' class.  Like

class CarPage(Page):
manufacturer = ForeignKeyField(
"car.Manufacturer", verbose_name=_("Manufacturer"), on_delete=models.PROTECT, related_name="manufacturer", required=True, help_text=_( "The car manufacturer " ), )





This solution gives you the above screen when you try and delete the Snippet. 



THE SOLUTIONS THAT DIDN'T WORK


FIX ATTEMPT 1


on_delete=models.RESTRICT

This is to be added in the GamePage model like so.


class CarPage(Page):
manufacturer = ForeignKeyField(
"car.Manufacturer", verbose_name=_("Manufacturer"), on_delete=models.RESTRICT, related_name="manufacturer", required=True, help_text=_( "The car manufacturer " ), )

However rather than the screenshot above after you try and delete it throws an error. FIX ATTEMPT 2

The next solution is to use the following code in the Developer Snippet

1def delete(self, *args, **kwargs): 2 CarPage = apps.get_model("cars", "CarPage") 3 carpages = CarPage.objects.filter(manufacturer=self) 4 if carpages: 5 print("@todo Let the user know that they can't delete!") 6 return 7 else: 8 super().delete(*args, **kwargs)

 

However I met another brick wall when trying to complete this and that’s in that I can output to a message as there is no request to use in this method. I really feel that there should be a way to resolve this method, so if you have one then please let me know, it'd be very useful for future development. FIX ATTEMPT 3

So I then thought I could use this code in a snippet hook .
Hooks — Wagtail Documentation 2.13.4 documentation

1from django.http import HttpResponse 2 3from wagtail.core import hooks 4 5@hooks.register('before_delete_snippet') 6def before_snippet_delete(request, instances): 7 # "instances" is a QuerySet 8 total = len(instances) 9 10 if request.method == 'POST': 11 # Override the deletion behaviour 12 instances.delete() 13 14 return HttpResponse(f"{total} snippets have been deleted", content_type="text/plain")

 

That hook works if you go through  'Admin>Snippets>Manufacturer' . However I also have a custom Admin menu items for ' Manufacturer' and if you navigate through that way the hook isn't called.

I think this is a real bug for Snippet Hooks. Even if you’re not using separate Admin menu items then whose not to say it may not be requested in the future.