Skip to content

Instantly share code, notes, and snippets.

View gpichot's full-sized avatar

Gabriel Pichot gpichot

View GitHub Profile
@gpichot
gpichot / install.md
Last active September 20, 2022 15:10
npm install --save-dev jest ts-jest jest-environment-jsdom @testing-library/react @testing-library/react-hooks @testing-library/user-event @testing-library/jest-dom
@gpichot
gpichot / gitlabcilint.zsh
Created December 27, 2019 10:21
Gitlab ci lint zsh
function gitlabcilint() {
data=$(cat .gitlab-ci.yml | node -e 'fs=require("fs");console.log(JSON.stringify({content:fs.readFileSync(0,"utf8")}))')
curl --header "Content-Type: application/json" https://gitlab.com/api/v4/ci/lint --data "${data}"
}
@gpichot
gpichot / exceptions.py
Last active December 12, 2018 12:36
Add error codes in django rest framework for form validation
from rest_framework.views import exception_handler
from rest_framework.exceptions import ErrorDetail, _get_full_details
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
@gpichot
gpichot / test_django_endpoint_from_shell.py
Created September 21, 2018 13:22
Request Django Rest Framework view response from shell with authentication
u = User.objects.get(username=username)
from rest_framework.test import APIRequestFactory, force_authenticate
factory = APIRequestFactory()
request = factory.get(url, format='json')
force_authenticate(request, u)
view = MyView.as_view()
@gpichot
gpichot / rails_simple_form_array_many_select_input.rb
Last active September 19, 2018 09:19
Simple Form input to generate multiple selects
# frozen_string_literal: true
# To use with a ActiveRecord array column for instance:
# add_column :myobjects, :features, :integer, array: true, default: []
#
# Based on https://tenforward.consulting/blog/integrating-an-array-column-in-rails-with-simple-form
class ArraySelectInput < SimpleForm::Inputs::CollectionSelectInput
def input(wrapper_options = nil)
label_method, value_method = detect_collection_methods
@gpichot
gpichot / django_settings_dev_ngrok_conf.py
Created September 12, 2018 08:24
Find a ngrok tunnel running locally and add it to the the django conf (iOS/Android development)
import requests
import urllib
ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
# Try to find a runnning ngrok instance
try:
tunnels = requests.get('http://127.0.0.1:4040/api/tunnels')
url = tunnels.json()['tunnels'][0]['public_url']
ngrok_domain = urllib.parse.urlparse(url).netloc
@gpichot
gpichot / rest.service.ts
Created January 2, 2016 11:42
Convert object to request body
return data.map((value, index) => {return index + '=' + value)).join('&');