def get_bookmarks(request): try: profile = request.user.profile results = profile.get_bookmarks() serialized = BookmarkSerializer(results, many=True).data return JsonResponse({ 'status': 'ok', 'result': serialized, }, status=200) except Exception as e: print(e) return not_authorized()
def bookmark(request, entity='news', entity_id=None): # GET return status of a bookmark (ES: {bookmarked:true|false}) # POST toggle status of a bookmark an return it (ES: {bookmarked:true|false}) results = {} try: local_entity = None profile = request.user.profile try: local_entity = ModelHelper.find_this_entity(entity, entity_id) except ObjectDoesNotExist as odne: return not_found() if request.method == 'POST': results['bookmarked'] = profile.bookmark_this(local_entity) else: results['bookmarked'] = profile.is_this_bookmarked_by_me( local_entity) return success('ok', 'bookmark', results) except AttributeError as a: return not_authorized() except Exception as e: print(e) return not_authorized()
def interest(request, entity='news', entity_id=None): ''' :param request: :param entity: :param entity_id: :return: GET mode If request's user exists, the api will return all user interested in the entity specified If request is from an anonymous users, just the number of interested people is will be returned POST mode(only for logged) Toggle the interest for the specified entity for the logged user ''' results = {} local_entity = None try: #logged user profile = request.user.profile try: local_entity = ModelHelper.find_this_entity(entity, entity_id) except ObjectDoesNotExist as odne: return not_found() if request.method == 'GET': results['iaminterested'] = profile.is_this_interested_by_me( local_entity) results['interested'] = ProfileSerializer( local_entity.interested(), many=True).data results['interested_counter'] = len(local_entity.interested()) else: # Toggle interest results['iaminterested'] = profile.interest_this(local_entity) results['interested'] = ProfileSerializer( local_entity.interested(), many=True).data results['interested_counter'] = len(local_entity.interested()) return JsonResponse({ 'status': 'ok', 'result': results, }, status=200) except Exception as e: #Anonymous user if request.method == 'GET': local_entity = ModelHelper.find_this_entity(entity, entity_id) results['interested_counter'] = len(local_entity.interested()) return JsonResponse({ 'status': 'ok', 'result': results, }, status=202) else: return not_authorized()
def update_field(request, to_be_updated, update_token): import threading if not update_token == settings.UPDATE_TOKEN: return not_authorized() if to_be_updated == 'crm': users = User.objects.all() thr = threading.Thread(target=create_or_update_party, kwargs=dict(users=users)) thr.start() if to_be_updated == 'default_img': # update default images users = User.objects.all() thr = threading.Thread(target=update_default_profile_image, kwargs=dict(users=users)) thr.start() return JsonResponse({ 'status': 'ok', 'updating': to_be_updated }, status=200)
def project(request, project_id=None): from dashboard.serializer import ProjectSerializer # DELETE PROJECT if request.method == 'DELETE' and project_id is not None: try: profile = request.user.profile projects = Project.objects.get(id=project_id, profile=profile) projects.delete() except ObjectDoesNotExist as e: print(e) print(not_authorized()) return success('ok', 'project deleted', {}) # GET ALL if request.method == 'GET' and project_id is None: try: profile = request.user.profile projects = Project.objects.filter(profile=profile) serialized = ProjectSerializer(projects, many=True) return success('ok', 'user projects', serialized.data) except Exception as e: print(e) return not_found() # GET SINGLE if request.method == 'GET' and project_id is not None: try: project = Project.objects.filter(id=project_id) serialized = ProjectSerializer(project, many=True) return success('ok', 'single project', serialized.data) except ObjectDoesNotExist as o: print(o) return not_found() except Exception as e: print(e) return error() # UPDATE if request.method == 'POST' and project_id is not None: data_to_update = {} data_to_update['name'] = request.POST.get('name', '') data_to_update['description'] = request.POST.get('description', '') data_to_update['start_date'] = request.POST.get('start_date', '') data_to_update['creator_role'] = request.POST.get( 'creator_role', '') data_to_update['project_url'] = request.POST.get('project_url', '') end_date = request.POST.get('end_date', '') # check if the image needs to be updated if request.POST.get('picture', None) != '': # image to be updated picture = request.FILES.get('picture') v13.check_image(picture) data_to_update['picture'] = picture # check if is or not an ongoing project try: if end_date != '': end_date = dt.strptime(end_date, '%Y-%m-%d') data_to_update['start_date'] = dt.strptime( data_to_update['start_date'], '%Y-%m-%d') # if end_date > dt.now(): # return bad_request('The project end_date cannot be in the future') if end_date < data_to_update['start_date']: return bad_request( 'The project end date cannot be before the project start date' ) data_to_update['end_date'] = end_date except Exception as e: print(e) return error() # get the model object and check the profile owns that project try: profile = request.user.profile project = Project.objects.filter(id=project_id, profile=profile).first() # clear and update tag project.set_tags(request.POST.get('tags', '')) # remove tag from data_to_update project.__dict__.update(data_to_update) project.save() except Project.DoesNotExist as e: print(e) return not_authorized() except Exception as e: print(e) return error() result = ProjectSerializer(project).data return success('ok', 'project updated', result) # CREATE if request.method == 'POST' and project_id is None: # check if fields are filled try: project_image = request.FILES.get('picture') project_name = request.POST['name'] project_description = request.POST['description'] project_start_date = dt.strptime(request.POST['start_date'], '%Y-%m-%d') project_creator_role = request.POST['creator_role'] project_url = request.POST['project_url'] project_tags = request.POST['tags'] # Check image is_image_ok = v13.check_image(project_image) if is_image_ok is not True: return is_image_ok except KeyError as k: print(k) return bad_request("Please fill all the fields") except Exception as e: return bad_request(e) # check if is or not an ongoing project try: project_end_date = dt.strptime(request.POST['end_date'], '%Y-%m-%d') except KeyError: project_end_date = None # if it is not an ongoing project check dates if project_end_date is not None: if project_end_date < project_start_date: return bad_request( 'The project END DATE cannot be before the project START DATE' ) try: # check user has not project with that name profile = request.user.profile projects = Project.objects.filter(profile=profile, name=project_name) if len(projects) > 0: return bad_request('Project name already exist') project = Project(profile=profile, name=project_name, picture=project_image, description=project_description, start_date=project_start_date, end_date=project_end_date, creator_role=project_creator_role, project_url=project_url) project.save() project.tags.clear() for tagName in [ x.lower().capitalize() for x in project_tags.split(",") ]: project.tags.add( Tag.objects.filter(name=tagName).first() or Tag.create(name=tagName)) project.save() result = ProjectSerializer(project).data except Exception as e: return bad_request(e) return success('ok', 'project created', result)
def post_om_invitation(request): if request.method != 'POST': return not_authorized() try: sender_first_name = request.POST['sender_first_name'].title() sender_last_name = request.POST['sender_last_name'].title() sender_email = request.POST['sender_email'].lower() receiver_first_name = request.POST['receiver_first_name'].title() receiver_last_name = request.POST['receiver_last_name'].title() receiver_email = request.POST['receiver_email'].lower() if sender_first_name.strip() == '' \ or sender_last_name.strip() == '' \ or sender_email.strip() == '' \ or receiver_first_name.strip() == '' \ or receiver_last_name.strip() == '' \ or receiver_email.strip() == '': return bad_request("Please fill al the fields") # Return to dsp error page if sender is already a DSP user? try: User.objects.get(email=sender_email) return HttpResponseRedirect('http://openmaker.eu/error_sender/') except User.DoesNotExist: pass Invitation.create(sender_email=sender_email, sender_first_name=sender_first_name, sender_last_name=sender_last_name, receiver_first_name=receiver_first_name, receiver_last_name=receiver_last_name, receiver_email=receiver_email, sender_verified=False) activation_link = 'http://{}/om_confirmation/{}/{}/{}/{}/{}/{}'.format( get_current_site(request), sender_first_name.encode('utf-8').encode('base64'), sender_last_name.encode('utf-8').encode('base64'), sender_email.encode('base64'), receiver_first_name.encode('utf-8').encode('base64'), receiver_last_name.encode('utf-8').encode('base64'), receiver_email.encode('base64')) EmailHelper.email(template_name='invitation_email_confirm', title='OpenMaker Nomination.. almost done!', vars={ 'SENDER_NAME': sender_first_name, 'CONFIRMATION_LINK': activation_link }, receiver_email=sender_email) except KeyError: return bad_request("Please fill al the fields") except EmailAlreadyUsed: return HttpResponseRedirect('http://openmaker.eu/error_receiver/') except UserAlreadyInvited: return HttpResponseRedirect('http://openmaker.eu/error_invitation/') except SelfInvitation: # @TODO : make appropriate page in openmaker return bad_request("Sender and receiver must be different") except Exception as e: #@TODO : make appropriate page in openmaker return bad_request("Some erro occour please try again") return HttpResponseRedirect('http://openmaker.eu/pending_invitation/')