How to save variables into mysql database in django - django

so l have pulled the data from a movie website and l saved it into variables for example title but now I'm struggling to send this data to MySQL DB
def index(request):
response = requests.get(
'https://fmovies.to/api/list_movies.json',
params={'limit':'20'},
)
json_response = response.json()
movies = json_response['data']['movies']
#title = movies[0]['title']
#movie_url = movies[0]['url']
#description = movies[0]['description_full']
#movie_torrent_link = movies[0]['torrents'][0]['url']
#cover_image = movies[0]['medium_cover_image']
for value in movies:
title = value['title']
movie_url = value['url']
description = value['description_full']
movie_torrent_link = value['torrents'][0]['url']
image = value['medium_cover_image']
rating = value['rating']
genre = value['genres']
runtime = value['runtime']
year_of_production = value['year']
slug = value['slug']
print(image)
print(rating)
print(runtime)
print(year_of_production)
print(slug)
return render(request, 'index.html',{'title':title})

managed to save direct without going the long way l was going
def index(request):
response = requests.get(
'https://fmovies.to/api/list_movies.json',
params={'limit':'20'},
)
json_response = response.json()
movies = json_response['data']['movies']
for value in movies:
save_to_db = Movie.objects.create(
title=value['title'],
description = value['description_full'],
image = value['medium_cover_image'],
category = value['genres'],
year_of_production = value['year'],
movie_url = value['url'],
movie_torrent_link = value['torrents'][0]['url'],
rating = value['rating'],
runtime = value['runtime'],
)
save_to_db.save()
return render(request, 'index.html',)

Related

How to do multiple request POST in django?

I have a view that sends a form to another view so that the user can review his form, and then after making another request post to save at that moment... so when the user enters the review view, he enters in request.POST and the form ends up being saved at the wrong time
def resumo(request, avaliado_id):
print(request.method == 'POST')
perfil = Perfil.objects.filter(user_id=avaliado_id)
queryDict = request.POST
notas = (list(queryDict.values()))
criterios = (list(queryDict.keys()))
valores = (list(queryDict.values()))
valores = [float(x.replace(",",".")) for x in valores[2:]]
pesos = (queryDict.getlist('pesos'))
pesos = [float(x.replace(",",".")) for x in pesos]
res_list = [valores[i] * pesos[i] for i in range(len(valores))]
media = sum(res_list)
lista = zip(criterios[2:], notas[2:])
print(list(lista))
query_criterio = Criterio.objects.filter(ativo = True).values_list('id', flat=True)
lista_criterio_id = list(query_criterio)
if request.method == 'POST':
avaliado = Perfil.objects.get(pk = avaliado_id)
avaliador = request.user.perfil
avaliacao = Avaliacao.objects.create(avaliador = avaliador, avaliado= avaliado, media = media)
avaliacao.save()
print(avaliacao.id)
for nota, criterio in zip(notas[2:], lista_criterio_id):
nota = Notas.objects.create(avaliacao = Avaliacao.objects.get(pk = avaliacao.id), notas = nota, criterios = Criterio.objects.get( pk = criterio))
nota.save()
context = {
'media' : media,
'chaves_e_valores' : zip(criterios[2:], notas[2:]),
'perfis' : perfil,
}
return render(request, 'admin/resumo.html', context)
in the first line "request.method == 'POST'" returns True and because of that it ends up hitting in conditional if.

how to pass data in array to post method in angular and django?

