Thursday, June 25, 2015

[Django] Changing field widget when when using inlines

For example, if you create a SiteOption model to extend Django's core sites framework you may want to add multiple inlines from one model (SiteOption) for grouping options, like:

General Options
Theme Options
Security Options
SEO Options

"Show me the code!"

Wednesday, June 17, 2015

[Django] Using cloud storage services as default storage backend

To use cloud storage services as default storage backend in Django – or actullay in Python, Apache's Libcloud wrapper can be a nice solution. Because it supports more than 30 providers, and has a huge community.

django-storages-redux app provides storage backends to use in Django. Good news, also supports Apache Libcloud. So you can use more than 30 cloud providers with Django. To install the app execute this:
$ pip install django-storages-redux
Also you must install Apache Libcloud:
$ pip install apache-libcloud
And enable from settings.py by adding following lines:
DEFAULT_FILE_STORAGE = 'storages.backends.apache_libcloud.LibCloudStorage'
DEFAULT_LIBCLOUD_PROVIDER = 'google_storage'
LIBCLOUD_PROVIDERS = {
 'google_storage': {
 'type': 'libcloud.storage.types.Provider.GOOGLE_STORAGE',
 'user': 'yourcloudstorageapikey:)',
 'key': 'yourcloudstorageapisecretkey:)',
 'bucket': 'default-bucket-name',
 }
}
 If you don't create bucket, you can create from shell:
$ ./manage.py shell

$ from storages.backends.apache_libcloud import LibCloudStorage
$ LibCloudStorage('google_storage').driver.create_container('default-bucket-name')

Wednesday, June 10, 2015

Detecting if an image is really an image

About Technique

HTTP protocol has really cool methods such as HEAD. HEAD method allows you to get only head data instead of head and body. Head data contains mimetype of the body, so we can rely on it to detect if it's really an image. Let's get it with JavaScript...

The Code

Sync Example


Async Example


As you can see, you can even detect huge images very quickly. ;)

Originally posted at https://coderwall.com/p/qdg71a/detecting-if-an-image-is-really-an-image

Sunday, April 19, 2015

Using Nginx on Mac OSX

To install Nginx on your Mac you can use simply brew command:
$ brew install nginx 
Default Nginx server name is localhost with 8080 port. So you can see default Nginx page by entering http://localhost:8080 address. Let's change something:

Changing default server name and port
Nginx.conf located at /usr/local/etc/nginx/nginx.conf dir. Open it with your favorite editor, to me the Vim:
$ vim /usr/local/etc/nginx/nginx.conf
 You'll see default server configuration like this:
server {    listen 8080;    server_name locahost;
    ## other things}
Change them with what you want and save the file. To reload Nginx configuration you should restart Nginx by calling these:
$ sudo nginx -s stop$ sudo nginx 
Including multi server defination from different files
Aww, cool topic. To keep different server definations in different files, you should need to this. Open nginx.conf, locate your cursor to the end of http defination block and simple paste this two lines:
$ include /usr/local/etc/nginx/conf.d/*.conf;
$ include /usr/local/etc/nginx/sites-enabled/*.conf;
Now your can create symlinks to these directories. Of course, don't forget to restart Nginx!

Saturday, February 21, 2015

[Django] Farkli App Icerisindeki Model'leri Ayni Grupta Gosterebilmek

Django admin'de birbiri ile alakali olan iki farkli app'iniz mevcutsa, bu modelleriniz asagidaki gibi gorunecektir:

[App 1]
- Model 1
- Model 2
[App 2]
- Model 3
Ancak siz bu goruntuyu istemiyor, asagidaki gibi bir goruntu istiyorsunuz:
[App 1]
- Model 1
- Model 2
- Model 3 
Hosgeldiniz! Yapilmasi gereken islem, App 2 altinda bulunan Model 3'un Meta sinifinda o modelin app'inin aslinda App 1 oldugunu soylemek. Onu da boyle yapiyoruz:



Onemli Guncelleme:
Bu degisikligin etkileri sandiginizdan daha buyuk olabilir. Bu islem tam anlami ile Django'nun app mimarisindeki app'inizin adinin degistirilmesi anlamina geliyor. Yani varsayilan olarak app'inizin adi olusturulan klasor ayni iken bunun degistirilmis olmasini sagladik. Buna ek olarak Python'un package yapisi yine klasor adi uzerinden calisacaktir.

Bu guncellemeyi yapmama neden olan case, bu app adini degistirdigim zaman bu Model 3'un Model 2'de Foreign key olarak tanimlandigi yerde hata almaya basladim. Bunun sebebi olaraki su tanimlamaydi:


Artik app adimiz 'app_1' olduguna gore buradakini de gistirmek zorundayiz. Bu durumda modelin refresh edilecegini de uzuntuyle belirtmem gerekiyor. :(

Saturday, January 3, 2015

Yazar Olma Sorunsali

Yaklasik bir bucuk sene once buyuk paylasim umutlari ile actigim bloguma ozellikle son bes aydir yazamadigimi fark ettim. Draft olarak birkac paragrafi yazilmis 18 adet yazim bulunmaktaymis. Bunlari sirasiyla tamamlayip daha fazla yazmak niyetindeyim. Yazi yazmak uretkenligimi arttirdigini hatirliyorum. Bunun ustune gidecegim. Hatta yazmanin faydalari da bir konu olarak olabilir! Takipte kalin, degerli gunluk bir tane olan ziyaretcilerim! 21 Subat '14 guncellemesi: (Yazamadi)

Thursday, July 31, 2014

[DevOps][Nginx] fastcgi_pass Problems

If you are getting error about fastcgi_pass option like this:
2014/07/31 08:08:39 [error] 3536#0: *6 connect() failed (111: Connection refused) while connecting to upstream, client: 95.95.95.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
or this:
2014/07/31 08:25:07 [crit] 3644#0: *23 connect() to unix:/tmp/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 95.95.95.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/tmp/php5-fpm.sock:", host: "localhost"
You have probably setted wrong or passive TCP or socket address. You can learn PHP's TCP or socket addsress from /etc/php5/fpm/pool.d/www.conf file. Check lines that starts with these:
listen = 127.0.0.1:9000 ;listen = /tmp/php5-fpm.sock
Destroy ; char from starting, and have fun! Don't forget to restart your PHP or your another service -- like this $ service php5-fpm restart!