Squashed commit of the following:

commit 99fd94580f95dcbfb77b73e2de846f76a5709ef9
Author: Sonny <sonnyba871@gmail.com>
Date:   Sat Feb 15 21:45:16 2020 +0100

    Use postgres password

    As of https://gitlab.com/gitlab-com/support-forum/issues/5199
This commit is contained in:
sonny 2020-07-12 20:26:14 +02:00
parent 391796a0c0
commit 177755e302
66 changed files with 8955 additions and 372 deletions

View file

@ -1,5 +1,7 @@
from datetime import datetime
from django.db.models.fields import CharField, TextField
from django.template.defaultfilters import truncatechars
from django.utils import timezone
import pytz
@ -10,6 +12,9 @@ from requests.exceptions import RequestException
from newsreader.news.collection.response_handler import ResponseHandler
DEFAULT_HEADERS = {"User-Agent": "linux:rss.fudiggity.nl:v0.2"}
def build_publication_date(dt, tz):
try:
naive_datetime = datetime(*dt[:6])
@ -20,12 +25,46 @@ def build_publication_date(dt, tz):
return published_parsed.astimezone(pytz.utc)
def fetch(url):
def fetch(url, headers={}):
headers = {**DEFAULT_HEADERS, **headers}
with ResponseHandler() as response_handler:
try:
response = requests.get(url)
response = requests.get(url, headers=headers)
response_handler.handle_response(response)
except RequestException as exception:
response_handler.handle_exception(exception)
response_handler.map_exception(exception)
return response
def post(url, data=None, auth=None, headers={}):
headers = {**DEFAULT_HEADERS, **headers}
with ResponseHandler() as response_handler:
try:
response = requests.post(url, data=data, auth=auth, headers=headers)
response_handler.handle_response(response)
except RequestException as exception:
response_handler.map_exception(exception)
return response
def truncate_text(cls, field_name, value):
field = cls._meta.get_field(field_name)
max_length = field.max_length
field_cls = type(field)
is_charfield = bool(issubclass(field_cls, CharField))
is_textfield = bool(issubclass(field_cls, TextField))
if not value or not max_length:
return value
elif not is_charfield or is_textfield:
return value
if len(value) > max_length:
return truncatechars(value, max_length)
return value