Send data from model signal post_save to view - javascript

I am connected to an API then I save all data coming to my DB, I have already setup the signals for that model's post_save.
How can I display all new data to my feed without the users needing to reload the page ?
I created a scheduler using apscheduler that pulls out the data from the API every specific time.
updater.py
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from news_updater import newsAPI
def start():
print('sched start')
scheduler = BackgroundScheduler()
scheduler.add_job(newsAPI.get_fxstreet_news, 'interval', minutes=1)
scheduler.start()
apps.py
from django.apps import AppConfig
class NewsFilterConfig(AppConfig):
name = 'news_filter'
def ready(self):
from news_updater import updater
updater.start()
and I added this to my models.py
from django.db.models.signals import post_save, pre_save
def save_article(sender, instance, **kwargs):
print('new article saved')
post_save.connect(save_article, sender=NewsArticles)
it's already printing the 'new article saved' everytime a new data comes in.
I also have setup a channel and is connected but I don't know how I can piece everything together
consumers.py
import asyncio
import json
from channels.consumer import AsyncConsumer
from channels.db import database_sync_to_async
from .models import NewsArticles
class NewsArticlesConsumer(AsyncConsumer):
async def websocket_connect(self, event):
print('connected', event)
await self.send({
'type': 'websocket.accept'
})
latest_news_obj = await self.get_latest_news()
print(latest_news_obj)
await self.send({
'type': 'websocket.send',
'text': "hello world"
})
async def websocket_receive(self, event):
print('received', event)
async def websocket_disconnect(self, event):
print('disconnected', event)
#database_sync_to_async
def get_latest_news(self):
return NewsArticles.objects.filter(is_new=True)[0]

Related

How to add default value for Date Picker in Django Model Form

