views.py 12.2 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8
from outils.common_imports import *
from outils.for_view_imports import *


from members.models import CagetteMember
from shop.models import CagetteShop

@never_cache
9 10
def shop_index(request):
    return index(request, mode='shop')
11

12 13 14 15
@never_cache
def delivery_index(request):
    return index(request, mode='delivery')

16
def _get_index_context(credentials, shop_settings, mode):
Administrator committed
17
    context = {'title': 'Commande / Réservation',
18
               'mode': mode,
Administrator committed
19
               'COMPANY_NAME': settings.COMPANY_NAME,
20 21
               'header_img': getattr(settings, 'SHOP_HEADER_IMG', '/static/img/header.jpg')
               }
Administrator committed
22 23
    if 'capital_message' in shop_settings:
        context['capital_message'] = shop_settings['capital_message']
24

Administrator committed
25 26 27 28 29 30 31 32
    if ('failure' in credentials):
        context['msg'] = ''
        if 'msg' in credentials:
            context['msg'] = credentials['msg']
        context['password_placeholder'] = 'Mot de passe'
        context['password_notice'] = "Par défaut, la date de naissance (jjmmaaaa)"
        context['with_shop_header'] = True
    else:
33
        if mode == 'shop' and hasattr(settings, 'SHOP_CAN_BUY'):
Administrator committed
34
            context['SHOP_CAN_BUY'] = settings.SHOP_CAN_BUY
35 36 37 38
            context['DELIVERY_CAN_BUY'] = False
        if mode == 'delivery' and hasattr(settings, 'DELIVERY_CAN_BUY'):
            context['SHOP_CAN_BUY'] = False
            context['DELIVERY_CAN_BUY'] = settings.DELIVERY_CAN_BUY
Administrator committed
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        context['SHOP_CATEGORIES'] = getattr(settings, 'SHOP_CATEGORIES', [])
        context['EXCLUDE_SHOP_CATEGORIES'] = getattr(settings, 'EXCLUDE_SHOP_CATEGORIES', [])
        context['MIN_DELAY_FOR_SLOT'] = getattr(settings, 'MIN_DELAY_FOR_SLOT', 30)
        context['HOURS_FOR_VALIDATION'] = getattr(settings, 'HOURS_FOR_VALIDATION_SHOP', 2)
        context['SHOP_OPENING'] = getattr(settings, 'SHOP_OPENING', {})
        context['SHOP_SLOT_SIZE'] = getattr(settings, 'SHOP_SLOT_SIZE', 15)
        context['SHOP_OPENING_START_DATE'] = getattr(settings, 'SHOP_OPENING_START_DATE', None)
        context['survey_link'] = getattr(settings, 'SHOP_SURVEY_LINK', '')
        context['extra_menus'] = getattr(settings, 'SHOP_EXTRA_MENUS', None)
        context['SHOW_SUBSTITUTION_OPTION'] = getattr(settings, 'SHOW_SUBSTITUTION_OPTION', False)
        context['CART_VALIDATION_BOTTOM_MSG'] = getattr(settings, 'CART_VALIDATION_BOTTOM_MSG', "")
        context['SHOP_BOTTOM_VALIDATION_MSG'] = getattr(settings, 'SHOP_BOTTOM_VALIDATION_MSG',\
                "Si vous arrivez avec un retard de plus d'une heure, la commande pourrait ne plus être disponible.")

        stock_warning = getattr(settings, 'SHOP_STOCK_WARNING', True)
        if stock_warning is True:
            context['SHOP_STOCK_WARNING'] = 'true'
        else:
            context['SHOP_STOCK_WARNING'] = 'false'

    return context


def index(request, mode="shop"):
    template = loader.get_template('shop/index.html')
    credentials = CagetteMember.get_credentials(request)
    shop_settings = CagetteShop.get_shop_settings()

    allowed_states = ["up_to_date", "alert", "delay"]
    #  Uncomment if 'coop_state' in credentials .... etc
    #  to prevent other states people to use the shop
    allowed = True

    context = _get_index_context(credentials, shop_settings, mode)

    if ('failure' in credentials):
        #  Visitor has not been identified
        template = loader.get_template('website/connect.html')
    else:
