How to integrate python script in a django project - python

So I am extremely new to programming, and am stuck at this issue, I am using python with Django and Mongodb for database. I need to write a service that assigns an ID (not the one assigned by mongodb) upon each user form submission. for example entry 1's ID will be [Prefix entered by user] 2101, entry 2's ID will be [Prefix entered by user] 2102, so its basically adding in the number 2100.
I have no idea how and where to integrate this logic in my code. I have tried a few solutions on the internet but nothing seems to work.
my code:
Model.py
class Writeups(Document):
blog_id = 2100
title = fields.StringField(max_length=120)
date_created = fields.DateField(blank=True, null=True)
date_modified = fields.DateField(blank=True, null=True)
version_number = fields.DecimalField(null= True , max_digits=1000, decimal_places=2)
storage_path = fields.StringField(max_length=120)
STRIKE_READY_BRIEF = 'SRB'
STRIKE_READY_THREAT_REPORT = 'SRTR'
PREFIX_CHOICES = [
(STRIKE_READY_BRIEF, 'SRB'),
(STRIKE_READY_THREAT_REPORT, 'SRTR'),
]
prefix = fields.StringField(
max_length=4,
choices=PREFIX_CHOICES,
null=False,
blank=False,
)
views.py:
#csrf_exempt
def writeups_request(request):
"""
Writeup Request
"""
if request.method == 'GET':
try:
data = {
'form-TOTAL_FORMS': '1',
'form-INITIAL_FORMS': '0',
}
writeups = WriteupsFormset(data)
# print(writeup)
return render(request, "writeups/writeups.html", {'writeups_forms': writeups})
except Exception as e:
print(e)
response = {"error": "Error occurred"}
return JsonResponse(response, safe=False)
if request.method == 'POST':
writeup_data = WriteupsFormset(request.POST)
if writeup_data.is_valid():
flag = False
logs = []
for writeups_data in writeup_data:
print(writeups_data)
if writeups_data.cleaned_data.get('DELETE'): # and malware_data._should_delete_form(form):
continue
title = writeups_data.cleaned_data.get('title')
date_created = writeups_data.cleaned_data.get('date_created')
date_modified = writeups_data.cleaned_data.get('date_modified')
version_number = writeups_data.cleaned_data.get('version_number')
storage_path = writeups_data.cleaned_data.get('storage_path')
prefix = writeups_data.cleaned_data.get('prefix')
try:
writeups = Writeups(),
title=title,
date_created=date_created,
date_modified=date_modified,
version_number=version_number,
storage_path=storage_path,
prefix=prefix)
writeups.save()

In order to implement your custom script at all submissions, Django's Signals are the solution for your use case. Look into post_save and pre-save signals and use them as per your problem.
https://docs.djangoproject.com/en/3.2/ref/signals/
If you have an existing database and you want a script to iterate through it and update the dataset, you can take a look at Management Commands
https://docs.djangoproject.com/en/4.0/howto/custom-management-commands/

Related

Django giving error when save JSON data in models