Currently I have a date picker in a django model form that works perfectly fine. However, I want to add an initial (default) value to it. Here is my current code :
class DatePickerInput(forms.DateInput):
input_type = 'date'
class PDFClassificationForm(forms.ModelForm):
class Meta:
model = Documents
fields = [
'id_documenttype',
'dateenvoi',,
'comment']
widgets = {
'dateenvoi' : DatePickerInput(),
}
I tried adding initial as a parameter to DatePickerInput() like so :
widgets = {
'dateenvoi' : DatePickerInput(initial= datetime.now().strftime('%d/%m/%y %H:%M:%S')),
}
However it was unsuccessful, any suggestions ?
Try this:
import datetime
class PDFClassificationForm(forms.ModelForm):
class Meta:
model = Documents
fields = [
'id_documenttype',
'dateenvoi',
'comment',
]
widgets = {
'dateenvoi' : DatePickerInput(widget=forms.TextInput(attrs={'value': datetime.now().strftime('%d/%m/%y %H:%M:%S')})),
}
try adding your desired initial value to the form in the view (views.py)
import datetime
from django.views.generic import TemplateView
from .forms import PDFClassificationForm
class YourView(TemplateView):
template_name = 'your_directory/your_template_name.html'
form_class = PDFClassificationForm
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
return render(request, self.template_name, {'form': form})
def get_context_data(self, *args, **kwargs):
context = super(YourView, self).get_context_data(*args, **kwargs)
request = self.request
context["form"] = PDFClassificationForm(request.POST or None, initial={ "dateenvoi": datetime.today)
return context

Can't get Plaid Link to Open using Javascript and python

When the plaid link handler is called it spins and then goes away. I can't get the link dialog box to show completely to get access token. Setup using flask for the python server on port 5000 and using the plaid api for calls in python. Using javascript on the client side.
I can see the requests going through but nothing happens or seems to post to the client:
127.0.0.1 - - [05/Apr/2022 21:53:31] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [05/Apr/2022 21:54:10] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [05/Apr/2022 21:54:10] "POST /create_link_token HTTP/1.1" 200 -
Javascript:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button id="link-button">Link Account</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.plaid.com/link/v2/stable/link-initialize.js"></script>
<script type="text/javascript">
(async function($) {
var handler = Plaid.create({
// Create a new link_token to initialize Link
token: (await $.post('/create_link_token')).link_token,
receivedRedirectUri: window.location.href,
onLoad: function() {
// Optional, called when Link loads
},
onSuccess: function(public_token, metadata) {
// Send the public_token to your app server.
// The metadata object contains info about the institution the
// user selected and the account ID or IDs, if the
// Account Select view is enabled.
$.post('/exchange_public_token', {
public_token: public_token,
});
},
onExit: function(err, metadata) {
// The user exited the Link flow.
if (err != null) {
// The user encountered a Plaid API error prior to exiting.
}
// metadata contains information about the institution
// that the user selected and the most recent API request IDs.
// Storing this information can be helpful for support.
},
onEvent: function(eventName, metadata) {
// Optionally capture Link flow events, streamed through
// this callback as your users connect an Item to Plaid.
// For example:
// eventName = "TRANSITION_VIEW"
// metadata = {
// link_session_id: "123-abc",
// mfa_type: "questions",
// timestamp: "2017-09-14T14:42:19.350Z",
// view_name: "MFA",
// }
}
});
$('#link-button').on('click', function(e) {
handler.open();
});
})(jQuery);
</script>
</body>
</html>
Python server.py:
# source /Users/tnappy/node_projects/quickstart/python/bin/activate
# Read env vars from .env file
from plaid.exceptions import ApiException
from plaid.model.payment_amount import PaymentAmount
from plaid.model.payment_amount_currency import PaymentAmountCurrency
from plaid.model.products import Products
from plaid.model.country_code import CountryCode
from plaid.model.recipient_bacs_nullable import RecipientBACSNullable
from plaid.model.payment_initiation_address import PaymentInitiationAddress
from plaid.model.payment_initiation_recipient_create_request import PaymentInitiationRecipientCreateRequest
from plaid.model.payment_initiation_payment_create_request import PaymentInitiationPaymentCreateRequest
from plaid.model.payment_initiation_payment_get_request import PaymentInitiationPaymentGetRequest
from plaid.model.link_token_create_request_payment_initiation import LinkTokenCreateRequestPaymentInitiation
from plaid.model.item_public_token_exchange_request import ItemPublicTokenExchangeRequest
from plaid.model.link_token_create_request import LinkTokenCreateRequest
from plaid.model.link_token_create_request_user import LinkTokenCreateRequestUser
from plaid.model.asset_report_create_request import AssetReportCreateRequest
from plaid.model.asset_report_create_request_options import AssetReportCreateRequestOptions
from plaid.model.asset_report_user import AssetReportUser
from plaid.model.asset_report_get_request import AssetReportGetRequest
from plaid.model.asset_report_pdf_get_request import AssetReportPDFGetRequest
from plaid.model.auth_get_request import AuthGetRequest
from plaid.model.transactions_get_request import TransactionsGetRequest
from plaid.model.transactions_get_request_options import TransactionsGetRequestOptions
from plaid.model.identity_get_request import IdentityGetRequest
from plaid.model.investments_transactions_get_request_options import InvestmentsTransactionsGetRequestOptions
from plaid.model.investments_transactions_get_request import InvestmentsTransactionsGetRequest
from plaid.model.accounts_balance_get_request import AccountsBalanceGetRequest
from plaid.model.accounts_get_request import AccountsGetRequest
from plaid.model.investments_holdings_get_request import InvestmentsHoldingsGetRequest
from plaid.model.item_get_request import ItemGetRequest
from plaid.model.institutions_get_by_id_request import InstitutionsGetByIdRequest
from plaid.model.transfer_authorization_create_request import TransferAuthorizationCreateRequest
from plaid.model.transfer_create_request import TransferCreateRequest
from plaid.model.transfer_get_request import TransferGetRequest
from plaid.model.transfer_network import TransferNetwork
from plaid.model.transfer_type import TransferType
from plaid.model.transfer_user_in_request import TransferUserInRequest
from plaid.model.ach_class import ACHClass
from plaid.model.transfer_create_idempotency_key import TransferCreateIdempotencyKey
from plaid.model.transfer_user_address_in_request import TransferUserAddressInRequest
from plaid.api import plaid_api
from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
from datetime import datetime
from datetime import timedelta
import plaid
import base64
import os
import datetime
import json
import time
from dotenv import load_dotenv
from werkzeug.wrappers import response
import pyodbc
load_dotenv()
app = Flask(__name__)
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=DESKTOP-DJCMSVC\MJBOURQUIN_SQL;"
"Database=MyBudget;"
"Trusted_Connection=yes;")
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Accounts')
for row in cursor:
print('row = %r' % (row,))
vals = ("SOMETHING")
data = "UPDATE Accounts Set API_ID = 'SOMETHING' WHERE Id = 1"
cursor.execute(data)
cursor.commit();
access = "THAT THING"
cursor = cnxn.execute('UPDATE Accounts Set API_ID = ? WHERE Id = ?', [access,1])
cursor.commit();
# Fill in your Plaid API keys - https://dashboard.plaid.com/account/keys
PLAID_CLIENT_ID = os.getenv('PLAID_CLIENT_ID')
PLAID_SECRET = os.getenv('PLAID_SECRET')
# Use 'sandbox' to test with Plaid's Sandbox environment (username: user_good,
# password: pass_good)
# Use `development` to test with live users and credentials and `production`
# to go live
PLAID_ENV = os.getenv('PLAID_ENV', 'sandbox')
# PLAID_PRODUCTS is a comma-separated list of products to use when initializing
# Link. Note that this list must contain 'assets' in order for the app to be
# able to create and retrieve asset reports.
PLAID_PRODUCTS = os.getenv('PLAID_PRODUCTS', 'transactions').split(',')
# PLAID_COUNTRY_CODES is a comma-separated list of countries for which users
# will be able to select institutions from.
PLAID_COUNTRY_CODES = os.getenv('PLAID_COUNTRY_CODES', 'US').split(',')
products = []
for product in PLAID_PRODUCTS:
products.append(Products(product))
def empty_to_none(field):
value = os.getenv(field)
if value is None or len(value) == 0:
return None
return value
host = plaid.Environment.Sandbox
if PLAID_ENV == 'sandbox':
host = plaid.Environment.Sandbox
if PLAID_ENV == 'development':
host = plaid.Environment.Development
if PLAID_ENV == 'production':
host = plaid.Environment.Production
# Parameters used for the OAuth redirect Link flow.
#
# Set PLAID_REDIRECT_URI to 'http://localhost:3000/'
# The OAuth redirect flow requires an endpoint on the developer's website
# that the bank website should redirect to. You will need to configure
# this redirect URI for your client ID through the Plaid developer dashboard
# at https://dashboard.plaid.com/team/api.
PLAID_REDIRECT_URI = empty_to_none('PLAID_REDIRECT_URI')
configuration = plaid.Configuration(
host=host,
api_key={
'clientId': PLAID_CLIENT_ID,
'secret': PLAID_SECRET,
'plaidVersion': '2020-09-14'
}
)
api_client = plaid.ApiClient(configuration)
client = plaid_api.PlaidApi(api_client)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/my-link/')
def my_link():
print ('I got clicked!')
return 'Click.'
#app.route("/create_link_token", methods=['POST'])
def create_link_token():
# Get the client_user_id by searching for the current user
request = LinkTokenCreateRequest(
products=products,
client_name="Budgetary",
country_codes=list(map(lambda x: CountryCode(x), PLAID_COUNTRY_CODES)),
language='en',
user=LinkTokenCreateRequestUser(
client_user_id=str(time.time())
)
)
if PLAID_REDIRECT_URI!=None:
request['redirect_uri']=PLAID_REDIRECT_URI
# create link token
response = client.link_token_create(request)
return jsonify(response.to_dict())
#app.route('/exchange_public_token', methods=['POST'])
def exchange_public_token():
global access_token
public_token = request.form['public_token']
request = ItemPublicTokenExchangeRequest(
public_token=public_token
)
response = client.item_public_token_exchange(request)
access_token = response['access_token']
item_id = response['item_id']
cursor = cnxn.execute('UPDATE Accounts Set API_ID = ? WHERE Id = ?', [access_token,1])
cursor.commit();
return jsonify(response.to_dict())
if __name__ == '__main__':
app.run(debug=True)
Solved by breaking the code out into pieces. Clicking the button, then requesting and printing value in python. Then sending and making sure it's parsed correctly. Then with the parsed token value opening up link successfully.