I am trying to pass a list of student IDs but it is not working.
from angular I am passing data as follows
let noticeData = this.announceForm.value;
if (noticeData.students.includes('all')){
noticeData.students = noticeData.students.filter((s) => s != 'all')
}
noticeData.students = JSON.stringify(noticeData.students);
rest call
var formData = new FormData();
for (var key in noticeData) {
formData.set(key, noticeData[key]);
}
this._rest.one("/classwork/notices/")
.post('', formData)
.toPromise()
.then((classworks) => {
}, (error) => {
console.log("ERROR SENDING ANNOUNCEMENT ", error);
this.alert = {
type : "error",
message: "Something went wrong, please try again."
}
});
}
from django i am reading and trying to store data as
Below is the to_internal_value method which I have overridden.
def to_internal_value(self, data):
tempdict = data.copy()
tempdict['students'] = json.loads(data['students'])
data = tempdict
return super(NoticesSerializer, self).to_internal_value(data)
views.py
class ClassworkView(APIView):
def get(self, request, format=None):
notices = ClassworksFilter(request.query_params, queryset=Notice.objects.all()).qs
materials = ClassworksFilter(request.query_params, queryset=Material.objects.all()).qs
assignments = ClassworksFilter(request.query_params, queryset=Assignment.objects.all()).qs
queryset = sorted(
chain(notices, materials, assignments),
key=lambda instance: instance.create_date
)
model_serializers = {
'notice': NoticesSerializer,
}
classworks = []
for instance in queryset:
serializer = model_serializers[instance._meta.model_name]
data = serializer(instance).data
data['classwork_type'] = instance._meta.model_name
classworks.append(data)
page_number = request.query_params.get('page', 1)
page_size = request.query_params.get('page_size', 20)
page = Paginator(classworks, per_page=page_size).get_page(page_number)
base_url = f'{request.scheme}://{request.get_host()}{reverse("classworks")}'
params = [f'{k}={v}' if k!='page' else None for k,v in request.query_params.items()]
indx = 0
for param in params:
if param==None:
params.pop(indx)
indx+=1
params = '&'.join(params)
next = f'{base_url}?page={page.next_page_number()}&{params}' if page.has_next() else None
prev = f'{base_url}?page={page.previous_page_number()}&{params}' if page.has_previous() else None
response = {
'count': len(classworks),
'next': next,
'previous': prev,
'results': page.object_list
}
return Response(response)
here is serializer.py
class NoticesSerializer(serializers.ModelSerializer):
class Meta:
model = Notice
fields = '__all__'
def to_representation(self, instance):
representation = super(NoticesSerializer, self).to_representation(instance)
try:
representation['classroom'] = ClassroomsSerializer(instance.classroom, context=self.context).data
students = StudentsSerializer(instance.students, many=True, context=self.context).data
for student in students:
student['classroom'] = student['classroom']['id']
representation['students'] = students
except Exception as error:
print("ERROR SETTING REPRESENTATION FOR NOTICE SERIALIZER", error)
return representation
here is the models.py
class Notice(Classwork):
file = models.FileField(upload_to='classworks/attachments', blank=True, null=True)
link = models.URLField(blank=True, null=True)
description = models.TextField()
class Meta(BaseModel.Meta):
db_table = 'ec_notice'
but it always returns the same error
{students: [“Incorrect type. Expected pk value, received list.”]}
here the data I am posting by form data

query that must exclude me from the whole list

