def parse_page(source, author_id): quote_regex = re.compile('<div class="quoteText">.*?ldquo;(.*?).rdquo;', re.DOTALL) quotelist = quote_regex.findall(source) global langcount for i in range(len(quotelist)): quotelist[i] = unicode(quotelist[i], 'utf-8') ''' quotelist[i] = quotelist[i].replace("<br />", "\n") quotelist[i] = quotelist[i].replace("<br/>", "\n") quotelist[i] = quotelist[i].replace("<br>", "\n") quotelist[i] = quotelist[i].replace("<em>", "") quotelist[i] = quotelist[i].replace("</em>", "") quotelist[i] = quotelist[i].replace("&","&") ''' try: language = detect(quotelist[i]) except:# UnicodeDecodeError: #TODO: handle LangDetectException language = "?" if language not in langcount.keys(): langcount[language] = 1 else: langcount[language] = langcount[language] + 1 #TODO: Add support for many languages, and choosing which language you want displayed #(currently only english is supported) if language == "en": quote = Quote(quote = quotelist[i], author_id = author_id, language = language) quote.save()
def save(self): lines = [Line.parse(l) for l in self.cleaned_data['quote'].splitlines()] quote = Quote() quote.save() for line in lines: line.quote = quote line.save() return quote
def handle(self, *args, **options): with open(options['json_file']) as json_file: data = json.load(json_file) for quote_data in data: author, _ = Author.objects.get_or_create(name=quote_data['author']) quote = Quote() quote.text = quote_data['text'] quote.author = author quote.save() print('Done')
def test_used_in_the_future(self): """ No quotes may be used in the future... Save a quote used in the future (1 day) and check if it is in the future """ q = Quote(quote="Future", used=timezone.now()+timedelta(days=1)) q.save() # If used is in the future this will be negative dt = (timezone.now() - q.used).total_seconds() self.assertGreaterEqual(dt, 0, msg="The quote is %F sec in the future" % (dt*-1))
def save(self): lines = [ Line.parse(l) for l in self.cleaned_data['quote'].splitlines() ] quote = Quote() quote.save() for line in lines: line.quote = quote line.save() return quote
def quotesformview(request): if request.method == 'POST': if request.POST.get('title') and request.POST.get( 'person') and request.POST.get('quote'): quote = Quote() #quote_cate = QuoteCategory() quote.title = request.POST.get('title') quote.author = request.POST.get('person') quote.quote = request.POST.get('quote') quote.owner = request.POST.get('owner') quote.save() return redirect('quotes') else: return render(request, 'quotesform.html')
def SubmitQuote(request): current_year = now().year if request.method == 'POST': # Create form instance and pull in data from the front-end form = QuoteSubmissionForm(request.POST) # Make sure submitted info is valid if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] phone = form.cleaned_data['phone'] comments = form.cleaned_data['comments'] try: # Try to save DB Record new_record = Quote( first_name=first_name, last_name=last_name, email=email, phone=phone, comments=comments, date_requested=now(), requiresResponse=False, closed=False, cost=0 ) # Save new quote record entry new_record.save() # Build E-Mail Templates generated_html = render_to_string('quotes/email_html.html', {'quote': new_record}) generated_txt = render_to_string('quotes/email_text.html', {'quote': new_record}) # Get first name to pass to /thanks first_name = str(new_record.first_name) sendMailToContacts(first_name, generated_html, generated_txt) except Exception as err: log(err) return HttpResponseRedirect('/thanks/?r=' + str(first_name)) else: form = QuoteSubmissionForm() template_name = 'quotes/submitQuote.html' return render(request, template_name, {'form': form, 'current_year': current_year})
def add_quote_shortcut(request): payload = json.loads(unquote(str(request.body))[10:-1]) print(payload) added_by_userid = payload['user']['id'] added_by_username = payload['user']['name'] teamid = payload['user']['team_id'] quote = payload['message']['text'].replace('+', ' ') q = Quote(added_by_userid=added_by_userid, added_by_username=added_by_username, teamid=teamid, text=quote, channel="#slack") q.save() return HttpResponse(status=status.HTTP_200_OK)
def add_quote(request): body = str(request.body) if body.find('&') == -1: payload = json.loads(unquote(body)[10:-1]) print(payload) try: added_by_userid = payload['user']['id'] added_by_username = payload['user']['name'] teamid = payload['user']['team_id'] quote = payload['message']['text'].replace('+', ' ') except KeyError: data = { "response_type": "in_channel", "text": f"You need to provide a quote to add!", } return JsonResponse(data) else: payload = dict(item.split("=") for item in body.split('&')) try: added_by_userid = payload['user_id'] added_by_username = payload['user_name'] teamid = payload['team_id'] quote = payload['text'] except KeyError: data = { "response_type": "in_channel", "text": f"You need to provide a quote to add!", } return JsonResponse(data) q = Quote(added_by_userid=added_by_userid, added_by_username=added_by_username, teamid=teamid, text=quote, channel="#slack") q.save() data = { "response_type": "in_channel", "text": f"Quote added as #{q.id}", } return JsonResponse(data)
def add(request, quote, author): a = Author(name=author) a.save() q = Quote(quotation=quote, author=a, category=1) q.save() return HttpResponse("SUCCESS! added \"%s\" - %s." % (q.quotation, q.author.name))
def test_create(self): ts = Quote(author="A", text="ABC") ts.save() self.assertEqual(str(ts), "A ABC")
def home(request): """ Displays list of generated quotes from quotesondesign. """ if request.method == "GET": posts = request.GET.get('posts', 4) page = request.GET.get('page', 1) # response = requests.get("https://quotesondesign.com/wp-json/wp/v2/posts?filter[orderby]=rand&filter[posts_per_page]=30") response = requests.get( "https://quotesondesign.com/wp-json/wp/v2/posts/?orderby=rand&filter[posts_per_page]=30" ) data = response.json() quotes = [] for dict_item in data: quote = Quote() quote.content = list( map(lambda d: parse_content(d), [ value['rendered'] for key, value in dict_item.items() if key == "content" ]))[0] # temporary update after API v5.0 quote.author = dict_item['title'][ 'rendered'] # temporary update after API v5.0 check = Quote.objects.filter(content=quote.content).all( ) # returns Queryset if quote content exists if not check: quote.save() else: quote.id = check.values("id")[0][ 'id'] # If in database, store existing quote id into object sent for template rendering q = Quote.objects.get( id=quote.id) # fetches quote from database quote.no_user_likes = q.like_set.all().count( ) # generate number of user likes for particular quote quote.no_user_favourites = q.favourite_set.all().count( ) # generate number of user likes for particular quote quote.check_liked = check.values("check_liked")[0][ "check_liked"] # check if quote has been liked by logged in user quote.check_favourited = check.values("check_favourited")[0][ "check_favourited"] # check if quote has been favourited by logged in user quotes.append(quote) # Paginate quotes quotes = quotes_paginator(quotes, posts, page) return render( request, 'quotes/home.html', { 'quotes': quotes, 'posts': posts, 'page': page, 'max_posts': list(range(3, 6)) }) elif request.method == "POST": # URL queries are always fetched via GET, not POST; request.POST.get() is only for retrieving from request.body posts = request.GET.get('posts', 4) page = request.GET.get('page', 1) if request.user is not AnonymousUser and isinstance( request.POST.get('submit_user_like'), str): quote_id = request.POST.get("submit_user_like") Like.objects.get_or_create( user=request.user, quote_id=quote_id ) # Creates Like entry in table quotes_like. Outputs (Like object, boolean on whether created) quote = Quote.objects.get(id=quote_id) if (quote.check_liked == True): quote.check_liked = False quote.save() Like.objects.filter(quote=quote_id).delete() messages.success(request, "Unliked!") else: quote.check_liked = True quote.save() messages.success(request, "Liked!") elif request.user is not AnonymousUser and isinstance( request.POST.get('submit_user_favourite'), str): quote_id = request.POST.get("submit_user_favourite") Favourite.objects.get_or_create( user=request.user, quote_id=request.POST.get('submit_user_favourite')) quote = Quote.objects.get(id=quote_id) if (quote.check_favourited == True): quote.check_favourited = False quote.save() Favourite.objects.filter(quote=quote_id).delete() messages.success(request, "Removed from favourites!") else: quote.check_favourited = True quote.save() messages.success(request, "Favourited!") # Build redirect string back to 'quotes-home' with 'posts' and 'page' url query base_url = reverse('quotes-home') query_string = urlencode({'posts': posts, 'page': page}) url = '{}?{}'.format(base_url, query_string) return redirect(url)
def save(self): data = self.cleaned_data quote = Quote(body=data['body'], sayer=data['sayer'], user=self.user) quote.save()
def handle(self, *args, **options): files = [] if os.path.isdir(settings.PREV_QUOTES_DIR): dcmp = dircmp(settings.PREV_QUOTES_DIR, settings.NEW_QUOTES_DIR) # Process subdirs common to both dir for subdir, sub_dcmp in dcmp.subdirs.items(): temp = sub_dcmp.right_only temp = [os.path.join(subdir, x) for x in temp] files.extend(temp) files = [os.path.join(settings.NEW_QUOTES_DIR, x) for x in files] # Process subdirs not present in PREV_QUOTES_DIR for subdir in dcmp.right_only: path = os.path.join(settings.NEW_QUOTES_DIR, subdir) if os.path.isdir(path): files.extend(glob.glob(os.path.join(path, '*.csv'))) else: files = glob.glob(os.path.join(settings.NEW_QUOTES_DIR, '*', '*.csv')) for afile in files: with open(afile, 'r') as f: created_obj = updated_obj = 0 logger.info('Processing {0}'.format(afile)) reader = csv.reader(f) for row in reader: try: symbol, quote_date, price_open, price_high, price_low, price_close, volume, value = row quote_date = datetime.strptime(quote_date, '%m/%d/%Y') if symbol.startswith('^'): name = symbol company, created = Company.objects.get_or_create(name=name) else: company, created = Company.objects.get_or_create(symbol=symbol) created = False try: quote = Quote.objects.get( company=company, quote_date=quote_date, ) except Quote.DoesNotExist: quote = Quote( company=company, quote_date=quote_date, ) created = True quote.price_open = price_open quote.price_high = price_high quote.price_low = price_low quote.price_close = price_close quote.volume = volume quote.save() if created: created_obj += 1 else: updated_obj += 1 except Exception, e: logger.error(row) logger.error(e) continue logger.info('{0} created records. {1} updated records.'.format(created_obj, updated_obj))
def add_quote(self, channel, nick, host, *args): """adds quote to database""" q = Quote(nick=nick, host=host, channel=channel, added=datetime.datetime.now(), quote=' '.join(args)) q.save() return 'added quote %d' % q.id