def notification_route(request, uid): if request.method != 'GET': return invalid_request(request) token = request.GET.get('token', None) action = request.GET.get('action', None) if token is None or action is None: return invalid_request(request) data = token_decode(token) if data is None: return invalid_request(request) user_id = data['user_id'] if request.user.id != user_id: return invalid_request(request) item = Notification.objects.filter(uid=uid, user_id=user_id).first() if item is None: return invalid_request(request) item.seen = True item.save() remain_notification = Notification.objects.filter(seen=False, user_id=user_id).count() if remain_notification == 0: request.user.has_notification = False request.user.save() return redirect(action)
def view(request, space_name, product_uid): product_uid = is_valid_uuid(product_uid) if product_uid is None: return invalid_request(request) try: product = Product.objects.get(uid = product_uid, space__name__iexact=space_name) media = ProductMedia.objects.filter(product_id=product.uid) context = {} context['product'] = product context['media'] = media current_site = get_current_site(request) context['current_site'] = current_site if not request.user.is_authenticated: return render(request, 'space/product/single.html', context) related_products = Product.objects.filter(category_id=product.category_id).values( 'uid', 'title','price', 'space__name', 'react_good', 'react_bad', 'react_fake', 'logo_url').order_by('-time_date')[:10] react_obj = ProductReact.objects.filter(product_id=product.uid, user_id=request.user.id).first() if react_obj is not None: context['has_react'] = True context['current_react'] = react_obj.react else: context['has_react'] = False context['current_react'] = 'N' if request.user.id == product.space.owner_id: context['is_owner'] = True else: context['is_owner'] = False pin = PinnedProduct.objects.filter(product_id=product.uid, user_id=request.user.id).first() if pin is not None: context['has_pin'] = True else: context['has_pin'] = False token = get_api_token(request) context['token'] = token context['related_products'] = related_products return render(request, 'space/product/view.html', context) except ObjectDoesNotExist as e: return invalid_request(request=request)
def update(request, space_name): context = {} try: space = Space.objects.get(code=hashing_into_int(space_name)) if request.user == space.owner: tab = request.GET.get('tab', 'information') tab = tab.lower() if tab == 'banner': banners = Banner.objects.filter(space_id=space.id) context['banners'] = banners token = get_api_token(request) context['token'] = token elif tab == 'logo': token = get_api_token(request) context['token'] = token else: tab = 'information' if request.method == 'POST': form = SpaceUpdateForm(request.POST, space=space) if form.is_valid(): space = form.save() return redirect('/'+space.name+'/') else: form = SpaceUpdateForm(space=space) context['form'] = form context['tab'] = tab context['space'] = space return render(request, 'space/manage/update.html', context) except ObjectDoesNotExist as e: pass return invalid_request(request, context)
def create(request): if request.user.has_space: return invalid_request(request) context = {} if request.method == 'POST': if request.user.has_space: return invalid_request(request) form = SpaceCreateForm(request.POST, request.FILES, request=request) if form.is_valid(): space = form.save() status = Status.objects.create(space=space) request.user.has_space=True request.user.save() return redirect('/'+space.name+'/') else: pass else: form = SpaceCreateForm(request=request) context['form'] = form return render(request, 'space/manage/create.html', context)
def update(request, space_name, product_uid): product_uid = is_valid_uuid(product_uid) if product_uid is None: return invalid_request(request) try: product = Product.objects.get(uid=product_uid, space__name__iexact=space_name) if product.space.owner_id == request.user.id: context = {} tab = request.GET.get('tab', 'details') tab = tab.lower() if tab == 'images': media = ProductMedia.objects.filter(product=product) context['media'] = media else: if request.method == 'POST': form = ProductUpdateForm(request.POST, product=product) if form.is_valid(): product = form.save() return redirect('/'+space_name+'/product/'+str(product_uid)+'/') else: form = ProductUpdateForm(product=product) tab = 'details' context['form'] = form token = get_api_token(request) context['token'] = token context['tab'] = tab context['product'] = product context['space_name'] = space_name return render(request, 'space/product/update.html', context) except ObjectDoesNotExist as e: pass return invalid_request(request)
def delete(request, space_name, product_uid): product_uid = is_valid_uuid(product_uid) if product_uid is None: return invalid_request(request) context = {} if request.method == 'POST': form = PasswordConfirmForm(request.POST, user=request.user) if form.is_valid(): return _delete_data(request, space_name, product_uid) else: form = PasswordConfirmForm(user=request.user) context['form'] = form print(context) return password_confirmation(request, context)
def verify(request, token): data = token_decode(token) if data is None: return views.invalid_request(request) user_id = data['user_id'] email = data['email'] user = Account.objects.get(id=user_id) if user.is_active: return redirect('/account/') if email == user.email: user.is_active = True user.save() return render(request, 'account/auth/confirm.html', {}) return invalid_request(request)
def search(request): context = {} query = None if request.method != 'GET': return invalid_request(request) query = request.GET.get('query', None) what = request.GET.get('what', 'product') if query is None: return redirect('/') token = get_api_token(request) context['token'] = token context['query'] = query context['what'] = what.lower() return render(request, 'home/filtering/search.html', context)
def _delete_data(request, space_name, product_uid): try: product = Product.objects.get(uid=product_uid, space__name__iexact=space_name) if product.space.owner == request.user: space_name = product.space.name # count of total total_pin = PinnedProduct.objects.filter(product_id=product.uid).count() PinnedProduct.objects.filter(product_id=product.uid).delete() ProductReact.objects.filter(product_id=product.uid).delete() status = Status.objects.get(space_id=product.space_id) status.total_good_react -= product.react_good status.total_bad_react -=product.react_bad status.total_fake_react -=product.react_fake status.total_post -= 1 status.total_pinned -= total_pin remove_point = ((ACTIVITY_POINT['GOOD']*product.react_good)+(ACTIVITY_POINT['PIN']*total_pin)+ (ACTIVITY_POINT['BAD']*product.react_bad)+(ACTIVITY_POINT['FAKE']*product.react_fake)) status.rating -= remove_point status.save() media = ProductMedia.objects.filter(product_id=product.uid) # delete image sources for item in media: Image.delete(item.location) # delete database objects media.delete() category = Category.objects.get(id=product.category_id) category.total_products -= 1 category.save() product.delete() return redirect('/'+space_name+'/') except ObjectDoesNotExist as e: pass return invalid_request(request)
def index(request, space_name): try: space = Space.objects.get(code=hashing_into_int(space_name)) status = Status.objects.get(space_id=space.id) banners = Banner.objects.filter(space_id=space.id) token = get_api_token(request) context = {} context['space'] = space context['banners'] = banners context['status'] = status context['has_favorite'] = False context['token'] = token try: favorite = Favorite.objects.get(user_id=request.user.id, space_id=space.id) context['has_favorite'] = True except ObjectDoesNotExist as e: pass return render(request, 'space/manage/index.html', context) except ObjectDoesNotExist as e: return invalid_request(request)