hi i have a problem with this filter. group_to_add takes some values ​​which should filter out the problem that I don't want those values ​​but I want the others without those.
I would like to find a way to take those values ​​and subtract them from others.
group_to_add = DatiGruppi.objects.filter(gruppi_scheda = scheda.id)
GruppiForm.base_fields['dati_gruppo'] = forms.ModelChoiceField(queryset = group_to_add)
I asked a similar question I leave the link
select filtering and removal if they are already present in the db
models
class Schede(models.Model):
nome_scheda = models.CharField(max_length=100)
utente = models.ForeignKey(User, on_delete = models.CASCADE,related_name = 'utente')
class DatiGruppi(models.Model):
dati_gruppo = models.ForeignKey(Gruppi,on_delete = models.CASCADE, related_name = 'dati_gruppo')
gruppi_scheda = models.ForeignKey(Schede,on_delete = models.CASCADE, related_name = 'gruppi_scheda')
class Gruppi(models.Model):
nome_gruppo = models.CharField(max_length=100)
I have this tab where inside there are saved data groups that contain groups that are inside a select the correct exclusion would be
group_to_add = Gruppi.objects.exclude(dati_gruppo = 147)
but instead of 147 I have to put the id of the data group of that board
view
def creazione(request, nome):
scheda = get_object_or_404(Schede, nome_scheda = nome)
eserciziFormSet = formset_factory(EserciziForm, extra = 0)
if request.method == "POST":
gruppo_form = GruppiForm(request.POST, prefix = 'gruppo')
if gruppo_form.is_valid():
gruppo = gruppo_form.save(commit = False)
gruppo.gruppi_scheda = scheda
gruppoName = gruppo_form.cleaned_data['dati_gruppo']
gruppo.save()
esercizi_formset = eserciziFormSet(request.POST, prefix='esercizi')
for esercizi in esercizi_formset:
esercizi_instance = esercizi.save(commit = False)
esercizi_instance.gruppo_single = get_object_or_404(DatiGruppi, gruppi_scheda = scheda.id, dati_gruppo = gruppoName)
esercizi_instance.save()
return HttpResponseRedirect(request.path_info)
else:
group_to_add = Gruppi.objects.exclude(dati_gruppo = 147)
GruppiForm.base_fields['dati_gruppo'] = forms.ModelChoiceField(queryset = group_to_add)
gruppo_form = GruppiForm(prefix = 'gruppo')
esercizi_formset = eserciziFormSet(prefix='esercizi')
context = {'scheda' : scheda, 'gruppo_form' : gruppo_form, 'esercizi_formset': esercizi_formset}
return render(request, 'crea/passo2.html', context)
If I understand it correctly, you should use .exclude(…) [Django-doc] not .filter(…) [Django-doc]:
group_to_add = Gruppi.objects.exclude(
dati_gruppo__gruppi_scheda=scheda
)
GruppiForm.base_fields['dati_gruppo'] = forms.ModelChoiceField(queryset=group_to_add)

Django Razorpay integration working on test server but fails during live session

I am using razorpay integration with Django, I have tested the integration on localhost and it worked, also the checkout process and the payment collection works fine but when it comes to verifying the payment it seems to break on the live server.
The order amount and details sent to razorpay seem to be working fine but response capture is maybe breaking.
Below is my copy of views.py
class CheckOutW2(View):
def get(self, request, customer_phone):
dtt = dt.datetime.now()
phone = request.session.get('customer')
customer_object = Customer.objects.filter(phone=phone, date__hour= str(dtt.hour))
customer_object = customer_object.reverse()[0]
address = Addresses.objects.filter(customer=customer_object, date__hour= str(dtt.hour))
address = address.reverse()[0]
cart = request.session.get('cart')
products = Product.get_products_by_id(list(cart.keys()))
ot = 0
for p in products:
summed = p.price * cart.get(str(p.id))
ot = ot+summed
ot_rz = ot *100
context = {'userD':customer_object, 'address':address, 'ot':ot_rz }
return render(request, 'checkoutW2.html', context)
def post(self, request, customer_phone):
dtt = dt.datetime.now()
cart = request.session.get('cart')
products = Product.get_products_by_id(list(cart.keys()))
ot = 0
for p in products:
summed = p.price * cart.get(str(p.id))
ot = ot+summed
order_amount = ot
order_currency = 'INR'
order_reciept = ('{}_{}_{}_{}'.format(dtt.day,dtt.month,dtt.year,customer_phone))
# response = client.order.create(amount=order_amount, currency=order_currency, reciept=order_reciept )
# order_id = response['id']
order_status = 'created'
if order_status == 'created':
return render(request,'checkoutW2.html', {'rzp_oid':order_id})
#csrf_exempt
def payment_status(request ):
response = request.POST
phone = request.session.get('customer')
cart = request.session.get('cart')
products = Product.get_products_by_id(list(cart.keys()))
dtt = dt.datetime.now()
customer_object = Customer.objects.filter(phone=phone, date__hour= str(dtt.hour))
customer_object = customer_object.reverse()[0]
address = Addresses.objects.filter(customer=customer_object, date__hour= str(dtt.hour))
address = address.reverse()[0]
params_dict = {
'razorpay_payment_id': response['razorpay_payment_id'],
'razorpay_order_id': response['razorpay_order_id'],
'razorpay_signature': response['razorpay_signature']
}
try:
status = client.utility.verify_payment_signature(params_dict)
for p in products:
O = OrderItemss(customer=customer_object,
item=p,
quantity=cart.get(str(p.id)))
O.save()
OR = Orderss(customer=customer_object,address=address)
OR.save()
orders = OrderItemss.objects.filter(customer=customer_object, date__hour= str(dtt.hour))
for ordr in orders:
O = Orderss.objects.filter(customer=customer_object)
O = O[0]
O.items.add(ordr)
orders = OrderItemss.objects.filter(customer=customer_object, date__hour= str(dtt.hour))
request.session.flush()
request.session.modified = True
order = Orderss.objects.get(customer=customer_object, date__hour= str(dtt.hour))
context = {'userD':customer_object, 'address':address, 'order':orders, 'orders':order, 'status':'sucess'}
return render(request, 'order_summary.html', context)
except:
context = {'userD':customer_object, 'address':address, 'status':'failed'}
return render(request, 'order_summary.html', context)

