Get API in Django for Category, Subcategory and Sub Subcategory in nested form

@api_view(['GET'])

def fetchCategory(request):

category = []

sub_category = []

sub_sub_category = []

category_list = []

row_sql = ''' select t.*, (case when t1.parent_id is not null then 'sub_sub_category' when t1.category_id is not null then 'sub_category' else 'category' end) as level from master_category t left outer join master_category t1 on t.parent_id = t1.category_id order by coalesce(t1.parent_id, t1.category_id, t.category_id), coalesce(t1.category_id, t.category_id), t.category_id'''

for c in Category.objects.raw(row_sql):

if c.level == 'category':

category.append({"category_id": c.category_id, "status": c.status, "name": c.name, "slug": c.slug,

"parent_id": c.parent_id, "level": c.level})

elif c.level == 'sub_category':

sub_category.append({"category_id": c.category_id, "status": c.status, "name": c.name, "slug": c.slug,

"parent_id": c.parent_id, "level": c.level})

else:

sub_sub_category.append({"category_id": c.category_id, "status": c.status, "name": c.name, "slug": c.slug,

"parent_id": c.parent_id, "level": c.level})

for c in category:

sc_data = []

for sc in sub_category:

if sc['parent_id'] == c['category_id']:

sc_data.append(

{"category_id": sc['category_id'], "parent_id": sc['parent_id'], "name": sc['name'],

"slug": sc['slug'],

"status": sc['status']})

scc_data = []

for scc in sub_sub_category:

if scc['parent_id'] == sc['category_id']:

scc_data.append(

{"category_id": scc['category_id'], "parent_id": scc['parent_id'], "name": scc['name'],

"slug": scc['slug'],

"status": scc['status']})

category_list.append({"category_id": c['category_id'],

"name": c["name"], "sub_category": sc_data, "sub_sub_category": scc_data})

data = {"category": category, "sub_category": sub_category, "sub_sub_category": sub_sub_category}

return Response({"data": category_list, "error_code": 0})

View More...

Get API in Django for Category, Subcategory and Sub Subcategory in Parallel way

@api_view(['GET'])

def fetchSingleCategory(request):

category_list = []

row_sql = f"SELECT t.category_id, t.parent_id, t.name, t.slug, t.status " \

f"from master_category t where t.parent_id is NULL"

cl = scl = []

for c in Category.objects.raw(row_sql):

row_sql = f"SELECT t1.category_id, t1.parent_id, t1.name, t1.slug, t1.status from master_category" \

f" t inner join master_category t1 on t1.parent_id = t.category_id where t1.parent_id={c.category_id}"

j=0

for sc in Category.objects.raw(row_sql):

cl.append(sc.category_id)

row_sql2 = f"SELECT t2.category_id, t2.parent_id, t2.name, t2.slug, t2.status from master_category t inner join master_category t1 on t1.parent_id = t.category_id inner join master_category t2 on t2.parent_id = t1.category_id where t2.parent_id={sc.category_id}"

i=0

for ssc in Category.objects.raw(row_sql2):

scl.append(ssc.category_id)

category_list.append({

"category_id":c.category_id,

"parent_id":c.parent_id,

"name":c.name,

"status":c.status,

"sub_category":{ "category_id": sc.category_id,

"parent_id": sc.parent_id,

"name": sc.name,

"status": sc.status

},

"sub_sub_category":{"category_id": ssc.category_id,

"parent_id": ssc.parent_id,

"name": ssc.name,

"status": ssc.status

}

})

i+=1

if i ==0:

category_list.append({

"category_id": c.category_id,

"parent_id": c.parent_id,

"name": c.name,

"status": c.status,

"sub_category": {"category_id": sc.category_id,

"parent_id": sc.parent_id,

"name": sc.name,

"status": sc.status

},

"sub_sub_category": {}

})

if j ==0 :

category_list.append({

"category_id": c.category_id,

"parent_id": c.parent_id,

"name": c.name,

"status": c.status,

"sub_category": {},

"sub_sub_category": {}

})

return Response({"data": category_list, "error_code": 0})

View More...

Advance SQL Functions

 #1: BIN() Function:

The BIN() function returns a binary representation of a number, as a string value.

Ex: SELECT BIN(15);

Result: 1111

#2: BINARY() Function:

The BINARY function converts a value to a binary string

Ex: SELECT BINARY "tech-question.com";

Result: tech-question.com

Ex: SELECT BINARY "Vishavjeet" = "VISHAVJEET";

Result: 0

#3: CASE() Function:

CASE statement goes through conditions and return a value when the first condition is met (like an IF-THEN-ELSE statement).

So, once a condition is true, it will stop reading and return the result.

If no conditions are true, it will return the value in the ELSE clause.

If there is no ELSE part and no conditions are true, it returns NULL.

Ex: SELECT product_id, Product_quantity as Quantity,

CASE WHEN Product_quantity > 50 THEN 'The product quantity is greater than 50'

WHEN Product_quantity = 50 THEN 'The product quantity is 50'

ELSE 'The product quantity is under 50'

END AS QuantityText

FROM products;

Result:

Product_id Quantity QuantityText

10248 12 The product quantity is under 50

10248 10 The product quantity is under 50

10248 5 The product quantity is under 50

10249 9 The product quantity is under 50

10249 50 The product quantity is greater than 50

10250 10 The product quantity is under 50

10250 55 The product quantity is greater than 50

10250 15 The product quantity is under 50

#4: CAST() Function

The CAST() function converts a value (of any type) into the specified datatype.

Ex: SELECT CAST("2020-02-18" AS Time);

Result: 00:20:17

dataType: DATE,DATETIME, DECIMAL, TIME, CHAR, NCHAR, SIGNED, UNSIGNED, BINARY

#5: COALESCE() Function

The COALESCE() function returns the first non-null value in a list.

Ex: SELECT COALESCE(NULL, NULL, 'tech-question.com', 'python-ds.com', NULL, 'tech-overflow.com');

Result: tech-question.com

#6: CONV() Function

The CONV() function converts a number from one numeric base system to another, and returns the result as a string value.

CONV(number, from_base, to_base)

Ex: SELECT CONV(15, 10, 2);

Result: 1111

#7: CONVERT() Function

The CONVERT() function converts a value into the specified datatype or character set.

Ex: SELECT CONVERT("2021-02-18" AS Time);

Result: 2021-02-18

>SELECT CURRENT_USER();

root@%

>SELECT DATABASE();

pythondb

>SELECT IF(500<1000, "YES", "NO");

YES

>SELECT IFNULL(NULL, "tech-question.com");

tech-question.com

>SELECT ISNULL(NULL);

1

>SELECT LAST_INSERT_ID();

5

>SELECT NULLIF(25, 25);

>SELECT SESSION_USER();

root@68.183.92.201

>SELECT SYSTEM_USER();

root@68.183.92.201

>SELECT USER();

root@68.183.92.201

>SELECT VERSION();

5.6.44

View More...

How to Create Multi level category hierarchy in SQL

select t.*,

(case when t4.parent_id is not NULL then 5

when t4.category_id is not null then 4th_level_subcategory

when t3.category_id is not null then 3rd_level_subcategory

when t2.category_id is not null then 'sub_sub_category'

when t1.category_id is not null then 'sub_category'

else 'category'

end) as level

from master_category t left outer join

master_category t1

on t.parent_id = t1.category_id left outer join

master_category t2

on t1.parent_id = t2.category_id left outer join

master_category t3

on t2.parent_id = t3.category_id left outer join

master_category t4

on t3.parent_id = t4.category_id

order by coalesce(t4.parent_id, t4.category_id, t3.category_id, t2.category_id, t1.category_id, t.category_id),

coalesce(t4.category_id, t3.category_id, t2.category_id, t1.category_id, t.category_id),

coalesce(t3.category_id, t2.category_id, t1.category_id, t.category_id),

coalesce(t1.category_id, t.category_id),

t.category_id

View More...

CCVenue Payment Transactional API integration in PHP CodeIgniter

Production API URL:-https://api.ccavenue.com/apis/servlet/DoWebTrans

Staging API URL:- https://apitest.ccavenue.com/apis/servlet/DoWebTran


public function ccaVenueStatusSuccess()

{

$this->load->library('ccvenuecrypto');

$txnid=$orders['txnid'];

$time=3*60; // 3 minute

if(isset($_REQUEST['time']) && $_REQUEST['time']>0)

$time=$_REQUEST['time'];

sleep($time);

$working_key='XXXX';//Shared by CCAVENUES

$access_code='XXXX';//Shared by CCAVENUES

$merchant_json_data = array('order_no' => $order_id);

// $merchant_json_data = array('txnid' => $txnid);

$merchant_data = json_encode($merchant_json_data);

$encrypted_data = $this->ccvenuecrypto->encrypt($merchant_data, $working_key);

$final_data = 'enc_request='.$encrypted_data.'&access_code='.$access_code.'&command=orderStatusTracker&request_type=JSON&response_type=JSON';

// https://apitest.ccavenue.com/apis/servlet/DoWebTrans

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.ccavenue.com/apis/servlet/DoWebTrans");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER,'Content-Type: application/json') ;

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $final_data);

// Get server response ...

$result = curl_exec($ch);

curl_close($ch);

$status = '';

$information = explode('&', $result);

print_r($information);

$dataSize = sizeof($information);

for ($i = 0; $i < $dataSize; $i++) {

$info_value = explode('=', $information[$i]);

if ($info_value[0] == 'enc_response') {

$status = $this->ccvenuecrypto->decrypt(trim($info_value[1]), $working_key);

}

}

echo 'Status revert is: ' . $status.'<pre>';

$obj = json_decode($status);

print_r($obj);

if($obj->Order_Status_Result->status==0 && $obj->Order_Status_Result->order_status=='Shipped'){

$bank_reponse=$obj->Order_Status_Result->order_bank_response;

$payment_status=2;

if($bank_reponse=='Y' && $order_r_id==$order_id)

{

 // Logic Here

}

}

}

View More...

First Previous 8 9 10 11 12 Next Last