Administrator committed
79 80 81
        d_p_pdts = CagetteShop.get_promoted_and_discounted_products()
        context['discounted_pdts'] = d_p_pdts['discounted']
        context['promoted_pdts'] = d_p_pdts['promoted']
82 83 84 85 86
        cat_nb_pdts = CagetteShop.get_categories_nb_of_products()
        if 'error' in cat_nb_pdts:
            context['cat_nb_pdts'] = None
        else:
            context['cat_nb_pdts'] = cat_nb_pdts
Administrator committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

    # if 'coop_state' in credentials and not (credentials['coop_state'] in allowed_states):
    #     allowed = False
    #     template = loader.get_template('shop/forbidden.html')
    response = HttpResponse(template.render(context, request))

    if ((allowed is True) and 'token' in credentials and 'auth_token' in credentials):
        response.set_cookie('id', credentials['id'])
        response.set_cookie('token', credentials['token'])
        response.set_cookie('auth_token', credentials['auth_token'])
        response.set_cookie('deconnect_option', 'true')
    return response

def get_all_available_bought_products(request):
    result = {}
    credentials = CagetteMember.get_credentials(request)
    if 'success' in credentials:
        try:
            result['data'] = CagetteShop.get_all_available_bought_products_by_member(request.COOKIES['id'])
        except:
            result['error'] = 'Erreur pendant la récupération de la liste de produits'
    else:
        result['error'] = 'Authentification non valide'
    return JsonResponse({'res': result})

def get_cat_children(request):
    result = {}
    try:
        result['data'] = CagetteShop.get_cat_children(request.GET.get('id'))
    except:
        result['error'] = 'Erreur pendant la récupération de la liste de catégories'
    return JsonResponse({'res': result})

def get_categ_products(request):
    result = {}
    credentials = CagetteMember.get_credentials(request)
    if 'success' in credentials:
        try:
            result['data'] = CagetteShop.get_category_products(request.GET.get('id'))
        except:
            result['error'] = 'Erreur pendant la récupération de la liste de produits'
    else:
        result['error'] = 'Authentification non valide'
    return JsonResponse({'res': result})

132

Administrator committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
def search_product(request):
    result = {}
    credentials = CagetteMember.get_credentials(request)
    if 'success' in credentials:
        try:
            kw = request.GET['kw']
            result['data'] = CagetteShop.get_products_matching(kw)
        except:
            result['error'] = 'Erreur pendant la récupération de la liste de produits'
    else:
        result['error'] = 'Authentification non valide'
    return JsonResponse({'res': result})

def cart_init(request):
    """A cart initialization is beeing submitted by a coop"""
    import json
    result = {}

    try:
        # first of all, verifiying cart is respecting time slots constraints
        cart = json.loads(request.POST.get('order'))
154 155
        # Verify for shop only, not for delivery
        ts_respect = CagetteShop.isCartRespectingTimeSlotContraints(cart) if cart['type'] == 'shop' else True
Administrator committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        if (ts_respect is False):
            result['ts_respect'] = False
            result['error'] = 'Forbidden timeslot'
            return JsonResponse({'res': result})
        credentials = CagetteMember.get_credentials(request)
        if 'success' in credentials:
            try:
                result['cart'] = CagetteShop.registrerCartInitialization(cart, request.COOKIES['id'])
            except Exception as e:
                result['error'] = str(e)
        else:
            result['error'] = 'Authentification non valide'
    except Exception as e:
        result['error'] = str(e)


    return JsonResponse({'res': result})

def cart(request):
    """A cart is beeing submitted by a coop"""
    import json
    result = {}

    try:
        # first of all, verifiying cart is respecting time slots constraints
        # still beeing done, in case user changed the date in browser memory
        cart = json.loads(request.POST.get('order'))
        # ts_respect = CagetteShop.isCartRespectingTimeSlotContraints(cart)
        # if (ts_respect is False):
        #     result['ts_respect'] = False
        #     result['error'] = 'Forbidden timeslot'
        #     return JsonResponse({'res': result})
        credentials = CagetteMember.get_credentials(request)
        if 'success' in credentials:
            try:
