Category Archives: Programming

Django Rest Framework, Testing ImageField

I start developing a web API with Django rest framework lately. So It was a little bit challenging for me to write the test for ImageField because I am not so familiar with Django environment.

So I just publish how I got over the problem. Maybe it helps someone out there.

In my case, It was a simple category app with photo field. All we need to do is populate the field with an image file but the trick is that the request format must be ‘multipart‘.

 

C# Web API 2 and AngularJS

angularIt has been almost a year that I have not posted anything to the blog so thanks to those life coach blog articles about “how to overcome laziness”! I have read bunch of them and then decided to write this out finally.

Anyway lets talk about web api 2, first of all this project was a weekend project and It is a very good example in order to learn web api 2 and basics of angular.

What does that project do? Basically I tried to create a micro accounting app has an stock support where the app user can perform Create/Read/Update/Delete (CRUD) operations for suppliers, products and link products to suppliers. After suppliers and products are created, app user can perform CRUD operations for purchase orders and link it with the suppliers but on the other hand there are customers which the app user can create sales orders  for them. So stock will be the difference of those purchase and sales orders for the products.

On the back-end since it is a .net MVC project;
My models are

  • Supplier
  • Product
  • PurchaseOrder
  • PurchaseOrderLine
  • User
  • SalesOrder
  • SalesOrderLine

I think the name of models are very clear. After creating the models I also tried to implement Repository Pattern for this project so I will not need to deal with different database types. There is good explanation at MSDN about the repository pattern :

” For example, if a client calls a catalog repository to retrieve some product data, it only needs to use the catalog repository interface. For example, the client does not need to know if the product information is retrieved with SQL queries to a database or Collaborative Application Markup Language (CAML) queries to a SharePoint list. Isolating these types of dependencies provides flexibility to evolve implementations. “

I did not want to engage with database in this project so I used List data type for seeding some test data into my project. You can see them in the TestDataHelper.cs file.

For the Repository pattern first of all I have created an interface that describes what my SupplierRepository class will do:

And then I implemented that Repository interface in the actual SupplierRepository class. As you will see below I load the table content in the constructor and do all the dirty stuff here for the data layer.

After doing so it comes to the controller. Under the controller folder there is only HomeController which extends the actual Controller class because I used that homecontroller for only calling appropriate view in the front-end but of course that could have done with a better way with separating each controller on its own.

The other controllers are only extends the APIController which lets us handle HTTP calls and create the logic. On the controller layer we have nothing left to do with domain model layer. It is time to create our app’s logic. Controllers initialize a SupplierRepository instance as a private variable in order to has the abilities of its repository class.

Well I think it is enough for the back-end lets talk about how things go on the front-end.

I used Bootstrap for the designing the interface and AngularJs 1.5.5 for performing CRUD operations on the API. It is easy to install these packages with nuGet. You can install them with

As I mentioned above in the HomeController I send all the actions to related view. So you can see my View files under the ~/Views/Home directory.  On the index you will see some buttons with the api’s endpoints you can reach the raw json data from here. I did not setup any authentication method because this is test project but you can reach a good tutorial of securing the web api from here you can also find a lot of tutorial on the google about securing the web api.

So lets continue with the angularJS you can see my angularJS controllers under ~/scripts/app folder. I have created the module, controller and the factory to use http service in angular. As you can see below we must initialize the Angular on the page with ng-app and then we need to specify our angular controller in the ng-controller attribute and then we can use angular freely in that code blocks.

AngularJS is very easy to write and understand. Its modular structure makes you write good code. Let’s see some example for the suppliers page controller for example.

On the controller side $scope represents our view’s attached page. We can add functions into scope to use for the actions. On the factory layer we isolated the api call’s from the logic. It uses the $http service of AngularJS in order to making calls to api back-end. You can find detailed AngularJS service here.

Screen shot of web api front-end

Screen shot of web api front-end

If you want to check out the code and run the application then you can find the code here on github. Feel free to contribute.
https://github.com/unicod3/WebApiExcercise

I also deployed the project on the free tier of azure server. I do not know how long it will last but the link to reach the project is below :
http://webapiexcercise.azurewebsites.net/

 

 

Android ile yönetilen Arduino; oyuncak araba