How can I use main.dart variable in my JS file?

I'm trying to create a calling app using flutter and I've created the backend using a node.js. This is how my main.dart file in flutter looks like:
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_dialpad/flutter_dialpad.dart';
import 'dart:js';
import 'package:js/js.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: SafeArea(
child:
DialPad(
enableDtmf: true,
outputMask: "(000) 000-0000",
backspaceButtonIconColor: Colors.red,
makeCall: (number){
print(number);
}
)
),
),
);
}
}
I want to use this "number" variable in my app.js file which looks like this:
const accountSid = '***';
const authToken = '***';
const client = require('twilio')(accountSid, authToken);
client.calls.create({
url: 'http://demo.twilio.com/docs/voice.xml',
to: '+10000000',
from: '+1000000',
}, function(err, call){
if (err) {
console.log(err);
} else {
console.log(call.sid);
}
})
I want to be able to use the "number" variable from my main.dart file in the "to" field in my app.js file. Please help me out...
What you need is a way to pass data between applications, and the easiest way for that would be through a REST API
You can use the HTTP module in NodeJS or a third-party package like Express and set up a POST Route to your NodeJS Server, where the number is sent as data.
Once the data is received on your server, you can call your Twilio function, and send a response back.
On Flutter, you can use the http package to make the API call.