191 192 193 194
                mode = "shop"
                if 'type' in cart:
                     mode = cart['type']
                result['cart'] = CagetteShop.registrerCart(cart, request.COOKIES['id'], mode)
Administrator committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
            except Exception as e:
                result['error'] = str(e)
        else:
            result['error'] = 'Authentification non valide'
    except Exception as e:
        result['error'] = str(e)


    return JsonResponse({'res': result})

def full_slots(request):
    """Returns full futur slots and dates when shop is closed."""
    result = {}
    #  Not critical, no matter connected or not
    try:
        result['full_slots'] = CagetteShop.get_full_slots()
        result['full_slots'].sort()

        shop_settings = CagetteShop.get_shop_settings()
        if 'closing_dates' in shop_settings:
            result['closing_dates'] = shop_settings['closing_dates']

    except Exception as e:
        result['error'] = str(e)
    return JsonResponse({'res': result})

def my_orders(request):
    result = {}
    credentials = CagetteMember.get_credentials(request)
    if 'success' in credentials:
        try:
            result['data'] = CagetteShop.get_orders_by_partner_id(request.COOKIES['id'])
        except:
            result['error'] = 'Erreur pendant la récupération de la liste des commandes'
    else:
        result['error'] = 'Authentification non valide'
    return JsonResponse({'res': result})

def change_cart_date(request, cart_id):
    """Couchdb delivery date entry will be changed."""

    """Only for connected people
    ensure that cart is connected member one is done in CagetteShop method
    """
    result = {}
    try:
        ts_respect = CagetteShop.isCartRespectingTimeSlotContraints({'best_date': request.POST.get('new_date')})
        if (ts_respect is False):
            result['ts_respect'] = False
            result['error'] = 'Forbidden timeslot'
            return JsonResponse({'res': result})
        credentials = CagetteMember.get_credentials(request)
        if 'success' in credentials:
            try:
                result = CagetteShop.change_best_date(cart_id, request)
            except:
                result['error'] = 'Erreur pendant le changement de date'
        else:
            result['error'] = 'Authentification non valide'
    except Exception as e:
        result['error'] = str(e)

    return JsonResponse({'res': result})

def delete_cart(request):
    """Couchdb entry will be deleted."""

    """Only for connected people
    ensure that cart is connected member one is done in CagetteShop method
    """
    result = {}
    credentials = CagetteMember.get_credentials(request)
    if 'success' in credentials:
        try:
            result = CagetteShop.destroy_connected_user_cart(request)
        except:
            result['error'] = 'Erreur pendant la destruction de la commande'
    else:
        result['error'] = 'Authentification non valide'
    return JsonResponse({'res': result})

def fusion_carts(request):
    """Couchdb entries will be gathered."""

    """Only for connected people
    ensure that cart is connected member one is done in CagetteShop method
    """
    result = {}
    credentials = CagetteMember.get_credentials(request)
    if 'success' in credentials:
        try:
            result = CagetteShop.fusion_carts(request)
        except:
            result['error'] = 'Erreur pendant la fusion des commandes'
    else:
        result['error'] = 'Authentification non valide'
    return JsonResponse({'res': result})

def planning(request):
    template = loader.get_template('shop/planning.html')
    context = {'title': 'Planning Commande / Réservation',
               'COMPANY_NAME': settings.COMPANY_NAME,
               'slots': CagetteShop.get_slots()}
    return HttpResponse(template.render(context, request))

@csrf_exempt
def log_browser_error(request):
    try:
        from pytz import timezone
        paris_tz = pytz.timezone('Europe/Paris')
        file = open('shop/errors.log', 'a')

        line = [str(datetime.datetime.now(tz=paris_tz)),
                request.META.get('HTTP_USER_AGENT', ''),
                request.POST.get('error')]
        file.write("\t".join(line) + "\n")
        file.close()
    except Exception as e:
        coop_logger.warning("Shop, log_browser_error : %s", str(e))

    return HttpResponse('ok')

def remove_unused_orders(request):
    """Unsed orders (in init state) will be deleted."""
    """called from a cron job"""
    res = {}
    try:
        res['deleted'] = CagetteShop.remove_unused_orders()
    except Exception as e:
        res['error'] = str(e)
325
    return JsonResponse(res)