Working Ninja
2017-08-24T07:47:57
# Simple List
', '.join([author.name for author in authors])

# Django
', '.join([author.name for author in book.author_set.all()])

Both will output "Author Name, Author 2 Name, ..., Author x Name"

2017-07-19T10:46:37

Let's say you add an element to the DOM via jQuery:

$("<button class="delete">Delete</button>").appendTo("foo");

And want to later remove it via a jQuery event. Calling $('.delete').on('click') will not fire the event. This is because .on() only looks for elements that were loaded initially with the DOM. To resolve you can use .live() for older version of jQuery or if you're using later versions of jQuery (1.7+), the following:

$(document).on("click", ".delete", function() { ... });

Since "document" existed when the DOM loaded a click on it will fire an event. Furthermore, since we specified the ".delete" CSS class for the selector arguement for the .on() event, the event will search through its descendents to find any elements with the ".delete" CSS class and execute our function.

Sources:
https://stackoverflow.com/questions/4900802/jquery-click-not-working-after-adding-element-to-the-dom
http://api.jquery.com/on/

2017-07-12T19:50:17

Using IUS (Inline with Upstream Stable)--a nifty repository provided by EPEL--we can upgrade beyond PHP 5.4 by first removing our current version of PHP, installing our preferred version (5.6 in our case), and then restarting Apache:

$ sudo yum remove php-cli mod_php php-common

$ sudo yum install mod_php56u php56u-cli php56u-mysqlnd

$ sudo apachectl restart

Sources:
https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-centos-7
https://ius.io/GettingStarted/

2017-06-30T21:58:27

Renewing an SSL but can't recall exactly how it was filled out last time? Try the following:

openssl req -text -in request.csr

The above command will decode the CSR file and output its contents in human readable text. Enjoy!

More here: https://linux.die.net/man/1/req

2017-05-29T10:58:45

I just started my first Django project with Python 3 in earnest. One of the first things I have noticed is that reload isn't available by default from the Django shell. It needs to be imported:

>>> from importlib import reload
>>> reload(<module>)

This became a pretty tedious process and didn't always reload the module as expected. So, instead of reloading the shell and the module to run a command, I moved the commands that I was running into a custom Django management command. No more reloading =)

2017-04-26T18:20:11

The following code allows us to monkey patch the json() method that is returned by requests.post().

import requests
from mock import MagicMock


@mock.patch.object(requests, 'post')
def my_function(self, post):
    attrs = {'json.return_value': {'id': 1}}
    post.return_value = MagicMock(**attrs)

    r = requests.post()
    r.json()['id']

Source: https://docs.python.org/dev/library/unittest.mock.htmll#unittest.mock.NonCallableMock

2017-04-03T20:16:40
from sqlalchemy.sql import text
from sqlalchemy import create_engine

import secrets


engine = create_engine(
    'mssql+pyodbc://{}:{}@MSSQL'.format(
        secrets.username, secrets.password
    )
)
conn = engine.connect()

s = text("SELECT * FROM users WHERE name = :name")
result = conn.execute(s, name=name).fetchall()
print result

For further information on installing and configuring unixODBC (requirement for pyodbc) on Linux:
https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-RHEL-or-Centos

Connecting to Microsoft SQL Server with pyodbc:
http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#dialect-mssql-pyodbc-connect

2017-03-31T22:33:13
function sendWebhook($data) {
    $url = 'https://url/';
    $jsonDataEncoded = json_encode($data);
    $webhook = curl_init($url);
    curl_setopt($webhook, CURLOPT_POST, 1);
    curl_setopt($webhook, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($webhook, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $result = curl_exec($webhook);
}

$data = array(
    'key' => 'value'
);

sendWebhook($data);

Source: http://thisinterestsme.com/sending-json-via-post-php/

2017-03-23T17:32:14

Sometimes it is necessary to resize partitions (e.g. updates to GitLab require additional space on /opt). Here's what we need to do. If you have an LVM parition, you're in luck! Things are pretty straightforward.

  1.   Run vgdisplay (Volume Group Display) to verify we have space to expand our partition.
    •     The line PE Free / Size should have space available.
  2.   Next, add space to the parition with lvresize (Logical Volume Resize):
    sudo lvresize --size +100M /dev/mapper/opt
  3.   Now that the volume has been expanded, let's commit those changes to the filesystem:
    sudo xfs_growfs /dev/mapper/opt

Profit!

2017-03-23T17:22:08

Here's a simple way to count lines of code via the command line that I recently used for a Django project:

find . -name '*.py' -not -path "*/migrations/*" | xargs wc -l

or

wc -l **/*.py

Source: http://stackoverflow.com/questions/1358540/how-to-count-all-the-lines-of-code-in-a-directory-recursively#1358573