Skip to content

Instantly share code, notes, and snippets.

View darwing1210's full-sized avatar

Darwing Medina darwing1210

  • Handraise
  • Austin, TX
  • 19:56 (UTC -05:00)
  • LinkedIn in/dmedina93
View GitHub Profile
@darwing1210
darwing1210 / async_download_files.py
Last active February 2, 2024 13:56
Script to download files in a async way, using Python asyncio
View async_download_files.py
import os
import asyncio
import aiohttp # pip install aiohttp
import aiofile # pip install aiofile
REPORTS_FOLDER = "reports"
FILES_PATH = os.path.join(REPORTS_FOLDER, "files")
def download_files_from_report(urls):
@darwing1210
darwing1210 / bulk_users_from_csv.py
Last active April 19, 2022 08:29
Django command to create users from a CSV file
View bulk_users_from_csv.py
import csv
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth import get_user_model
from django.utils.crypto import get_random_string
class Command(BaseCommand):
"""manage.py import_users --csv='/Users/user/usuariosweb.csv' --encoding='iso-8859-1'"""
help = 'Imports users based on a CSV file'
@darwing1210
darwing1210 / reset_app.py
Last active February 15, 2018 22:58
Django Command to reset apps tables
View reset_app.py
from io import StringIO
from optparse import make_option
from django.apps import apps
from django.core.management.base import AppCommand, CommandError
from django.core.management.color import no_style
from django.core.management import call_command
from django.db import connections, transaction, DEFAULT_DB_ALIAS
@darwing1210
darwing1210 / social_api_template_tags.py
Created February 12, 2018 16:58
Django Template tags to get posts from Facebook, Twitter, Instagram and YouTube.
View social_api_template_tags.py
import urllib
import json
import twitter
from datetime import datetime
from django.conf import settings
from django import template
register = template.Library()
@darwing1210
darwing1210 / views.py
Last active October 2, 2017 17:04
Some useful Django ajax views
View views.py
class JSONResponseMixin(object):
'''
This is a slightly modified version from django-braces project
(https://github.com/brack3t/django-braces)
'''
content_type = None
json_dumps_kwargs = None
def get_content_type(self): # pragma: no cover
return self.content_type or "application/json"
@darwing1210
darwing1210 / validators.py
Created May 12, 2017 22:19 — forked from jrosebr1/validators.py
Validator for files, checking the size, extension and mimetype for Django.
View validators.py
# @brief
# Performs file upload validation for django. The original version implemented
# by dokterbob had some problems with determining the correct mimetype and
# determining the size of the file uploaded (at least within my Django application
# that is).
# @author dokterbob
# @author jrosebr1
import mimetypes
@darwing1210
darwing1210 / migration_file.py
Created February 11, 2017 17:05
Example migraton file to autogerate slug from existing django models
View migration_file.py
# -*- coding: utf-8 -*-
# Generated by Django 1.9.9 on 2017-02-11 16:54
from __future__ import unicode_literals
from django.db import migrations, models
from django.template.defaultfilters import slugify
def migrate_docs_forward(apps, schema_editor):
documents = apps.get_model("documents", "Documents")
@darwing1210
darwing1210 / wordpress_fabfile.py
Last active August 9, 2019 23:29
A Python Fabfile to deploy a wordpress site over SSH
View wordpress_fabfile.py
import os
from time import time
from fabric.api import sudo, run, task, env, cd, get, local
from fabric.context_managers import shell_env
env.hosts = ['yourhost']
env.user = 'yoursshuser'
env.password = 'yoursshpassword'
# env.key_filename = '~/yourkey.pem'