From da4da03148059580be9c5c8b25cee1561d63e189 Mon Sep 17 00:00:00 2001 From: Alisha Mohanty Date: Mar 24 2019 20:38:17 +0000 Subject: Search option to send Happiness Packet to Specific FAS account --- diff --git a/.gitignore b/.gitignore index b05fefb..7b49c56 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,6 @@ syntax: glob #OIDC credentials client_secrets.json + +#Adding fas-admin-details.json so that the username and password does not get pushed +fas-admin-details.json diff --git a/assets/css/custom.css b/assets/css/custom.css index 1130691..9bea875 100644 --- a/assets/css/custom.css +++ b/assets/css/custom.css @@ -324,3 +324,12 @@ blockquote { .socio-buttons a { padding: 0.9rem; } +.row .error{ + color: red; + font-size: 1.4rem; + text-align: center; +} +.row .searching-text{ + text-align: center; + font-size:1.4rem; +} diff --git a/assets/js/fas_details.js b/assets/js/fas_details.js new file mode 100644 index 0000000..46d7145 --- /dev/null +++ b/assets/js/fas_details.js @@ -0,0 +1,64 @@ +$(document).ready(function () { + $("#id_fasid").val('') + $("#id_recipient_name").val('') + $("#id_recipient_email").val('') + + $("#id_fasid").change(function () { + + let fasid = $('#id_fasid').val() + $("#server-error").remove() + + if (fasid != '') { + + $("#id_recipient_name").attr("placeholder", "") + $("#id_recipient_name").val('') + $("#id_recipient_email").val('') + $("#id_fasid").prop('disabled', true); + $("#no-fas-id-error").remove() + $("#server-error").remove() + $("#id_fasid").after('

Searching for FAS Username.........

') + + email = $.get('/send/search', { fasid: fasid }, function (data) { + + if (data['server_error'] == 'True') { + $("#id_fasid").val('') + $('#server-error').remove() + $('.searching-text').remove() + $("#id_fasid").prop('disabled', false); + $("#id_fasid").after('

Internal Server Error Occured! Enter Name and Email manually!

') + $('#server-error').fadeIn('slow', function () { + $('#server-error').delay(3000).fadeOut() + }) + } + else { + $('.error').remove() + $('.searching-text').remove() + $("#id_fasid").prop('disabled', false); + + if (data['account_exists'] == 'Yes') { + + if (data['name'] == 'No name') { + $("#id_recipient_name").attr("placeholder", "Privacy is Set! Type Name Manually") + } + + else { + $("#id_recipient_name").val(data['name']) + } + + $("#id_recipient_email").val(data['email']) + } + else { + $("#no-fas-id-error").remove() + $("#id_fasid").after('

Sorry! No such FAS Username exsist

