@@ -406,6 +406,164 @@ def cron_meli_questions(self):
406406
407407 #mercadolibre_use_buyer_name = fields.Boolean(string="Use buyer name",default=True)
408408
409+ #Toma y lista los ids de las publicaciones del sitio de MercadoLibre, filtrados por official_store_id
410+ def fetch_list_meli_ids ( self , params = None ):
411+
412+ company = self or self .env .user .company_id
413+
414+ if not params :
415+ params = {}
416+
417+ config = company
418+
419+ meli = self .env ['meli.util' ].get_new_instance ( company )
420+ if meli .need_login ():
421+ return meli .redirect_login ()
422+
423+ official_store_id = config .mercadolibre_official_store_id or None
424+ seller_id = config .mercadolibre_seller_id
425+
426+ response = meli .get ("/users/" + str (seller_id )+ "/items/search" ,
427+ {'access_token' :meli .access_token ,
428+ 'search_type' : 'scan' ,
429+ 'limit' : 100 , #max paging limit is always 100
430+ ** params })
431+
432+ rjson = response .json ()
433+ scroll_id = ""
434+ results = []
435+ ofresults = (rjson and "results" in rjson and rjson ["results" ]) or []
436+ filt_results = self .filter_meli_ids ( ofresults , official_store_id )
437+ results += filt_results or []
438+ condition_last_off = True
439+ total = (rjson and "paging" in rjson and "total" in rjson ["paging" ] and rjson ["paging" ]["total" ]) or 0
440+ _logger .info ("fetch_list_meli_ids: params:" + str (params )+ " total:" + str (total ))
441+
442+ if (rjson and 'scroll_id' in rjson ):
443+ scroll_id = rjson ['scroll_id' ]
444+ condition_last_off = False
445+
446+ while (condition_last_off != True ):
447+ search_params = {
448+ 'access_token' : meli .access_token ,
449+ 'search_type' : 'scan' ,
450+ 'limit' : 100 ,
451+ 'scroll_id' : scroll_id ,
452+ ** params
453+ }
454+ response = meli .get ("/users/" + str (seller_id )+ "/items/search" , search_params )
455+ rjson2 = response .json ()
456+ if (rjson2 and 'error' in rjson2 ):
457+ _logger .error (rjson2 )
458+ if rjson2 ['message' ]== 'invalid_token' or rjson2 ['message' ]== 'expired_token' :
459+ ACCESS_TOKEN = ''
460+ REFRESH_TOKEN = ''
461+ account .write ({'access_token' : ACCESS_TOKEN , 'refresh_token' : REFRESH_TOKEN , 'code' : '' } )
462+ condition = True
463+ url_login_meli = meli .auth_url ()
464+ return {
465+ "type" : "ir.actions.act_url" ,
466+ "url" : url_login_meli ,
467+ "target" : "new" ,}
468+ condition_last_off = True
469+ else :
470+ #results+= (rjson2 and "results" in rjson2 and rjson2["results"]) or []
471+
472+ ofresults = (rjson2 and "results" in rjson2 and rjson2 ["results" ]) or None
473+ filt_results = self .filter_meli_ids ( ofresults , official_store_id )
474+ results += filt_results or []
475+ condition_last_off = (total > 0 and len (results )>= total )
476+
477+ return results
478+
479+ def filter_meli_ids ( self , results , store_id ):
480+
481+ company = self or self .env .user .company_id
482+
483+ config = company
484+
485+
486+ meli = self .env ['meli.util' ].get_new_instance ( company )
487+ if meli .need_login ():
488+ return meli .redirect_login ()
489+
490+ official_store_id = store_id
491+ if not official_store_id :
492+ return results
493+
494+ c = 0
495+ n20 = 0
496+ rresults = []
497+ #_logger.info("results:"+str(results))
498+ if not results :
499+ return rresults
500+
501+ ids = ""
502+ coma = ""
503+ maxc = (results and len (results )) or 0
504+ for meli_id in results :
505+ c += 1
506+ n20 += 1
507+
508+ #read id and official_store_id to check
509+ #hacer paquetes de 20 !!!!!! /items?ids=$ITEM_ID1,$ITEM_ID2&attributes=$ATTRIBUTE1,$ATTRIBUTE2,$ATTRIBUTE3
510+
511+ #armamos el paquete
512+ ids += coma + str (meli_id )
513+ coma = ","
514+
515+ if n20 == 20 or c == maxc :
516+ item_params = {
517+ "ids" : str (ids ),
518+ "attributes" : "id,official_store_id"
519+ }
520+ #_logger.info("item_params:"+str(item_params))
521+ responseItem = meli .get ("/items" + str ('?ids=' + str (ids )+ '&attributes=' + str ('id,official_store_id' )), {} )
522+ #[ { "code": 200, "body": { "id": "MLM863472529", "official_store_id": 3476 } },
523+ #_logger.info("responseItem:"+str(responseItem and responseItem.json()))
524+ if responseItem .json ():
525+ for rr in responseItem .json ():
526+ #_logger.info("rr:"+str(rr))
527+ if ("code" in rr and rr ["code" ]== 200 ):
528+ if ("body" in rr and rr ["body" ]):
529+ st_id = "official_store_id" in rr ["body" ] and rr ["body" ]["official_store_id" ]
530+ ml_id = "id" in rr ["body" ] and rr ["body" ]["id" ]
531+ if (st_id and official_store_id and str (st_id )== str (official_store_id ) ):
532+ rresults .append (ml_id )
533+ ids = ""
534+ n20 = 0
535+ #_logger.info("rresults:"+str(rresults))
536+
537+ return rresults
538+
539+ #list all meli ids in odoo from this account, that are not in parameter filter_ids...
540+ def list_meli_ids ( self , filter_ids = None ):
541+ meli_ids = []
542+ company = self
543+ product_melis = []
544+ if not filter_ids :
545+ product_melis = self .env ['product.product' ].search ([
546+ ('meli_id' ,'!=' ,False ),
547+ ('meli_id' ,'ilike' ,'M%' ),
548+ '|' ,
549+ ('company_id' ,'=' ,company .id ),
550+ ('company_id' ,'=' ,False )
551+ ])
552+ else :
553+ product_melis = self .env ['product.product' ].search ([
554+ ('meli_id' ,'!=' ,False ),
555+ ('meli_id' ,'ilike' ,'M%' ),
556+ ('meli_id' ,'not in' ,filter_ids ),
557+ '|' ,
558+ ('company_id' ,'=' ,company .id ),
559+ ('company_id' ,'=' ,False )
560+ ])
561+
562+ if product_melis :
563+ meli_ids = product_melis .mapped ('meli_id' )
564+
565+ return meli_ids
566+
409567 def meli_logout (self ):
410568 _logger .info ('company.meli_logout() ' )
411569 self .ensure_one ()
0 commit comments