I tried this code to save my JSON data to my model that is Mvouchar. But getting this error. I easily get data through cmd but I tried to save this in my model then I get the error, why this happens, I think am doing some minor mistake but can't catch please help if u get my problem
#views.py
#csrf_exempt
def jsdata(request):
table_data = json.loads(request.POST.get('MyData'))
print(table_data)
for data in table_data:
b_no = request.POST['billno']
b_details = request.POST['billdetails']
at = request.POST['amount2']
record = Mvouchar(bill_no = data.b_no, bill_details = data.b_details,am=data.at)
record.save()
return render(request, 'cheque/mvouchar.html', {'msg': 'Data Saved.'})
#models.py
class Mvouchar(models.Model):
related = models.ForeignKey(Signs, on_delete=models.CASCADE, null=True, blank=True)
bill_no = models.CharField(max_length=80, null=True, blank=True)
bill_details = models.CharField(max_length=1000, null=True, blank=True)
am = models.CharField(max_length=30, null=True, blank=True)
vouchar_no = models.CharField(max_length=1000, null=True, blank=True)
#urls.py
url(r'jsondata/$', views.jsdata, name='jsondata'),
#script
<script>
$("#btnjson").click(function () {
var array1 = [];
$("tbody tr").each(function () {
var firstTableData = {};
firstTableData.BillNo = $(this).find('td').eq(0).text();
firstTableData.BillDetails = $(this).find('td').eq(1).text();
firstTableData.Amount = $(this).find('td').eq(2).text();
array1.push(firstTableData);
//}
});
alert(JSON.stringify(array1));
$.ajax({
type: "POST",
url: "/jsondata/",
dataType: 'json',
data: {MyData: JSON.stringify(array1)},
success: function(msg){
alert(msg);
}
});
return false;
} );
});
</script>
from django.http import JsonResponse
def jsdata(request):
table_data = json.loads(request.POST.get('MyData'))
# print(table_data)
r_data = {
'success': True,
}
for data in table_data:
# Since you are just creating objects you don't need to save created object in a variable.
try:
Mvouchar.objects.create(bill_no = data['BillNo'], bill_details=data['BillDetails'],at=data['Amount'])
except:
r_data['success'] = False
# IMO Views responding to ajax requests should send JsonResponse
if r_data['success']:
r_data['msg'] = 'Data Saved'
else:
r_data['msg'] = 'Not all Data Saved'
return JsonResponse(r_data)
This error probably occurs when the key not present in request data. By changing
b_no = request.POST['billno']
to
b_no = request.POST.get('BillNo').
solve throwing this exception. Even if data not present it returns None. Or can handle it by add try.except block.
try:
b_no = request.POST['billno']
except KeyError:
pass
And in your code, you call every dict key in lower case form and amount key as amount2. Change that to Amount
If your are storing data in table_data (table_data = request.POST.get('MyData')) then use table_data to store it in models.
Your data is in dictionary form so need to call values by its keys.
like -
#csrf_exempt
def jsdata(request):
table_data = json.loads(request.POST.get('MyData'))
print(table_data)
for data in table_data:
b_no = data.get('BillNo')
b_details = data.get('BillDetails')
at = data.get('Amount')
record = Mvouchar(bill_no = b_no , bill_details = b_details ,am=at )
record.save()
return render(request, 'cheque/mvouchar.html', {'msg': 'Data Saved.'})
Try this and tell me if any error occurs.
A KeyError is usually thrown when you look up a value for a key in a dictionary that does not exist. You could either provide a default value or check the existence of the key before you do the lookup.
request.POST.get('billno', 'default_value')
For more information about querying a dictionary with default values, see this helpful StackOverflow answer
The line above as such will not work, since there are other issues in this code besides the key not existing with only lowercase letters (see below).
Looking at the code I expect the key to not exist either because it has not been sent or because it may contain uppercase letters.
As it has been pointed out in the comments to your question, not only are you querying for billno (only lowercase letters) while the key you send has uppercase letters (BillNo), but you also embed it into another dictionary MyData - you need to change your query from request.POST['billno'] to data['BillNo'] (the same goes for all the other values you are trying to extract).
The correct query would thus look like this:
for data in table_data:
b_no = data['BillNo']
b_details = data['BillDetails']
at = data['Amount']
record = Mvouchar(bill_no = b_no, bill_details = b_details,am=at)
record.save()

Python37 and Django 2 TextField not a string?

