Skip to content

Instantly share code, notes, and snippets.

View quevon24's full-sized avatar
🏠
Working from home

Kevin Ramirez quevon24

🏠
Working from home
View GitHub Profile
@quevon24
quevon24 / postgresql-set-id-seq.sql
Created July 3, 2024 21:17 — forked from henriquemenezes/postgresql-set-id-seq.sql
PostgreSQL set Next ID Sequence Value to MAX(id) from Table
-- Get Max ID from table
SELECT MAX(id) FROM table;
-- Get Next ID from table
SELECT nextval('table_id_seq');
-- Set Next ID Value to MAX ID
SELECT setval('table_id_seq', (SELECT MAX(id) FROM table));
@quevon24
quevon24 / fixtures.rake
Created April 13, 2024 15:27 — forked from ZachBeta/fixtures.rake
Rake task to create fixtures from test database in Rails 3.1
#put in lib/tasks/fixtures.rake
namespace :db do
namespace :fixtures do
desc 'Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override.'
task :dump => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_migrations"]
ActiveRecord::Base.establish_connection(:development)
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
@quevon24
quevon24 / rakefile_extract_fixtures.rb
Created March 28, 2024 23:50 — forked from deepak/rakefile_extract_fixtures.rb
generate fixtures from actual data in database
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
#!/bin/bash
set -e
# You must place all uncompressed bulk files in the same directory and set
# environment variable BULK_DIR, BULK_DB_HOST, BULK_DB_USER, BULK_DB_PASSWORD
# NOTES:
# 1. If you have your postgresql instance on a docker service, you need to mount
# the directory where the bulk files are, otherwise you will get this error:
# ERROR: could not open file No such file or directory
# 2. You may need to grant execute permissions to this file
from django.contrib.admin import ModelAdmin, register, SimpleListFilter
from django.db.models.functions import Length, StrIndex, Substr, NullIf, Coalesce
from django.db.models import Value as V
from .models import Item
class AlphanumericSignatureFilter(SimpleListFilter):
title = 'Signature (alphanumeric)'
parameter_name = 'signature_alphanumeric'
@quevon24
quevon24 / function.py
Created April 11, 2023 20:27
Django Function to remove characters from column, keep only number and cast it as integer
class ExtractInteger(Func):
"""Returns the first int value from the string. Note that this
requires the string to have an integer value inside.
Example of usage:
YourModel.objects.filter(something=True).annotate(integer_value=ExtractInteger("field_name")).order_by("-integer_value")
"""
function = "REGEXP_MATCH"
@quevon24
quevon24 / state_names.py
Created March 1, 2023 19:59
Python list with names of US states
us_state_names = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware",
"Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
"South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
"West Virginia", "Wisconsin", "Wyoming"]
@quevon24
quevon24 / factory.py
Created February 2, 2023 22:57
Pass data on the fly to factory_boy factory using custom RelatedFactoryList class
import copy
from factory import RelatedFactoryList
class RelatedFactoryVariableList(RelatedFactoryList):
"""This factory allows you to specify how many related objects do you
want to create, but also allows you to define the data for each related
object.
@quevon24
quevon24 / _object_history_list.html
Last active January 11, 2023 18:53
django-pghistory revert object from admin integration
{% load i18n %}
{% load admin_urls %}
{% load getattribute from pghistory %}
<table id="change-history" class="table table-bordered table-striped">
<thead>
<tr>
<th scope="col">{% trans 'PGH ID' %}</th>
{% for column in history_list_display %}
<th scope="col">{% trans column %}</th>
@quevon24
quevon24 / admin.py
Created January 6, 2023 16:29 — forked from leisurelicht/admin.py
Make all fields readonly for Django Admin
class OpsIPInfoAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
# make all fields readonly
readonly_fields = list(
set([field.name for field in self.opts.local_fields] +
[field.name for field in self.opts.local_many_to_many])))
if 'is_submitted' in readonly_fields:
readonly_fields.remove('is_submitted')