IMAG3595Elektronikte tabiri caizse artık popi bir kart olan arduino ile ilk çalışmamız gayet başarılı ve verimli geçti.
Android kullanarak geliştirdiğimiz bluetooth ile yönetilebilen araba, elektronik ve yazılımı birleştirdiğimiz basit ama güzel bir proje oldu. Ayrıca ekip olarak bir şeyler çıkartmak da bir o kadar zevkliydi.

IMAG3600Malzemeler sağdaki resimde görülebilir. İlk olarak bir adet motorlu oyuncak araba, diğerleri ise arduino uno R3, Arduino Bluetooth shield, Motor Shield, güç için 9v pil, arduino yu programlayabilmek için bir ara kablo, ihtiyaç durumda kabloları lehimleyebilmek için bir lehim makinesi, iki tane far için led.

Bu elemanların arasındaki elektronik bağlantıları şematik yolla çizmek yerine daha görsel bir şekilde çizmeyi tercih ettik, aşağıdaki resimde bağlantıları görebilirsiniz.

IMAG3612 Genel olarak projemizdeki arabanın bluetooth ile çalışmasının mantığı şu şekildedir; Android kısmında yazdığmız uygulama arduino ya bluetooth ile char türünde veri gönderir. Arduino ise bu veriyi bluetooth shield aracılığı ile karşılayarak gelen veriye göre işlem yapar.

Projenin android ve arduino kısmı için yazmış olduğum tüm kodlara buradan[BtManagerCar] erişebilirsiniz.

Yararlandığım kaynaklar;

Android in bluetooth kütüphanesinin kullanımı için kendi sitesinde bulunan açıklama gayet iyi;
http://developer.android.com/guide/topics/connectivity/bluetooth.html

Arduino motor shield için ise yine kendi sitesinde paylaşılan dökümantasyon ve örnek kod baya yararlı ;
http://www.arduino.cc/en/Main/ArduinoMotorShieldR3

Aşağıda da arabaya ait bir videoyu bulabilirsiniz.

Android; QRcode and barcode reader with zxing

Barcode ReaderIf you did any research on reading barcode and QRcode with Android then you must have seen zxing library which is opensource. The library does not simply read barcodes but also covers a lot of things, like which camera or layout we will use for capturing.

So I made a simple android application using zxing, my application is a  barcode reader, It just reads the barcode or QRcode and writes the data to the screen.

As you can see,  after creating an instance from IntentIntegrator. We just need to call initiateScan method and then boom, scaning starts!

After reading a barcode we can parse the result at “onActivityResult” like this

And you can find my android application’s codes here.

Check Domain

Python Domain Name Checker

I was really tired about checking whois websites to learn about a domain’s whois information. So I found a nice python library which is Python-Whois and it is under “Do What The Fuck You Want To Public License” then I decided to put an end to this misery for myself and created a GUI by using python-whois and pygtk libraries so that I can use it any time easily without open a browser from my desktop.

Well I must admit that my application’s appearance does not look well 😀 but I think it does the job. You can see the screenshots below.

Screenshot-Check Domain-1 Check Domain

Click here to go to my github account for the application, but it is still as py file so feel free to contribute. By the way I wrote that application with version 2.7 of python.

Happy coding…

 

 

USPS get multiple package rates

USPS-get multiple package rateI am currently working on a domestic shipping rate comparison project and one of the carriers that I need to get rates was USPS. In my project , we had more than just one package to get rate and this can turn into a real problem to deal with. In order to get rates in one shot, I went with the batch processing.

You can find USPS’ s API documentation here about getting rates. For the USPS rate API we don’t need to specify a password and note that We can only add packages up to 25 which makes our calls pretty fast.

Lets take a look at xml request example which is given here. All we need to do is using the production link and producing a valid xml structure for making request.

You can see the xml request for rate API below:

In response, we get this from USPS :

Multiple Package Thing

For getting multi-package rates from USPS, we only need to append a new “Package” item with a unique ID into the xml request, but as i mentioned above we can’t just keep on adding packages, limit is 25 package per request. So I end up writing something  like this in C#. In foreach loop it checks the limit and behave appropriate to the situation.

Here is the github link of the class.