We decided to migrate our django 1.10.5 project with python 2.7.15 to a newer version of python and django. Now we are using python 3.7 and django 2. After some problems with our email sending script I found something strange. When sending automated mails we took text from a TextField in the database and inserted it into our Mail.
content = content.replace('##ENTRY##', entry.text)
The text field of entry is a django models.TextField.
Now with python 37 it doesn't allow me to use it like that. I have to wrap the entry.text into a str() cast. But shouldn't the TextField be a string?
content = content.replace('##ENTRY##', str(entry.text))
With that it works, but it lets my tummy hurt when doing it like that, since i don't understand why.
Edit:
def send_task_entry_new(entry):
content = Email.load_email_template('tasks.watcher.info.entry.new')
content = content.replace('##TAG##', entry.task.get_tag())
content = content.replace('##ENTRY##', entry.text)
content = content.replace('##SUBJECT##', entry.task.get_subject())
subject = entry.task.get_tag() + " " + entry.task.get_subject()
for watcher in entry.task.taskwatcher_set.all():
if watcher.user.last_login:
content = content.replace('##LINK##', settings.BASE_URL + entry.task.get_absolute_url())
# If User has never logged in (Dummy for external task user)
else:
content = content.replace('##LINK##', settings.BASE_URL + reverse('tasks_public', args=[watcher.token]))
Email.send(watcher.user.email, '', '', subject, content)
Entry is an TaskEntry object which gets created as soon as I create a Task. The TaskEntry model looks like this. At least the attributes of a TaskEntry:
task = models.ForeignKey(Task, null=True, on_delete=models.SET_NULL)
creator = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
external = models.EmailField(null=True, blank=True)
text = models.TextField(verbose_name="Text")
date_created = models.DateTimeField(auto_now_add=True)
Edit Flow:
Email comes in and get processed:
text = get_decoded_email_body(response_part[1])
which fires:
def get_decoded_email_body(message_body):
msg = email.message_from_bytes(message_body)
if msg.is_multipart():
# Walk through all parts of this email
for part in msg.walk():
# print "%s => %s" % (part.get_content_type(), part.get_content_charset())
charset = part.get_content_charset()
#TODO umstellung auf Python3 enfernen und testen
if part.get_payload(decode=True):
if part.get_content_type() == 'text/plain':
if charset:
return str(part.get_payload(decode=True), charset, 'ignore')
else:
return to_unicode(part.get_payload(decode=True))
if part.get_content_type() == 'text/html':
if charset:
return str(part.get_payload(decode=True), charset, 'ignore')
else:
return to_unicode(part.get_payload(decode=True))
return "Email has no text/plain or text/html part"
else:
return to_unicode(msg.get_payload(decode=True))
After that we got our text extracted and create a now task:
user = User.objects.get_or_create(email=sender)[0]
task = Task.objects.create(subject=subject, creator=user)
task.create_entry(text, user) #Here it crashes
Email.send_task_operators_new_task(task, text, sender)
Now into the models.py and we go into create_entry:
def create_entry(self, text, creator):
entry = TaskEntry()
entry.text = text
entry.task = self
entry.creator = creator
entry.save()
if creator:
TaskWatcher.objects.get_or_create(task=self, user=creator)
Next step is the .save() method:
def save(self, *args, **kwargs):
if self.pk == None:
created = True
else:
created = False
super(TaskEntry, self).save(*args, **kwargs)
if created:
# If this entry is the first one inform creator and owner if given
if self.task.taskentry_set.all().count() == 1:
Email.send_task_created_to_creator(self.task)
# If a owner has been given how is not the creator
if self.task.owner and self.task.owner != self.task.creator:
Email.send_task_created_to_owner(self.task)
# This is not the first entry, so inform all watcher about the new entry
if self.task.taskentry_set.all().count() > 1:
Email.send_task_entry_new(self)
Now it executes the Email.send_task_entry_new(self) and throws the replace() error.
Thanks to Daniel Roseman I found the issue. Since I take the payload from the mail as bytes and never convert it into a string, it is saved as bytes into the db. As soon as I request the data from the db it is bytes and I have to convert them into string as I need it.

How do I assign specific users to a user-uploaded file so they can modify it/delete it (Django + Apache)

Im using django 1.10 + Apache in Linux.
I've created a small webapp to upload documents (with dropzone.js) and want to implement the ability for a user to specify who can view/modify/delete a specific file but i can't figure out a way how. I attempted using a ManyToManyField but maybe im not understading the Field itself correctly.
The "Document" model is this:
Model
class Document(models.Model):
file = models.FileField(upload_to = 'files/')
#validators=[validate_file_type])
uploaded_at = models.DateTimeField(auto_now_add = True)
extension = models.CharField(max_length = 30, blank = True)
thumbnail = models.ImageField(blank = True, null = True)
is_public = models.BooleanField(default = False)
accesible_by = models.ManyToManyField(User) #This is my attempt at doing this task.
def clean(self):
self.extension = self.file.name.split('/')[-1].split('.')[-1]
if self.extension == 'xlsx' or self.extension == 'xls':
self.thumbnail = 'xlsx.png'
elif self.extension == 'pptx' or self.extension == 'ppt':
self.thumbnail = 'pptx.png'
elif self.extension == 'docx' or self.extension == 'doc':
self.thumbnail = 'docx.png'
def delete(self, *args, **kwargs):
#delete file from /media/files
self.file.delete(save = False)
#call parent delete method.
super().delete(*args, **kwargs)
#Redirect to file list page.
def get_absolute_url(self):
return reverse('dashby-files:files')
def __str__(self):
return self.file.name.split('/')[-1]
class Meta():
ordering = ['-uploaded_at']
My View to handle the creation of documents:
View
class DocumentCreate(CreateView):
model = Document
fields = ['file', 'is_public']
def form_valid(self, form):
self.object = form.save(commit = False)
## I guess here i would Add the (self.request.user) to the accesible_by Field.
self.object.save()
data = {'status': 'success'}
response = JSONResponse(data, mimetype =
response_mimetype(self.request))
return response
Thanks in advance to anyone for any ideas or suggestions...
You have a model and a view that hopefully works for adding new documents, you still have a number of steps to go.
You'll need a place to assign users that can view/modify/delete your files. If you need to store access levels (view/delete...), your accessible_by will not suffice and you'll do well with a through table to add more information like access level.
You need to write views for various actions like view, delete... that users will request and here you ensure users have the right privileges. An implementation would be to get the request.user and the document id, look up if the user has the permission for what she's doing, return an http unauthorized exception or allow the action to proceed.
Edit: My question is about how can I assign user-permissions to each
individual file
If we're keeping this to access control from the django level, using the document model you already have, and you've taken some steps and for every document, you can assign users (accessible_by). Something like this can get you started:
from django.core.exceptions import PermissionDenied
def view_document(request, doc_pk):
doc = get_object_or_404(Document, pk=doc_pk)
if not doc.accessible_by.filter(username=request.user.username):
raise PermissionDenied
#perform rest of action
Or do you mean to use the permissions framework itself?

