def index(request): last = request.GET.get('last', 'month') query = days_query[last] today = datetime.date.today() since = today - datetime.timedelta(days=query) total_entities = Entity.objects.count() new_entities = Entity.objects.filter( registered__gte=since).order_by('-registered') new_entities = helpers.paginate(new_entities, 1, elems_perpage=6) total_persons = Person.objects.count() new_persons = Person.objects.filter( registered__gte=since).order_by('-registered') new_persons = helpers.paginate(new_persons, 1, elems_perpage=6) debit_wallet = Wallet.objects.filter(type__id="debit", city_debit__id="mad").first() print(debit_wallet) return render( request, 'reports/index.html', { 'last': last, 'total_entities': total_entities, 'total_persons': total_persons, 'persons': new_persons, 'debit_wallet': debit_wallet, 'entities': new_entities })
def activity_list(request): activities = Activity.objects.all() query_string = '' if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET['q'] entry_query = helpers.get_query(query_string, ['name', 'description', 'type_activity']) if entry_query: activities = activities.filter(entry_query) page = request.GET.get('page') activities = helpers.paginate(activities, page, elems_perpage=15) params = { 'ajax_url': reverse('activity_list'), 'query_string': query_string, 'activities': activities, 'page': page } if request.is_ajax(): response = render(request, 'activity/search_results.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: return render(request, 'activity/list.html', params)
def wallet_search(request): wallets = Wallet.objects.all().select_related('user') query_string = '' if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET['q'].strip() entry_query = helpers.get_query(query_string, ['user__entity__name', 'user__person__name', 'user__username']) if entry_query: wallets = wallets.filter(entry_query) page = request.GET.get('page') wallets = helpers.paginate(wallets, page, elems_perpage=12) params = { 'ajax_url': reverse('wallet_search'), 'query_string': query_string, 'wallets': wallets, } if request.is_ajax(): response = render(request, 'wallets/wallets_query.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: return render(request, 'wallets/wallets_list.html', params)
def entity_list(request): entities = Entity.objects.all() category_filter = request.GET.get('cat', None) if category_filter: entities = entities.filter(categories__pk=category_filter) query_string = '' if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET['q'] entry_query = helpers.get_query( query_string, ['name', 'description', 'short_description']) if entry_query: entities = entities.filter(entry_query) page = request.GET.get('page') entities = helpers.paginate(entities, page, elems_perpage=6) params = { 'ajax_url': reverse('entity_list'), 'query_string': query_string, 'entities': entities, 'page': page } if request.is_ajax(): response = render(request, 'entity/search_results.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: params['categories'] = Category.objects.all() return render(request, 'entity/list.html', params)
def profile_list(request): persons = Person.objects.all() query_string = '' if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET['q'] entry_query = helpers.get_query(query_string, ['name', 'surname', 'email']) if entry_query: persons = persons.filter(entry_query) page = request.GET.get('page') persons = helpers.paginate(persons, page, elems_perpage=10) params = { 'ajax_url': reverse('profile_list'), 'query_string': query_string, 'profiles': persons, 'page': page } if request.is_ajax(): response = render(request, 'profile/profile_query.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: return render(request, 'profile/list.html', params)
def retrieve_questions(): ''' Handles GET requests for questions, including pagination (every 10 questions). Returns a list of questions, number of total questions, current category, categories. ''' questions = Question.query.all() page = request.args.get('page', 1, type=int) page_questions = paginate(page, questions) if len(page_questions) == 0: abort(404) categories = Category.query.order_by(Category.id).all() formatted_categories = { category.id: category.type for category in categories } return jsonify({ 'success': True, 'questions': page_questions, 'total_questions': len(questions), 'categories': formatted_categories, 'current_category': 'All' })
def transaction_list(request): start_date = timezone.now() - datetime.timedelta(days=71) end_date = timezone.now() transactions = Transaction.objects.all().order_by('-timestamp') transactions_bydate = Transaction.objects.filter(timestamp__gte=start_date, timestamp__lte=end_date) \ .annotate(day=TruncDay('timestamp')) \ .values('day') \ .annotate(total=Sum('amount')).order_by('day') page = request.GET.get('page') transactions = helpers.paginate(transactions, page, elems_perpage=10) params = { 'transactions': transactions, } if request.is_ajax(): response = render(request, 'wallets/transactions_query.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: params['transactions_bydate'] = transactions_bydate return render(request, 'wallets/transactions_list.html', params)
def news_list(request): news = News.objects.all() query_string = '' if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET['q'] entry_query = helpers.get_query( query_string, ['name', 'description', 'short_description']) if entry_query: news = news.filter(entry_query) page = request.GET.get('page') news = helpers.paginate(news, page, elems_perpage=10) params = { 'ajax_url': reverse('news_list'), 'query_string': query_string, 'news': news, 'page': page } if request.is_ajax(): response = render(request, 'news/search_results.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: return render(request, 'news/list.html', params)
def get(self, cook): user = User.is_logged(self) author = User.all().filter('nickname =', cook.lower()).fetch(1) if not len( author ): self.error(404) return author = author[0] if user: alreadyfollow = Friendship.alreadyfollow( user, author ) title = "%s's CookBook" %(author.nickname) subhead = "Discover what %s has shared" % (author.nickname) author_recipes_count = Koch.all().filter('author =', author).count(); if author.fb_profile_url: avatar = "https://graph.facebook.com/%s/picture" % (author.nickname) elif not author.usegravatar and author.avatar: avatar = "/avatar/?user_id=%s" %(author.key()) else: avatar = helpers.get_gravatar( author.email, 90 ) page = self.request.get_range('page', min_value=0, max_value=1000, default=0) tmp_kochs, next_page, prev_page = helpers.paginate( Koch.all().filter('author =', author).order('-created'), page ) kochs = helpers.get_kochs_data(tmp_kochs) last_kochs = Koch.all().filter('author =', author).order('-created').fetch(5); last_from_all = Koch.get_random() current = "explore" self.response.out.write(template.render('templates/list_kochs.html', locals()))
def activity_detail(request): params = { 'ajax_url': reverse('activity_detail'), 'act': request.GET.get('act', None), 'person': request.GET.get('person', None), } activities = PersonActivity.objects.all() if ('act' in request.GET) and request.GET['act'].strip(): act = request.GET['act'] activities = activities.filter(activity=act) params['activity'] = Activity.objects.filter(pk=act).first() if ('person' in request.GET) and request.GET['person'].strip(): person = request.GET['person'] activities = activities.filter(person=person) params['person'] = Person.objects.filter(pk=person).first() page = request.GET.get('page') activities = helpers.paginate(activities, page, elems_perpage=10) params['activities'] = activities params['page'] = page if request.is_ajax(): response = render(request, 'activity/activity_query.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: return render(request, 'activity/detail_list.html', params)
def wallet_detail(request, pk): wallet = get_object_or_404(Wallet, pk=pk) user_type, instance = wallet.user.get_related_entity() transactions = TransactionLog.objects.filter(wallet=wallet) page = request.GET.get('page') transactions = helpers.paginate(transactions, page, elems_perpage=10) if request.is_ajax(): response = render(request, 'wallets/transaction_logs_query.html', {'transactions':transactions}) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: start_date = timezone.now() - datetime.timedelta(days=71) end_date = timezone.now() transactions_bydate = TransactionLog.objects.filter(wallet=wallet, timestamp__gte=start_date, timestamp__lte=end_date) \ .annotate(day=TruncDay('timestamp')) \ .values('day') \ .annotate(total=Sum('amount')).order_by('day') return render(request, 'wallets/detail.html', { 'showing_all': False, 'instance':instance, 'user_type':user_type, 'wallet': wallet, 'transactions': transactions,'transactions_bydate':transactions_bydate })
def popular_questions(request): q = Question.objects.all().order_by('-rating') page = paginate(request, q) return render( request, 'qa/popular.html', {'page': page, 'title': 'popular'} )
def new_question(request): q = Question.objects.all().order_by('-added_at') page = paginate(request, q) return render( request, 'qa/question_list.html', {'page': page, 'title': 'new questions'} )
def popular(request): questions = Question.objects.allByRating() paginator, page = paginate(request, questions) url = reverse('popular') return render(request, 'popular.html', { 'questions': page.object_list, 'paginator': paginator, 'page': page, 'baseurl': url, })
def questions(request): questions = Question.objects.allByDate() paginator, page = paginate(request, questions) url = reverse('home') return render(request, 'questions.html', { 'questions': page.object_list, 'paginator': paginator, 'page': page, 'baseurl': url, })
def get(self): user = User.is_logged(self) page = self.request.get_range('page', min_value=0, max_value=1000, default=0) title = "Explore Recipes" subtitle = "" subhead = "You can find hidden treasures." current = "explore" tmp_kochs, next_page, prev_page = helpers.paginate( Koch.all().order('-created'), page ) kochs = helpers.get_kochs_data(tmp_kochs) last_from_all = Koch.get_random() self.response.out.write(template.render('templates/list_kochs.html', locals()))
def index(): selection = Question.query.order_by(Question.id.desc()).all() questions = paginate(request,selection) categories = Category.query.all() print([category.format() for category in categories]) return jsonify({ "data":True, "success":True, "total_questions":len(selection), "categories":[category.format() for category in categories], "questions":[q.format() for q in questions] })
def create_search_question(): ''' POST endpoing to create a new question or search for a questions, Creation requires the question and an answer text, category, and difficulty score. Search requires search term. ''' body = request.get_json() new_question = body.get('question', None) new_answer = body.get('answer', None) new_category = body.get('category', None) new_difficulty = body.get('difficulty', None) search_term = body.get('searchTerm', None) try: if search_term: questions = Question.query.filter( Question.question.ilike('%{}%'.format(search_term))).all() page = request.args.get('page', 1, type=int) page_questions = paginate(page, questions) if len(page_questions) == 0: abort(404) else: return jsonify({ 'success': True, 'questions': page_questions, 'total_questions': len(questions), 'current_category': 'All' }) else: if not new_question or not new_answer \ or not new_category or not new_difficulty: abort(400) else: question = Question( question=new_question, answer=new_answer, category=new_category, difficulty=int(new_difficulty) ) question.insert() return jsonify({ 'success': True, 'created': question.id, }) except: abort(422)
def get(self, star): user = User.is_logged(self) query = User.all().filter('nickname =', star.lower()).fetch(1) if len( query ) == 1: fan = query[0] title = "%s is following" % ( fan.nickname ) subhead = "Favorites cooks" page = self.request.get_range('page', min_value=0, max_value=1000, default=0) foll_tmp, next_page, prev_page = helpers.paginate( Friendship.all().filter('fan =', fan).order('-created'), page, 12 ) followers = helpers.get_following_data( foll_tmp ) last_from_all = Koch.get_random() self.response.out.write(template.render('templates/followers.html', locals())) else: self.error(404)
def pending_payments(request): pending_payments = Payment.objects.pending(user=request.user) page = request.GET.get('page') pending_payments = helpers.paginate(pending_payments, page, elems_perpage=10) params = { 'payments': pending_payments } if request.is_ajax(): response = render(request, 'wallets/payments_query.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: return render(request, 'wallets/pending_payments.html', params)
def admin_payments(request): payments = Payment.objects.all().order_by('-timestamp') page = request.GET.get('page') payments = helpers.paginate(payments, page, elems_perpage=10) params = { 'payments': payments, 'showing_all': True } if request.is_ajax(): response = render(request, 'payment/query.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: return render(request, 'payment/admin.html', params)
def user_wallet(request): pending_payments = Payment.objects.pending(user=request.user) wallet = Wallet.objects.filter(user=request.user).first() transactions = TransactionLog.objects.filter(wallet=wallet) page = request.GET.get('page') transactions = helpers.paginate(transactions, page, elems_perpage=10) if request.is_ajax(): response = render(request, 'wallets/transaction_logs_query.html', {'transactions':transactions}) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: return render(request, 'wallets/user_wallet.html', { 'pending_payments': pending_payments, 'showing_all': False, 'wallet': wallet, 'transactions': transactions })
def list_offers(request): offers = Offer.objects.all().order_by('-published_date').select_related('entity') page = request.GET.get('page') offers = helpers.paginate(offers, page, elems_perpage=6) params = { 'ajax_url': reverse('list_offers'), 'offers': offers, 'page': page } if request.is_ajax(): response = render(request, 'offers/search_results.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response else: return render(request, 'offers/list.html', params)
def retrieve_category_questions(category_id): ''' GET endpoint to get questions based on category.''' category = Category.query.filter_by(id=category_id).one_or_none() if category is None: abort(404) questions = Question.query.filter_by(category=str(category_id)).all() page = request.args.get('page', 1, type=int) page_questions = paginate(page, questions) if len(page_questions) == 0: abort(404) else: return jsonify({ 'success': True, 'questions': page_questions, 'total_questions': len(questions), 'current_category': category.type })
def search_users(request): users = User.objects.all() query_string = '' if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET.get('q') entry_query = helpers.get_query(query_string, ['username', 'first_name', 'last_name', 'email']) if entry_query: users = users.filter(entry_query) page = request.GET.get('page') users = helpers.paginate(users, page, 5) params = { 'ajax_url': reverse('search_users'), 'query_string': query_string, 'users': users, 'page': page } response = render(request, 'profile/search_results.html', params) response['Cache-Control'] = 'no-cache' response['Vary'] = 'Accept' return response
def get(self): try: if request.args: for key in request.args.keys(): if key not in ["q", "page", "limit"]: res = { "status": "error", "message": "You must provide q or page or limit in the query params" } return Response(json.dumps(res), status=400, mimetype="application/json") q = request.args.get('q') page = request.args.get('page') limit = request.args.get('limit') response = movies if q: response = search_movie(movies, q) if len(response) > 10: res = paginate(response) if q and page: res = paginate(data=response, page=int(page)) if q and limit: res = paginate(data=response, limit=int(limit)) if q and page and limit: res = paginate(data=response, page=int(page), limit=int(limit)) else: res = { "status": "success", "data": response, "total": len(response) } if page: page = int(page) res = paginate(response, page=page) if limit: limit = int(limit) res = paginate(response, limit=limit) if len(response) < limit: res = { "status": "error", "message": "Limit provided is too large" } return Response(res, status=400, mimetype="application/json") if page and limit: res = paginate(response, limit=limit, page=page) else: response = movies res = paginate(response) return Response(json.dumps(res), status=200, mimetype="application/json") except ValueError: res = {"status": "error", "message": "Invalid request"} return Response(json.dumps(res), status=400, mimetype="application/json")