Saturday, February 6, 2016

Getting started with web2py using pythonanywhere web hosting service (cloud)


This post tells you, how to deploy and execute python code written using web2py framework in
pythonanywhere web hosting service kind of cloud for python.
First write any sample code using web2py framework. Sample codes you can check my previous posts like getting started with web2py , blog app using web2py

Once you have completed the simple project, then we need to deploy the same using the cloud.
Web2py can be deployed any web hosting services, now we will look how we can deploy using https://www.pythonanywhere.com/




Click on Signup here! (if you are not sign up yet)




Click on Create a Beginner account and provide the required details, it is enough to post any details in internet.




After successful sign up and Login then, you can redirected to pythonanywhere


Click on DashBoard Then Click on Web




Now we need to create new web app (Click on Add anew web app).




Pythonanywhere will support so many python frameworks, Select the web2py , since we are implementing application using web2py.





Provide the password , which is required to access the application. It will created directory (/home/siva82k/web2py/) with my username.
Click on Next, which will create url for you


Now you can check your application through internet, usually welcome application will be copied to your account.
My case my url will be http://siva82k.pythonanywhere.com, if you try to click on this you can redirected to your application.




Now it’s time to deploy our existing code into pythonanywhere site. First go to our application, where we have written our code, click on pack all as shown in below image.




Then save the code in local system.Once it is completed then go to your pythonanywhere site.
Click on Administrative Interface, and the provide the password, which you have given while creating web2py account.



After successful Login, it will redirected to below page



We need to upload the file into python anywhere site . Provide the details under upload and install packed application
I am providing Application name as sivaweb2py
Upload a package from your local system, Earlier where you have downloaded.
Then click on install, our application got installed on pythonanywhere machine.
Now what ever we did in local host machine same thing available in pythonanywhere internet.
You can check my previous posts related to web2py examples.
Earlier we checked Role based access, same thing we will check in pythonanywhere
Click on https://siva82k.pythonanywhere.com/sivaweb2py/blog/view

It will ask the username and password , in my case I have provided user only (siva82k@gmail.com), have access to post the blog.
https://siva82k.pythonanywhere.com/sivaweb2py/blog/post



If we provide correct user name and password, it will take us to post the blog.



If we provide other details, other than post access then it will say you are not authorized.



One more example which we have worked earlier, basics to add the 2 numbers
https://siva82k.pythonanywhere.com/sivaweb2py/basics/request_args/10/20



We can test whatever we did in our previous examples in local system, same thing available in internet.
This is how we can deploy our web2py code using pythonanywhere.
Thanks for viewing this post








Sunday, January 31, 2016

State design pattern with java


State pattern, which allows objects to behave in different ways depending on internal state. State is used when you need a class to behave differently, such as performing slightly different computations, based on some arguments passed through to the class.

Problem: Vehicle different status details

1. VehicleNotificationStatus – having different status details about vehicle.
package com.siva;

public enum VehicleNotificationStatus {
 
 VEHICLE_ORDERED("Vehicle-Ordered"),
 VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD("Vehicle-Order-Submitted-For-Body-Build"),
 VEHICLE_BODY_SHOP_COMPLETED("Vehicle-Body-shop_completed"),
 VEHICLE_DELIVERED("Vehicle-Delivered"),
 INVALID("Invalid");
 
 VehicleNotificationStatus(String _type){
  this.type=_type;
 }
 private String type;
 

 public String getType() {
  return type;
 }
 public void setType(String type) {
  this.type = type;
 }

}

2. VehicleStatus interface to evaluate the different statues.
package com.siva;

  public interface VehicleStatus {
 
 public VehicleNotificationStatus evaluateVehicleStatus(String status);

  }

3. Different statues classes to implement the VehicleStatus interface.

VehicleOrdered is one type of status class is to implement VehicleStatus
package com.siva.status;

import com.siva.VehicleNotificationStatus;
import com.siva.VehicleStatus;

public class VehicleOrdered implements VehicleStatus{

 @Override
 public VehicleNotificationStatus evaluateVehicleStatus(String status) {
  System.out.println("VehicleOrdered state start");
    if(VehicleNotificationStatus.VEHICLE_ORDERED.getType().equalsIgnoreCase(status)){
    return VehicleNotificationStatus.VEHICLE_ORDERED;
  }
  return VehicleNotificationStatus.INVALID;
    
 }
 

}
2. VehicleForBodyBuild is one type of status class is to implement VehicleStatus
package com.siva.status;

import com.siva.VehicleNotificationStatus;
import com.siva.VehicleStatus;

public class VehicleForBodyBuild implements VehicleStatus{

 @Override
 public VehicleNotificationStatus evaluateVehicleStatus(String status) {
  System.out.println("VehicleForBodyBuild state start");
    if(VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD.getType().equalsIgnoreCase(status)){
    return VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD;
  }
  return VehicleNotificationStatus.INVALID;
    
 }

}
3. VehicleDelivered is one type of status class is to implement VehicleStatus
package com.siva.status;

import com.siva.VehicleNotificationStatus;
import com.siva.VehicleStatus;

public class VehicleDelivered  implements VehicleStatus{

 @Override
 public VehicleNotificationStatus evaluateVehicleStatus(String status) {
  System.out.println("VehicleDelivered state start");
    if(VehicleNotificationStatus.VEHICLE_DELIVERED.getType().equalsIgnoreCase(status)){
     System.out.println();
    return VehicleNotificationStatus.VEHICLE_DELIVERED;
  }
  return VehicleNotificationStatus.INVALID;
    
 }
}