') + console.log('No such FAS username exsists') + $('#no-fas-id-error').fadeIn('slow', function () { + $('#no-fas-id-error').delay(2700).fadeOut() + }) + $("#id_fasid").val('') + } + return data; + } + }) + } + }) +}); diff --git a/docs/setup/development.rst b/docs/setup/development.rst index 4ed8d77..442b5bd 100644 --- a/docs/setup/development.rst +++ b/docs/setup/development.rst @@ -32,6 +32,8 @@ The project comes with a Dockerfile that allows easy deployment of a web server. ./generate_client_secrets.sh +#. Create a fas-admin-details.json file and add a json object with your FAS-Username and FAS-Password. See fas-admin-details.json.example. + Although the Dockerfile runs the script to check if a client_secrets.json file is present, please generate it before starting the Docker container, so that client secrets are not being constantly generated every time the image is rebuilt. In order to run the web server, alongside the Redis queue and celery worker instance, simply run ``docker-compose up``. diff --git a/fas-admin-details.json.example b/fas-admin-details.json.example new file mode 100644 index 0000000..8659be7 --- /dev/null +++ b/fas-admin-details.json.example @@ -0,0 +1,5 @@ +#Create fas-admin-details.json and store the following json object into the file with your actual credentials. +{ + "ADMIN_USERNAME":"", + "ADMIN_PASSWORD":"" +} diff --git a/happinesspackets/messaging/forms.py b/happinesspackets/messaging/forms.py index 1c6e822..b9f9636 100644 --- a/happinesspackets/messaging/forms.py +++ b/happinesspackets/messaging/forms.py @@ -27,6 +27,7 @@ def validate_email(email): class MessageSendForm(forms.ModelForm): # hp = forms.CharField(label="do not fill", required=False) + fasid = forms.CharField(label="FAS Username", required=False) class Meta: model = Message @@ -52,6 +53,7 @@ class MessageSendForm(forms.ModelForm): self.helper.layout = Layout( # Fieldset('This Happiness Packet is from...', 'sender_name', 'sender_email', 'hp'), + Fieldset("Search for a FAS Username", 'fasid' ), Fieldset("Send this Happiness Packet to...", 'recipient_name', 'recipient_email'), Fieldset("Your message is...", 'message'), Fieldset("Privacy and permissions", 'sender_named', 'sender_approved_public', 'sender_approved_public_named'), diff --git a/happinesspackets/messaging/urls.py b/happinesspackets/messaging/urls.py index 0e89d37..ec2a51a 100644 --- a/happinesspackets/messaging/urls.py +++ b/happinesspackets/messaging/urls.py @@ -4,7 +4,7 @@ from django.urls import re_path from .views import (StartView, MessageSearchView, MessageSendView, MessageSenderConfirmationSentView, MessageSenderConfirmationView, MessageSenderConfirmedView, MessageRecipientMessageUpdate, FaqView, ArchiveView, InspirationView, - BlacklistEmailView, ReceivedMessagesView, SentMessagesView) + BlacklistEmailView, ReceivedMessagesView, SentMessagesView, FasidSearchView) app_name = 'messaging' @@ -21,5 +21,6 @@ urlpatterns = [ re_path(r'^send/confirmation/(?P[\w-]+)/(?P[\w-]+)/$', MessageSenderConfirmationView.as_view(), name='sender_confirm'), re_path(r'^send/confirmed/$', MessageSenderConfirmedView.as_view(), name='sender_confirmed'), re_path(r'^recipient/(?P[\w-]+)/(?P[\w-]+)/$', MessageRecipientMessageUpdate.as_view(), name='recipient_message_update'), + re_path(r'^send/search/$', FasidSearchView.fasidCheck, name='fasid_check'), re_path(r'^search/?$', MessageSearchView.as_view(), name='search'), ] diff --git a/happinesspackets/messaging/views.py b/happinesspackets/messaging/views.py index 597e9b5..bd02bca 100644 --- a/happinesspackets/messaging/views.py +++ b/happinesspackets/messaging/views.py @@ -5,7 +5,7 @@ import logging from django.contrib import messages from django.urls import reverse, reverse_lazy -from django.http import HttpResponseRedirect, Http404 +from django.http import HttpResponseRedirect, Http404, JsonResponse from django.utils.crypto import salted_hmac, constant_time_compare from django.utils.decorators import method_decorator from django.utils.html import format_html @@ -14,6 +14,7 @@ from django.views.generic import FormView, TemplateView, UpdateView, ListView from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models import Q from django.shortcuts import render +from django.conf import settings from haystack.generic_views import SearchView from haystack.forms import SearchForm @@ -26,6 +27,10 @@ from fedora_messaging.config import conf from fedora_messaging.exceptions import PublishReturned, ConnectionException from happinesspacket_schema.schema import MessageV1 +#Include python-fedora +from fedora.client.fas2 import AccountSystem +from fedora.client import AuthError + logger = logging.getLogger(__name__) class MessageSearchView(SearchView): @@ -141,7 +146,7 @@ class MessageSenderConfirmationView(TemplateView): "id": message.identifier, "sender": sender_name, "recipient": message.recipient_name - } + } ) try: publish(message) @@ -199,3 +204,33 @@ class SentMessagesView(UserMessageView): def get_queryset(self): queryset = super(SentMessagesView, self).get_queryset() return queryset.filter(sender_email=self.request.user.email) + +class FasidSearchView(): + @staticmethod + def fasidCheck(request): + try: + fas = AccountSystem(username= settings.ADMIN_USERNAME, password= settings.ADMIN_PASSWORD) + fasid = request.GET['fasid'] + is_server_error = 'False' + type_of_error = ' No Error ' + person = fas.person_by_username(fasid) + u_name = 'No name' + u_email = 'No email' + if not person: + logger.error("The FAS username doesnot exsist!") + account_exists = 'No' + else: + account_exists = 'Yes' + privacy = person['privacy'] + if not(privacy): + logger.warn("The privacy is set to not view the Name!") + u_name = person['human_name'] + u_email = person['email'] + + context = {'account_exists':account_exists,'email': u_email, 'name': u_name, 'server_error': is_server_error, 'type_of_error': type_of_error} + except Exception as ex: + type_of_error = ex.__class__.__name__ + logger.error("%s Occured", type_of_error) + is_server_error = 'True' + context = {'account_exists':'Can\'t Say','email': 'Can\'t Say', 'name': 'Can\'t Say', 'server_error': is_server_error, 'type_of_error': type_of_error} + return JsonResponse(context) diff --git a/happinesspackets/settings/base.py b/happinesspackets/settings/base.py index 141eadd..dbb1b62 100644 --- a/happinesspackets/settings/base.py +++ b/happinesspackets/settings/base.py @@ -130,6 +130,10 @@ OIDC_RP_SIGN_ALGO = 'RS256' OIDC_RP_IDP_SIGN_KEY = '-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAq/0/XjILQxF3OaQZtFE3wVJ5UUuxZbxiJ/z+Zai0EOHiaMMxVyoo\nibDRen615r525DQ8TmQyR0eMQEpQ6SUvaOunahpYohgAkbkYggUMQhcoCLme18ZJ\nBTNWTP8w4t7mcuZd1cy1KtHpEvH4gkrjp8N3vIv1lzFraSc+p2rHMbV+AX5CJQ1H\nohBdwaqyOBKp0nzY27gu2EH2vzCwXkO4zGtrHfjjGc0Ra4WG+xz1AWg833xcFj3p\nqM3vca09jDLBme+GT151LcCCXRNyOZPZ3ZX62NxkMyqvVJHC3Uu2Q1hSHO7f6AZk\nZXY88PXXEH52T2ZrWiISowjTcGUboP8goQIDAQAB\n-----END RSA PUBLIC KEY-----\n' OIDC_RP_CLIENT_ID = os.environ.get('OIDC_RP_CLIENT_ID') OIDC_RP_CLIENT_SECRET = os.environ.get('OIDC_RP_CLIENT_SECRET') + +ADMIN_USERNAME = os.getenv('ADMIN_USERNAME') +ADMIN_PASSWORD = os.getenv('ADMIN_PASSWORD') + OIDC_OP_AUTHORIZATION_ENDPOINT = "https://iddev.fedorainfracloud.org/openidc/Authorization" OIDC_OP_TOKEN_ENDPOINT = "https://iddev.fedorainfracloud.org/openidc/Token" OIDC_OP_USER_ENDPOINT = "https://iddev.fedorainfracloud.org/openidc/UserInfo" diff --git a/happinesspackets/settings/dev.py b/happinesspackets/settings/dev.py index 8da1bd0..f4d957d 100644 --- a/happinesspackets/settings/dev.py +++ b/happinesspackets/settings/dev.py @@ -83,4 +83,8 @@ with open("client_secrets.json") as f: OIDC_RP_CLIENT_ID = secrets["client_id"] OIDC_RP_CLIENT_SECRET = secrets["client_secret"] - +# Reading the fas-id and Password +with open("fas-admin-details.json") as f: + secrets = json.load(f) + ADMIN_USERNAME = secrets["ADMIN_USERNAME"] + ADMIN_PASSWORD = secrets["ADMIN_PASSWORD"] diff --git a/requirements/base.txt b/requirements/base.txt index 218c91e..0c5143b 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -42,3 +42,5 @@ psycopg2==2.7.5 # PostgreSQL driver Whoosh==2.7.4 django-haystack==2.8.1 +#python-fedora for f-a-s API +python-fedora==0.10.0 diff --git a/templates/base.html b/templates/base.html index eb9a271..3fb1125 100644 --- a/templates/base.html +++ b/templates/base.html @@ -13,6 +13,8 @@ + +