In Django 1.6.x how to get data of associated models in views?

After so much googling I did'nt found any relevant solution for getting the associated models data in views.
Things is I am requesting to the server via Ajax and want to fetch the folders and folder creator name(from User model). But it did'nt return me the first_name of the creator. Here's the code
models.py
class UserFolder(models.Model):
creator = models.ForeignKey(User)
name = models.CharField(blank=False, max_length=150)
is_shared = models.BooleanField(default=False)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
views.py
def pagination(obj, request):
max = request.GET.get('max') if 'max' in request.GET else 1
paginator = Paginator(obj, max) # Show 25 contacts per page
page = request.GET.get('page')
try:
return paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
return paginator.page(1)
except EmptyPage:
return ""
def folder_list(request): #called via ajax
folders = UserFolder.objects.filter(creator=request.user.id).order_by('-modified_date')
folders = pagination(folders, request) #for pagination
folders = json.dumps({}) if not folders else serializers.serialize("json", folders)
return HttpResponse(folders)
I have tried .select_related('creator') too but it did'nt work.
In js I am fetching the data like this :
$.each(data, function(i, v) {
var t = fldrTpl, id = v.pk;
v = v.fields
t = t.replace(/\{(ID)\}/g, id);
t = t.replace(/\{(NAME)\}/g, v.name);
t = t.replace(/\{(C_SIZE)\}/g, (v.current_size == null?0:v.current_size));
t = t.replace(/\{(C_ID)\}/g, v.creator.first_name);
t = t.replace(/\{(C_DATE)\}/g, v.created_date);
$(".my-folder-table").append(t);
});
v.creator.first_name always return undefined.
Any help would be really appreciated.
Thanks
You can always create a dictionary of the data you want to use on your client side:
def folder_list(request): #called via ajax
folders_obj = UserFolder.objects.filter(creator=request.user).select_related('creator').order_by('-modified_date').all()
folders_dict = []
for folder in folders_obj:
d = {}
for k, v in folder.__dict__.items():
d[k] = str(v)
d["creator__first_name"] = folder.creator.first_name
folders_dict.append(d)
folders = pagination(folders_dict, request) #for pagination
folders = json.dumps({}) if not folders else serializers.serialize("json", folders)
return HttpResponse(folders)
And in your js:
t = t.replace(/\{(C_ID)\}/g, v.creator__first_name);

Using tastypie resource in view

