Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
In normal git flow in case you have to checkout a file you would use:
git checkout -- your_file_name

In case you want to checkout multiple files you give other file names in front of checkout --. Sometimes there are bunch of modified files that you want to checkout all of them at once, so you need to go to the root of the project and:
git checkout -- .

It will checkout all files in your project.

You can also use:
git reset --hard

It will reset your working directory and replace all changes (including the index).

#git #checkout #reset #hard
If you want to comment a block of code out in Jinja2 template, use {# ... #}. It can also span into multiple lines:
{#
<tr class="">
<td colspan="2" style="font-family:Ubuntu,Helvetica,Arial,sans-serif; padding: 0px 0px 0px 20px;" class="">
Regards,
</td>
</tr>
<tr class="">
<td colspan="2" style="font-family:Ubuntu,Helvetica,Arial,sans-serif; padding: 0px 0px 30px 20px;" class="">
SYSTEM INFO
</td>
</tr>
#}

#python #jinja2 #comment #comment_out
It has been set to my routine task to copy folder/files from my local host or a remote linux server to another server or vise versa. scp linux command is used for this specific need.
The general command is:
scp YOUR_LOCAL_FILE USERNAME@YOUR_SERVER:~

The above command will copy a file from your local machine to a remote server. If you want to copy a folder to a remote machine user -r to recursively copy the whole folder into the remote.

You can also copy from your remote server to your local machine by changing the order in scp as below:
scp USERNAME@YOUR_SERVER:~/YOUR_REMOTE_FILE .

The above command will copy the remote file called YOUR_REMOTE_FILE from the home directory to your current path (.). Instead of dot you can provide your full path.

NOTE: use man scp and see tons of flags to master this command.

#linux #sysadmin #scp #copy
jinja2 has tons of filters for strings, lists, etc that can be used to make the process of, um well, filtering simpler.

To apply a filter you can use pipe symbol (|). In order to lower case`/`upper case a string:
{{"SOMETHING"|lower}}
{{"sOmThing"|upper}}

Or let's say getting the max number from a list:
{{ [1, 2, 3]|max }}

The filters are endless, see below for more:
http://jinja.pocoo.org/docs/2.10/templates/#filters

#python #jinja2 #template #filter #uppercase #lowercase #max
This media is not supported in your browser
VIEW IN TELEGRAM
📺 نسخه جدید تلگرام با امکانات جذابی مانند ارسال مجموعه عکس، فیلم به صورت یک مطلب و pin کردن پیام در کانال و... منتشر شد.
@Farsna
For enabling push notification on pushd server, you need to get a file with .p12 extension and .cer certificate file. For pushd to work you need to generate a .pem file and give its path in push configuration(`/etc/pushd/pushd.conf`).

We need to generate 2 pem files one called apns-cert.pem (generated from .cer file) and the other called apns-key.pem (generated from .p12 file).

To generate .pem file use openssl command, with the format below:
openssl pkcs12 -in YOUR_KEY.p12 -out apns-key.pem -nodes
NOTE: it may ask you for the password, enter the given password by whom that gave you the p12 file.

-in set your input file name and -out sets your output file name which will be generated.

And now generate the key pem file:
openssl x509 -in cert.cer -inform DER -outform PEM -out apns-cert.pem

Restart the pushd and check for any error in /var/log/pushd.

#pushd #openssl #p12 #cer #pem #push
lowercase/uppercase a function, variable or a string in PyCharm by using:
OS X: Command+Shift U

#pycharm #lowercase #uppercase
Download a static file from a remote URL by using requests in python:

# author -> Roman Podlinov
def download_file(url):
local_filename = url.split('/')[-1]
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return local_filename

NOTE: stream=True parameter is used for returning large files in chunk and save it into a file

#python #requests #stream #large_file #download
Modern_Linux_Administration.epub
2.3 MB
Modern Linux Administration
by Sam R. Alapati”

#ebook #epub #book #linux #sysadmin
To find all of users ids:

ps -eo uid


#linux sysadmin #ps #users #uid
Django tutorials will be published today or the day after, sorry for the delay. 🙈
In order to create a debian package from your shell script do as follow:

Let's consider our package name is dangling. We need to create 2 folders, first is called dangling and the second is DEBIAN which is inside of the dangling folder:
mkdir helloworld && mkdir helloworld/DEBIAN

You can copy the files into your package with the full paths on the destination filesystem. E.g. if you want to put a file in /usr/local/bin/ you put it in dangling/usr/local/bin/:
mkdir -p dangling/usr/local/bin
cp /usr/local/bin/dangling.sh dangling/usr/local/bin/

In your DEBIAN directory (created at the begining of the post), create a control file with the below content:
<span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Package: dangling
</span><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Version: 0.1
</span><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Maintainer: Alireza Hoseini
</span><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Architecture: all
</span><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Description: would wipe all docker dangling volumes out

NOTE: These are the mandatory fields in the control file.

Now build the package using dpkg-deb:
dpkg-deb --build dangling

This creates a dangling.deb file, which you can now install on any Debian installation with following command:
dpkg -i dangling.deb

#debian #deb #create #package #dpkg #build
In case you want to add a custom font to OS X, you need to go to /Library/fonts and paste your custom fonts in this folder. No need to restart or logout.

reference: https://support.apple.com/en-us/HT201722

#apple #osx #font #fonts
Remove all stopped containers.
docker rm $(docker ps -a -q)

#docker #rm #ps
DJANGO

Let's do a little bit more by fetching data from database and pass them to views in Django.

For now we make a change in polls/views.py in index function to get data from Question model and return it as an HTTP response object. At first let's import the model:
from .models import Question


And now in index function write the below code:
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)


The code above gets the last 5 questions and on the second line question_text fields for those que
stions are joined together by comma and finally returned as an HTTP response.

Now if you head over to http://127.0.0.1:8000/polls/ you would see the result of question texts bound together by co
mma.

#python #django #query #django_part9
Here we aim to design templates and render them to interact with Django the way it likes! We try to isolate the page's design.

Create a folder called templates in polls directory (this is the default folder name where django will look for app templates). Inside of templates create polls directory and inside of it create index.html file. So your templates are in path polls/templates/polls/index.html.

We have polls inside of templates folder to render templates in the format of loader.get_template('polls/index.html').

NOTE: if you don't create polls subdirectory inside of templates and you have templates we the same name in different apps (in the same project) you would encounter bizarre problems. DON'T DO THAT! (Django will choose the first template it finds whose name matches)

Now put the below code in polls/templates/polls/index.html:
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}


OK now do as follow in index function in polls/views.py for template rendering:
from django.http import HttpResponse
from django.template import loader
from .models import Question


def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {
'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context, request))


Note that we have imported loader from django.template.

As you can see above, we have rendered the template with our variables (context) and passed it to http response object. We have used django.template module to render templates.

I've got the below result (next picture) what about you?

#django #loader #render #templates #django_part10