def upload_view(request,what): log('upload_view(request,what={})'.format(what)) if what == 'event-image': if request.FILES['images[0]'].content_type!='image/jpeg' and request.FILES['images[0]'].content_type!='image/png': response = JsonResponse({ 'notification': get_template('components/notification.html').render({ 'text_message': 'Please upload a JPG or PNG image.', 'str_icon': 'error' })}) response.status_code = 500 else: import boto3 import os,sys if BOOLEAN__key_exists('AWS.ACCESS_KEYID') and BOOLEAN__key_exists('AWS.SECRET_ACCESS_KEY') and BOOLEAN__key_exists('AWS.S3.BUCKET_NAME') and BOOLEAN__key_exists('AWS.S3.SERVER_AREA'): AWS_S3_URL = STR__get_key('AWS.S3.BUCKET_NAME')+'.s3-' + \ STR__get_key('AWS.S3.SERVER_AREA')+'.amazonaws.com' session = boto3.Session( aws_access_key_id=STR__get_key('AWS.ACCESS_KEYID'), aws_secret_access_key=STR__get_key('AWS.SECRET_ACCESS_KEY'), ) s3 = session.resource('s3') image = request.FILES['images[0]'] s3.Bucket(STR__get_key('AWS.S3.BUCKET_NAME')).put_object(Key=image.name, Body=image,ACL='public-read') response = JsonResponse({'url_image': 'https://'+AWS_S3_URL+'/'+image.name}) else: log('--> AWS secrets are missing. Couldnt upload image.') response = JsonResponse({'url_image': None}) log('--> return response') return response
def delete_post(post_url): log('delete_post()') if BOOLEAN__key_exists('DISCOURSE.API_KEY') == False: log('--> Failed: DISCOURSE.API_KEY not set') return None # get ID of post response = requests.get(post_url) if response.status_code != 200: log('--> Couldn\'t find post on Discourse. Skipped deleting.') return False topic_id = response.url.split('/')[-1] response = requests.delete(DISCOURSE_URL+'/t/'+topic_id+'.json', headers={ 'content-type': 'application/json' }, params={ 'api_key': STR__get_key('DISCOURSE.API_KEY'), 'api_username': STR__get_key('DISCOURSE.API_USERNAME') }) if response.status_code == 200: log('--> Deleted') return True else: log('--> Not deleted') print(response.status_code) print(response.json()) return False
def create_post(str_headline, str_text, str_category): log('create_post()') from html import unescape import emoji if BOOLEAN__key_exists('DISCOURSE.API_KEY') == False: log('--> Failed: DISCOURSE.API_KEY not set') return None response = requests.post(DISCOURSE_URL+'posts.json', headers={ 'content-type': 'application/json' }, params={ 'api_key': STR__get_key('DISCOURSE.API_KEY'), 'api_username': STR__get_key('DISCOURSE.API_USERNAME'), 'title': emoji.get_emoji_regexp().sub(u'', unescape(str_headline)), 'raw': str_text, 'category': get_category_id(str_category) # TODO add event details # 'event': {'start': '2019-12-13T15:00:00+00:00', 'end': '2019-12-13T19:00:00+00:00'} }) if response.status_code == 200: if DISCOURSE_URL.endswith('/'): url = DISCOURSE_URL+'t/'+str(response.json()['topic_id']) log('--> Created Discourse post: '+url) return url else: url = DISCOURSE_URL+'/t/'+str(response.json()['topic_id']) log('--> Created Discourse post: '+url) return url else: print(response.status_code) print(response.json())
def send_message(message, channel='#general'): log('send_message(message, channel={})'.format(channel)) if BOOLEAN__key_exists('SLACK.API_TOKEN') == False: log('--> KEY MISSING: SLACK.API_TOKEN not defined. Couldnt sent notification via Slack.' ) else: client = slack.WebClient(token=STR__get_key('SLACK.API_TOKEN')) # see https://github.com/slackapi/python-slackclient#sending-a-message-to-slack response = client.chat_postMessage(channel=channel, text=message) if response['ok'] == True: log('--> Success! Sent message to Slack') else: log('--> Failed!')
def BOOLEAN__API_access_works(): log('BOOLEAN__API_access_works()') if BOOLEAN__key_exists('DISCOURSE.API_KEY') == False: log('--> Failed: DISCOURSE.API_KEY not set') return None response = requests.get(DISCOURSE_URL+'notifications.json', headers={ 'content-type': 'multipart/form-data' }, params={ 'api_key': STR__get_key('DISCOURSE.API_KEY'), 'api_username': STR__get_key('DISCOURSE.API_USERNAME'), }) if response.status_code == 200: return True else: print(response.status_code) print(response.json()) return False
def new_view(request): log('new_view(request)') if request.method == 'GET' and request.GET.get('what', None) == 'meeting': new_meeting = MeetingNote() new_meeting.save() response = JsonResponse( { 'html': get_template( 'components/body/meetings/current_meeting.html').render({ 'language':getLanguage(request), 'current_meeting': new_meeting }) } ) elif request.method == 'POST' and request.POST.get('what', None) == 'event': from hackerspace.APIs.slack import send_message from hackerspace.Website.views_helper_functions import INT__UNIX_from_date_and_time_STR,INT__duration_minutes DOMAIN = get_config('WEBSITE.DOMAIN') int_UNIXtime_event_start = INT__UNIX_from_date_and_time_STR(request.POST.get('date',None),request.POST.get('time',None)) int_minutes_duration = INT__duration_minutes(request.POST.get('duration',None)) try: if request.FILES['images[0]'].content_type=='image/jpeg' or request.FILES['images[0]'].content_type=='image/png': image = request.FILES['images[0]'] else: image = None except: image= None uploaded_photo_url = request.POST.get('photo',None) new_event = Event( boolean_approved=request.user.is_authenticated, str_name_en_US=request.POST.get('name_english',None), str_name_he_IL=request.POST.get('name_hebrew',None), int_UNIXtime_event_start=int_UNIXtime_event_start, int_minutes_duration=int_minutes_duration, int_UNIXtime_event_end=int_UNIXtime_event_start+(60*int_minutes_duration), url_featured_photo=uploaded_photo_url if 'https' in uploaded_photo_url else None, image_featured_photo=image, text_description_en_US=request.POST.get('description_english',None), text_description_he_IL=request.POST.get('description_hebrew',None), one_space=Space.objects.QUERYSET__by_name(request.POST.get('space',None)), one_guilde=Guilde.objects.QUERYSET__by_name(request.POST.get('guilde',None)), str_crowd_size=request.POST.get('expected_crowd',None), str_welcomer=request.POST.get('event_welcomer',None), boolean_looking_for_volunteers=True if request.POST.get('volunteers',None)=='yes' else False ) if request.POST.get('location',None): if request.POST.get('location',None)!=get_config('BASICS.NAME'): new_event.str_location = request.POST.get('location',None) if request.POST.get('repeating',None): # if repeating, mark as such and auto generate new upcoming events with "update_database" command str_repeating_how_often = request.POST.get('repeating',None) str_repeating_up_to = request.POST.get('repeating_up_to',None) if str_repeating_how_often and str_repeating_how_often!='': new_event.int_series_startUNIX = new_event.int_UNIXtime_event_start new_event.str_series_repeat_how_often = str_repeating_how_often if str_repeating_up_to and str_repeating_up_to!='': new_event.int_series_endUNIX = INT__UNIX_from_date_and_time_STR(str_repeating_up_to,request.POST.get('time',None)) new_event.save() hosts = request.POST.get('hosts',None) if hosts: if hosts.startswith(','): hosts = hosts[1:] hosts = hosts.split(',') for host in hosts: new_event.many_hosts.add(Person.objects.by_url_discourse(host)) # if loggedin user: share event to other platforms (Meetup, Discourse, etc.) if request.user.is_authenticated: new_event.create_discourse_event() new_event.create_meetup_event() # else (if event created via live website) inform via slack about new event and give community chance to delete it or confirm it elif 'HTTP_HOST' in request.META and request.META['HTTP_HOST']==DOMAIN: send_message( 'A website visitor created a new event via our website.\n'+ 'If no one deletes it within the next 24 hours, it will be automatically published and appears in our website search'+(', meetup group' if STR__get_key('MEETUP.ACCESS_TOKEN') else '')+(' and discourse' if BOOLEAN__key_exists('DISCOURSE.API_KEY')==True else '')+'.\n'+ '🚫-> Does this event already exist or is it spam? Open on the following event link and click "Delete event".\n'+ '✅-> You have a user account for our website and want to publish the event directly? Open on the following event link and click "Approve event".\n'+ 'https://'+DOMAIN+'/'+new_event.str_slug ) else: log('--> Request not sent via hackerspace domain. Skipped notifying via Slack.') # if event is repeating, create upcoming instances new_event = new_event.create_upcoming_instances() # if user is signed in and event autoapproved - direct to event page, else show info response = JsonResponse( { 'url_next': '/'+new_event.str_slug } ) log('--> return response') return response
def key_exists(str_key_name): return BOOLEAN__key_exists(str_key_name)