4. Now we need to write Factory class which can be implement all this status details.
package com.siva;

import com.siva.status.VehicleDelivered;
import com.siva.status.VehicleForBodyBuild;
import com.siva.status.VehicleOrdered;

public class VehicleStatusFactory {
 
 public static VehicleStatus createStatus(VehicleNotificationStatus status){
  if(status.equals(VehicleNotificationStatus.VEHICLE_ORDERED)){
   return new VehicleOrdered();
  }
  else if(status.equals(VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD)){
   return new VehicleForBodyBuild();
  }
  else if(status.equals(VehicleNotificationStatus.VEHICLE_DELIVERED)){
   return new VehicleDelivered();
  }
  else
   return null;
 }

}

5. We have completed the all the classes related to State pattern, now we need to write test class to see the results.
package com.siva;

public class TestStateDesignPattern {
 
 public static void main(String[] args) {
  
  VehicleStatus vehicleStatus =VehicleStatusFactory.createStatus(VehicleNotificationStatus.VEHICLE_ORDERED);
  VehicleNotificationStatus notificationStatus = vehicleStatus.evaluateVehicleStatus(VehicleNotificationStatus.VEHICLE_ORDERED.getType());
  System.out.println(notificationStatus);
  vehicleStatus =VehicleStatusFactory.createStatus(VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD);
  VehicleNotificationStatus notificationStatus1 = vehicleStatus.evaluateVehicleStatus(VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD.getType());
  System.out.println(notificationStatus1);
  vehicleStatus =VehicleStatusFactory.createStatus(VehicleNotificationStatus.VEHICLE_DELIVERED);
  VehicleNotificationStatus notificationStatus2 = vehicleStatus.evaluateVehicleStatus(VehicleNotificationStatus.VEHICLE_DELIVERED.getType());
  System.out.println(notificationStatus2);
 }

}


Output:
VehicleOrdered state start
VEHICLE_ORDERED
VehicleForBodyBuild state start
VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD
VehicleDelivered state start

VEHICLE_DELIVERED




This is how state pattern will work.
Thanks for viewing this post.

Saturday, January 30, 2016

Role Based Access control using web2py framework


This post having how to provide Role based access to particular users. More information about web2py framework You can check my previous posts.
Getting started with web2py and blog app using web2py
Usually
Role Based Access control having

Account Registration
Providing Security
Adding to Groups
Access to Groups

and users can be

Administrator
Standard User
Manager

Now we can see how we will provide these roles using web2py

1. Start the web2py and provide the password an d start the server.

2. Click this link to open the web2py homepage http://127.0.0.1:8000/welcome/default/index




Click on the Admin link and provide the password.
Edit the sivaweb2py application




After edit, the application will be look like this





Click on the database administration tab which is under Models section, you can view the below page related to database table details




Now we are going to create role based access for exiting project, which is available in my previous post(blog app using web2py). Click the below link to open the exiting project results
http://127.0.0.1:8000/sivaweb2py/blog/view




Now we need to signup this blog and screen will look like as below.



After entering the details and click on signup then you will be redirected to view page. With message as Logged In




Now we have created user and logged in successfully, Now we can check in the database administrator for db details.





We can edit this record and we can see the details. Click on the 1 and see the details, password is encrypted.
Now we have to create auth group, allowing that only same group users only can post the topics in blog.
Go to Databaseadministrator under Models Section
Click on the db.auth_group
Click on the New Record and provide the Role as blog_users and description as you like




After submit the record table details look like below




Now we need to create authentication membership Click on db.auth_membership




Click on the New Record and assign the new memebership for this user and select the Group ID as blog_users





Now it’s time to provide access control
Edit the our existing blog.py.
If any user want to post requires authentication and who ever there under blog_users membership only can post the topics in blog.
@auth.requires_membership('blog_users')
Users are if login then only they can able to view the page.
@auth.requires_login()

# -*- coding: utf-8 -*-
# try something like
def index(): return dict(message="hello from blog.py")

@auth.requires_membership('blog_users')
def post():
    form =SQLFORM(db.blog).process()
    return locals()
@auth.requires_login()
def view():
    rows = db(db.blog).select(orderby=~db.blog.id)
    return locals()

def display_form():
   form = SQLFORM(db.blog)
   if form.process().accepted:
       response.flash = 'form accepted'
       redirect(URL('thanks'))
   elif form.errors:
       response.flash = 'form has errors'
   else:
       response.flash = 'please fill out the form'
   return locals()
def update():
    record = db.blog(request.args(0)) or redirect (URL(post))
    form = SQLFORM(db.blog,record)
    if form.process().accepted:
        response.flash = T('Record Updated')
    else:
        response.flash=T('Please complete the form')
    return locals()


After updating the blog.py with requires_membership and requires_login for post and view respectively, then

If we try to http://127.0.0.1:8000/sivaweb2py/blog/view or
http://127.0.0.1:8000/sivaweb2py/blog/post
Then it will redirected us to login page




Once we enter correct details after successful login , then it will redirected to blog post page or view page





Now I am going to create one more user, who does not have any privileges to post.




New user is not part of post_users group, If we try to click the below link
http://127.0.0.1:8000/sivaweb2py/blog/post

So it will redirect us to not authorized page.




This is how access control can be given to users and user groups using web2py.

Thanks for viewing this page....

Tuesday, January 26, 2016

AddToAny

Contact Form

Name

Email *

Message *