Django form is sent but doesent save to database

I´m doing a simple user-business application, where a user has one or many business. The problem is that my create business forms is not saving its data to the database. The user has all the permissions and is active, and I can save data from the create user form with no problem. What is wrong?
View.py:
class crear_negocio(LoginRequiredMixin, FormView):
template_name = "tienda/crear_negocio.html"
form_class= Negocio_Form
success_url = reverse_lazy('tienda_app:crear_negocio')
login_url = reverse_lazy('register_app:logIn')
form.py:
class Negocio_Form(forms.ModelForm):
class Meta:
model = Negocio_Model
fields = ("Nombre_Negocio","Administrador","Descipcion_Negocio",'Correo_Negocio','Telefono_Negocio','Direccion_Negocio')
Model.py:
class Negocio_Model(models.Model):
Nombre_Negocio = models.CharField(max_length=25)
Administrador = models.ForeignKey(Usuario_Model, on_delete=models.CASCADE)
Descipcion_Negocio = models.TextField(null=True, blank=True)
Correo_Negocio = models.EmailField()
Telefono_Negocio = models.CharField(max_length=13)
Direccion_Negocio = models.CharField(max_length=25)
def __str__(self):
return self.Nombre_Negocio+' '+self.Correo_Negocio+' '+self.Telefono_Negocio+' '+self.Direccion_Negocio
Database config:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bdtg1',
'USER':'juan',
'PASSWORD':'juanjo123',
'HOST':'127.0.0.1',
'PORT':'3306'
}
}
A FormView does not .save() the form, thus it will indeed not create a record at the database. By default in case the form is successful, it redirects to the success URL, that's all. A typical use case of a FormView is for example to send an email instead of saving it to the database.
You can override the form_valid(…) method [Django-doc] to save the form, but it might be better to make use of a CreateView [Django-doc]:
from django.views.generic.edit import CreateView
class crear_negocio(LoginRequiredMixin, CreateView):
template_name = 'tienda/crear_negocio.html'
form_class= Negocio_Form
success_url = reverse_lazy('tienda_app:crear_negocio')
login_url = reverse_lazy('register_app:logIn')

How to send error responses back to client in django rest framework?

I am newbie to Django rest framework and trying to send custom error messages back to client, where I am using VueJS.
I followed the answer given here but I am unable to receive error message on client side.
client-side
auth.phoneVerification(this.fullName, this.mobileNo, this.email)
.then((response)=>{
console.log(response.data)
})
.catch((error)=>{
console.log("Error is ", error)
})
server-side
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = InterestedUser
fields = ('full_name', 'phone_no', 'email')
def __init__(self, *args, **kwargs):
super(UserSerializer, self).__init__(*args, **kwargs)
self.fields['full_name'].error_messages['required'] = 'Please provide your full name.'
self.fields['phone_no'].error_messages['required'] = 'Please provide your mobile number.'
self.fields['email'].error_messages['required'] = 'Please provide your email id.'
views.py
class UserView(GenericAPIView):
serializer_class = UserSerializer
def post(self, request):
serializer = self.get_serializer(data=request.data)
print("requested data is ", request.data)
if not serializer.is_valid():
raise serializers.ValidationError(serializer.errors)
print("serialiser errors are ",serializer.errors)
In console I am unable to receive the message as provided by serializer.errors
You can return any Django response with proper data, ex:
....
return JsonResponse({'errors': serializer.errors}, status=500)

Categories

Resources