<commit_before>from datetime import datetime
from bson import ObjectId
import re

class DuplicateStoreNameException(Exception):
  def __init__(self, message='Store name already exits'):
    Exception.__init__(self, message)


class StoreService(object):
  def __init__(self, db):
    self.db = db
    self.stores = self.db.store_collection
    self.weekday_names = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']

  def search(self, tenant_id, filter_text=None, only_veg=False, only_open=False, page_no=1,page_size=10):
    query = {"tenant_id": ObjectId(tenant_id)}

    if filter_text is not None and len(filter_text) > 2:
      query['name'] = {'$regex': r".*%s.*" % filter_text, '$options': 'i'}

    if only_veg:
      query['food_type'] = {'$elemMatch': {'$eq': 'veg'}}

    if only_open:
      now = datetime.now()
      hrmin = float("%.2f" % ((now.hour + (now.minute/60))%12))
      day = self.weekday_names[now.weekday()]
      query['open_time'] = { "$lte": hrmin }
      query['close_time'] = { "$gte": hrmin }
      query['is_closed'] = { "$eq": False }
      query['holidays'] = {
        "$elemMatch":  {
          '$not': re.compile(day, re.IGNORECASE)
        }
      }

    offset = (page_no - 1) * page_size
    if offset < 0: offset = 0
    lst = self.stores.find(query)
    return [x for x in lst.sort("created_at", -1).skip(offset).limit(page_size)], lst.count()

  def save(self, store_item):
    if store_item.get('cuisines', None) is not None and isinstance(store_item['cuisines'], str):
        store_item['cuisines'] = store_item['cuisines'].split(',')

    if '_id' not in store_item or store_item['_id'] is None or store_item['_id'] == "-1":
      store_item.pop('_id', None)
      store_item['created_at'] = datetime.now()
      store_item['status'] = True
    else:
      store_item['updated_at'] = datetime.now()

    if self.check_duplicate_name(store_item['name'], store_item.get('_id', None)):
      raise DuplicateStoreNameException()

    return self.stores.save(store_item)

  def get_by_name(self, name):
    return self.stores.find_one({'name': name})

  def delete(self, _id):
    item = self.stores.find_one({'_id': ObjectId(_id)})
    if item:
      self.stores.remove(item)<commit_msg>fixed open/close time issue in store query<commit_after>from datetime import datetime
from bson import ObjectId
import re

class DuplicateStoreNameException(Exception):
  def __init__(self, message='Store name already exits'):
    Exception.__init__(self, message)


class StoreService(object):
  def __init__(self, db):
    self.db = db
    self.stores = self.db.store_collection
    self.weekday_names = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']

  def search(self, tenant_id, filter_text=None, only_veg=False, only_open=False, page_no=1,page_size=10):
    query = {"tenant_id": ObjectId(tenant_id)}

    if filter_text is not None and len(filter_text) > 2:
      query['name'] = {'$regex': r".*%s.*" % filter_text, '$options': 'i'}

    if only_veg:
      query['food_type'] = {'$elemMatch': {'$eq': 'veg'}}

    if only_open:
      now = datetime.now()
      hour = now.hour
      if hour > 12:
        open_time = float("%.2f" % ((hour + (now.minute/60))%12))
        close_time = open_time
      else:
        open_time = float("%.2f" % ((hour + (now.minute/60))))
        close_time = open_time%12

      day = self.weekday_names[now.weekday()]
      query['open_time'] = { "$lte": open_time }
      query['close_time'] = { "$gte": close_time }
      query['is_closed'] = { "$eq": False }
      query['holidays'] = {
        "$elemMatch":  {
          '$not': re.compile(day, re.IGNORECASE)
        }
      }
    # print(query)
    offset = (page_no - 1) * page_size
    if offset < 0: offset = 0
    lst = self.stores.find(query)
    return [x for x in lst.sort("created_at", -1).skip(offset).limit(page_size)], lst.count()

  def save(self, store_item):
    if store_item.get('cuisines', None) is not None and isinstance(store_item['cuisines'], str):
        store_item['cuisines'] = store_item['cuisines'].split(',')

    if '_id' not in store_item or store_item['_id'] is None or store_item['_id'] == "-1":
      store_item.pop('_id', None)
      store_item['created_at'] = datetime.now()
      store_item['status'] = True
    else:
      store_item['updated_at'] = datetime.now()

    if self.check_duplicate_name(store_item['name'], store_item.get('_id', None)):
      raise DuplicateStoreNameException()

    return self.stores.save(store_item)

  def get_by_name(self, name):
    return self.stores.find_one({'name': name})

  def delete(self, _id):
    item = self.stores.find_one({'_id': ObjectId(_id)})
    if item:
      self.stores.remove(item)
{'fraction_non_alphanumeric': 0.16424510423247, 'fraction_numerical': 0.007369972625815961, 'mean_word_length': 3.576107899807322, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2541074', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1629, 'n_words': 344}