def test_get_post_details(self): # get a post categories = Discourse().get_categories() posts = Discourse().get_category_posts(categories[0]) slug = posts[0]['slug'] post_details = Discourse().get_post_details(slug) self.assertTrue('id' in post_details) self.assertTrue('cooked' in post_details)
def setup(self): from pyprintplus import Log from _apis.models import Discourse, MediaWiki try: discourse_setup_done = Discourse(test=self.test).setup_done mediawiki_setup_done = MediaWiki(test=self.test).setup_done if not discourse_setup_done or not mediawiki_setup_done: Log().show_messages( ['Let\'s setup the search for your new website!']) if not discourse_setup_done: Discourse(test=self.test).setup() if not mediawiki_setup_done: MediaWiki(test=self.test).setup() Log().show_message('Search setup complete.') except KeyboardInterrupt: Log().show_message('Ok, canceled setup.')
def import_from_discourse(self): print('import_from_discourse()') from _database.models import Person from _apis.models import Discourse from dateutil import parser from _setup.models import Secret import time import requests from pyprintplus import Log DISCOURSE_URL = Secret('DISCOURSE.DISCOURSE_URL').value if DISCOURSE_URL: Log().show_message( '✅ Found DISCOURSE.DISCOURSE_URL - start importing Consensus Items from Discourse.' ) time.sleep(2) if requests.get(DISCOURSE_URL + '/c/consensus-items').status_code == 200: consensus_items = Discourse().get_category_posts( category='consensus-items', all_pages=True) print('process {} consensus-items'.format( len(consensus_items))) for consensus_item in consensus_items: if consensus_item[ 'title'] != 'About the Consensus Items category': Consensus().create( json_content={ 'str_name_en_US': consensus_item['title'], 'url_discourse': Secret('DISCOURSE.DISCOURSE_URL').value + 't/' + consensus_item['slug'], 'text_description_en_US': consensus_item['excerpt'], 'int_UNIXtime_created': round( datetime.timestamp( parser.parse( consensus_item['created_at']))), 'one_creator': Person.objects.get_discourse_creator( consensus_item['slug']), }) else: Log().show_message( 'WARNING: Can\'t find the "consensus-items" category on your Discourse. Skipped importing Consensus Items from Discourse.' ) time.sleep(4) else: Log().show_message( 'WARNING: Can\'t find the DISCOURSE.DISCOURSE_URL in your secrets.json. Will skip Discourse for now.' ) time.sleep(4)
def get_discourse_creator(self, slug): from _apis.models import Discourse from django.db.models import Q print('get_discourse_creator()') try: details = Discourse().get_post_details(slug) return self.filter( Q(str_name_en_US=details['name']) | Q(str_name_en_US=details['username'])).first() except: return None
def import_from_discourse(self, url=Secret('DISCOURSE.DISCOURSE_URL').value): Log().print('Event.objects.import_from_discourse(self)') from _apis.models import Discourse from dateutil.parser import parse from datetime import datetime from django.db.models import Q from _database.models import Event events = Discourse(url=url).get_category_posts('events', True) now = datetime.now() for event in events: if 'event' in event: date_start = parse(event['event']['start']) if date_start.year >= now.year and date_start.month >= now.month and date_start.day >= now.day: if Event.objects.filter( Q(str_name_en_US=event['title']) | Q(str_name_he_IL=event['title'])).exists( ) == False: event['description'] = Discourse( url=url).get_post_details(event['slug'])['cooked'] self.createEvent(event)
def import_from_discourse(self, DISCOURSE_URL=Secret( 'DISCOURSE.DISCOURSE_URL').value): from _apis.models import Discourse import time import requests from _setup.models import Log if DISCOURSE_URL: Log().show_message( '✅ Found DISCOURSE.DISCOURSE_URL - start importing persons from Discourse.' ) time.sleep(2) if requests.get(DISCOURSE_URL).status_code == 200: users = Discourse(url=DISCOURSE_URL).get_users() Log().print('--> process {} users'.format(len(users))) for user in users: Person().create( json_content={ 'str_name_en_US': user['user']['name'] if user['user']['name'] and user['user']['name'] != '' else user['user'] ['username'], 'url_featured_photo': DISCOURSE_URL + user['user'] ['avatar_template'].replace('{size}', '240'), 'url_discourse': DISCOURSE_URL + 'u/' + user['user']['username'], 'text_description_en_US': user['user']['title'] if user['user']['title'] != '' else None }) else: Log().show_message( 'WARNING: I can\'t access your Discourse page. Is the URL correct? Will skip Discourse for now.' ) time.sleep(4) else: Log().show_message( 'WARNING: Can\'t find the DISCOURSE.DISCOURSE_URL in your secrets.json. Will skip Discourse for now.' ) time.sleep(4)
def import_from_discourse(self, DISCOURSE_URL=Secret('DISCOURSE.DISCOURSE_URL').value): print('import_from_discourse()') from _database.models import Person from _apis.models import Discourse from datetime import datetime from dateutil import parser from _setup.models import Log import time import requests if DISCOURSE_URL: Log().show_message( '✅ Found DISCOURSE.DISCOURSE_URL - start importing projects from Discourse.') time.sleep(2) if requests.get(DISCOURSE_URL+'/c/projects').status_code == 200: projects = Discourse().get_category_posts( category='projects', all_pages=True) Log().print('--> process {} projects'.format(len(projects))) for project in projects: if project['title'] != 'About the Projects category': Project().create(json_content={ 'str_name_en_US': project['title'], 'url_featured_photo': project['image_url'] if project['image_url'] and '/uploads' in project['image_url'] else None, 'url_discourse': Secret('DISCOURSE.DISCOURSE_URL').value + 't/'+project['slug'], 'text_description_en_US': project['excerpt'] if 'excerpt' in project else None, 'one_creator': Person.objects.get_discourse_creator(project['slug']), 'int_UNIXtime_created': round(datetime.timestamp(parser.parse(project['created_at']))) } ) else: Log().show_message( 'WARNING: Can\'t find the "projects" category on your Discourse. Skipped importing Consensus Items from Discourse.') time.sleep(4) else: Log().show_message( 'WARNING: Can\'t find the DISCOURSE.DISCOURSE_URL in your secrets.json. Will skip Discourse for now.') time.sleep(4)
def test_search(self): search_results = Discourse().search('3d print') self.assertTrue(len(search_results) > 0) self.assertTrue(search_results[0]['name'])
def test_get_users(self): users = Discourse().get_users() self.assertTrue(type(users) == list) self.assertTrue('user' in users[0])
def test_get_category_posts(self): self.assertTrue(type(Discourse().get_category_posts('events')) == list)
def test_get_category_id(self): category_id = Discourse().get_category_id('events') self.assertTrue(category_id == None or type(category_id) == int)
def test_get_categories(self): categories = Discourse().get_categories() self.assertTrue(type(categories) == list) self.assertTrue(len(categories) > 0)
def test_create_and_delete_post(self): new_post_url = Discourse().create_post('This is an automated test', 'Unittests are awesome.', 'events') self.assertTrue(type(new_post_url) == str) self.assertTrue(Discourse().delete_post(new_post_url) == True)
def query(self, query, filter_name=None): self.log('query()') from _apis.models import Discourse, MediaWiki from _database.models import Person, Event, MeetingNote, Guilde, Machine, Space, Consensus, Project from _setup.models import Config if not query: return [] if filter_name and filter_name == 'hosts': return Person.objects.filter( Q(str_name_en_US__icontains=query) | Q(url_discourse__icontains=query)) # search in database events = Event.objects.filter( Q(boolean_approved=True) & (Q(str_name_en_US__icontains=query) | Q(text_description_en_US__icontains=query)) ).QUERYSET__upcoming() if filter_name == 'events': return events else: events = events.LIST__search_results()[:5] # search in social network accounts networks = [{ 'icon': x['name'].lower(), 'name': x['name'], 'url': x['url'], } for x in Config('SOCIAL.SOCIAL_NETWORKS').value if query.lower() in x['name'].lower()] internchannels = [{ 'icon': x['name'].lower(), 'name': x['name'], 'url': x['url'], } for x in Config('SOCIAL.INTERNAL_COMMUNICATION_PLATFORMS').value if query.lower() in x['name'].lower()] meeting_notes = MeetingNote.objects.filter( Q(text_date__icontains=query) | Q(text_keywords__icontains=query) ).past().LIST__search_results()[:5] guildes = Guilde.objects.filter( Q(str_name_en_US__icontains=query) | Q(text_description_en_US__icontains=query)).LIST__search_results( )[:5] machines = Machine.objects.filter( Q(str_name_en_US__icontains=query) | Q(text_description_en_US__icontains=query)).LIST__search_results( )[:5] spaces = Space.objects.filter( Q(str_name_en_US__icontains=query) | Q(text_description_en_US__icontains=query)).LIST__search_results( )[:5] consensus_items = Consensus.objects.filter( Q(str_name_en_US__icontains=query) | Q(text_description_en_US__icontains=query)).LIST__search_results( )[:5] projects = Project.objects.filter( Q(str_name_en_US__icontains=query) | Q(text_description_en_US__icontains=query)).LIST__search_results( )[:5] # search in wiki try: wiki_search_results = MediaWiki().search(query) except: self.log('-> ERROR: wiki search failed') wiki_search_results = [] # search in discourse try: discourse_search_results = Discourse().search(query) except: self.log('-> ERROR: discourse search failed') discourse_search_results = [] return networks + internchannels + events + guildes + machines + spaces + meeting_notes + consensus_items + projects + wiki_search_results + discourse_search_results
def config(self): from _apis.models import Discourse, MediaWiki return { "Discourse": Discourse().config, "MediaWiki": MediaWiki().config }
def setup_done(self): from _apis.models import Discourse, MediaWiki return True if Discourse().setup_done and MediaWiki( ).setup_done else False