my first question here :
So I'm using tastypie to have api's for my app.
I want to be able to use tastypie to render json and then include that in a django view so that I can bootstrap my app's data.
There is an example of this in django tastypie cookbook here : http://django-tastypie.readthedocs.org/en/latest/cookbook.html#using-your-resource-in-regular-views
The problem is that I CANNOT get this to work, I've tried variants from simpler to more complex and I just cant get it, here some code for my models :
class ChatMessage(models.Model):
content = models.TextField()
added = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(ChatUser, related_name="messages")
chat_session = models.ForeignKey(ChatSession, related_name="messages")
answer_to = models.ForeignKey('self', blank=True, null=True)
flagged = models.BooleanField(blank=True,default=False)
mododeleted = models.BooleanField(blank=True,default=False)
mododeleted_by = models.ForeignKey(ChatUser,blank=True,null=True,default=None)
mododeleted_at = models.DateTimeField(blank=True,null=True,default=None)
[...]
class ChatSession (models.Model):
title = models.CharField(max_length=200)
link_title = models.CharField(max_length=200)
description = tinymce_models.HTMLField()
date = models.DateTimeField()
online = models.BooleanField(default=False)
next_session = models.BooleanField(default=False)
meps = models.ManyToManyField(ChatMep)
uid_newsupdate = models.CharField(max_length=200,blank=True,null=True,default="")
[...]
and my resources :
class ChatMessageResource(MyModelResource):
chat_session = fields.ForeignKey(ChatSessionResource, 'chat_session')
def renderOne(self,request,pkval):
data = self.obj_get(None,pk=pkval)
dbundle = self.build_bundle(obj=data,request=request)
return self.serialize(None,self.full_dehydrate(dbundle),'application/json')
def dehydrate(self, bundle):
bundle.data['likes'] = bundle.obj.get_likes()
bundle.data['likes_count'] = len(bundle.data['likes'])
return bundle
class Meta:
authentication = Authentication()
authorization = Authorization()
queryset = ChatMessage.objects.all()
resource_name = 'message'
fields = ('content', 'added', 'flagged', 'mododeleted','author','answer_to','chat_session')
filtering = {
'chat_session': ALL_WITH_RELATIONS,
}
and my view index :
def index(request):
cur_sess = get_current_chat_session()
data1= ChatMessageResource().renderOne(request,723)
return render_to_response('test.html',
{
'all_data' : data1
},
context_instance=RequestContext(request))
What I want is my renderOne() function to give me the json of ONE ChatMessageResource
And also I'd like a renderAll() function to gice me ALL (or filtered) ChatMessageResources in json.
And I want to use tastypie internals, I KNOW i could serialize it by myself but that's not the point..
Right now the error is :
NoReverseMatch at /live/
Reverse for 'api_dispatch_detail' with arguments '()' and keyword arguments '{'pk': 14L, 'resource_name': 'session'}' not found.
I'm just getting crazy, I've been trying for hours.
So please, how to get ONE/ALL resource as JSON by code using tastypie in a django view !
If It's not clear or I need to clarify, please just ask, thanks
Really what I want to do is to be able to get the JSON returned by an API url I created, but from code, not by visiting the url .. So If I have /api/v1/messages/?chat_session=14 which return a list of messages, I want to be able to do the same by code (and not by fetching the url with curl or something please).
Note :
definition of ModelResource.obj_get from https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py
def obj_get(self, request=None, **kwargs):
"""
A ORM-specific implementation of ``obj_get``.
Takes optional ``kwargs``, which are used to narrow the query to find
the instance.
"""
try:
base_object_list = self.get_object_list(request).filter(**kwargs)
object_list = self.apply_authorization_limits(request, base_object_list)
stringified_kwargs = ', '.join(["%s=%s" % (k, v) for k, v in kwargs.items()])
if len(object_list) <= 0:
raise self._meta.object_class.DoesNotExist("Couldn't find an instance of '%s' which matched '%s'." % (self._meta.object_class.__name__, stringified_kwargs))
elif len(object_list) > 1:
raise MultipleObjectsReturned("More than '%s' matched '%s'." % (self._meta.object_class.__name__, stringified_kwargs))
return object_list[0]
except ValueError:
raise NotFound("Invalid resource lookup data provided (mismatched type).")
So here I found the solution, the problem was with url resolving ... I needed to add
def get_resource_uri(self, bundle_or_obj):
return '/api/v1/%s/%s/' % (self._meta.resource_name,bundle_or_obj.obj.id)
to the related object (session here) in order for it to work (don't ask why!)
So here is my working solution for renderDetail and renderList :
def renderDetail(self,pkval):
request = HttpRequest()
request.GET = {'format': 'json'}
resp = self.get_detail(request, pk=pkval)
return resp.content
def renderList(self,options={}):
request = HttpRequest()
request.GET = {'format': 'json'}
if len(options) > 0:
request.GET.update(options)
resp = self.get_list(request)
return resp.content
And here is an example usage :
cmr = ChatMessageResource()
dataOne= cmr.renderDetail("723")
dataAll = cmr.renderList({'limit':'0','chat_session':cur_sess.pk})
https://github.com/toastdriven/django-tastypie/issues/962
I've found that obj_get method needs a bundled request object. See the link.
def user_detail(request, username):
ur = UserResource()
# Add this request bundle to the obj_get() method as shown.
req_bundle = ur.build_bundle(request=request)
user = ur.obj_get(req_bundle, username=username)
....
Your problem seems to be here:
data = self.obj_get(None,pk=pkval)
The parameters to obj_get should be kwargs that can be passed directly to a standard get. None should not be in there.

Categories

Resources