Has anyone done a Twitter sentiment analysis using PySpark?

Has anyone done a Twitter sentiment analysis using Apache Spark?I have tried this
class Tweet(dict):
def __init__(self, tweet_in, encoding = 'utf-8'):
super(Tweet, self).__init__(self)
if tweet_in and 'delete' not in tweet_in:
self['id'] = tweet_in['id']
self['geo'] = tweet_in['geo']['coordinates'] if tweet_in['geo'] else None
self['text'] = tweet_in['text'].encode(encoding)
self['user_id'] = tweet_in['user']['id']
self['hashtags'] = [x['text'].encode(encoding) for x in tweet_in['entities']['hashtags']]
self['timestamp'] = dateutil.parser.parse(tweet_in[u'created_at']).replace(tzinfo=None).isoformat()
self['screen_name'] = tweet_in['user']['screen_name'].encode(encoding)
def connect_twitter():
consumer_key = "personal_info"
consumer_secret = "personal_info"
access_token = "personal_info"
access_secret = "personal_info"
auth = twitter.OAuth(token = access_token,
token_secret = access_secret,
consumer_key = consumer_key,
consumer_secret = consumer_secret)
return twitter.TwitterStream(auth=auth)
def get_next_tweet(twitter_stream, i ):
block = False # True
stream = twitter_stream.statuses.sample(block=False)
tweet_in = None
while not tweet_in or 'delete' in tweet_in:
tweet_in = stream.next()
tweet_parsed = Tweet(tweet_in)
return json.dumps(tweet_parsed)
def process_rdd_queue(twitter_stream, nb_tweets = 5):`enter code here`
rddQueue = []
for i in range(nb_tweets):
json_twt = get_next_tweet(twitter_stream, i )
dist_twt = ssc.sparkContext.parallelize([json_twt], 5)
rddQueue += [dist_twt]
lines = ssc.queueStream(rddQueue, oneAtATime=False)
lines.pprint()
enter code here
try : sc.stop()
except : pass
sc = SparkContext(appName="PythonStreamingQueueStream")
ssc = StreamingContext(sc, 1)
twitter_stream = connect_twitter()
process_rdd_queue(twitter_stream)
try : ssc.stop(stopSparkContext=True, stopGraceFully=True)
except : pass
ssc.start()
time.sleep(2)
ssc.stop(stopSparkContext=True, stopGraceFully=True)
Do I need to have a static file where I need positive and negative words for sentiment analysis mechanism.
I am a beginner at this.