in order to generate the full url in the email I send to the ticket purchaser, I have to access get_current_site in my models.py. To do so I pass request like this: get_absolute_url(request)
Is there anything wrong in doing it that way, or would you consider this as good code/approach?
def checkout_page(request):
[...]
#Send email
subject = render_to_string('checkout/email/your_ticket_subject.txt', {})
to_email = [transaction_profile.cleaned_data['email'],]
email_ticket_link = t.get_absolute_url(request, email=True)
content = render_to_string('checkout/email/your_ticket_message.txt', {'email_ticket_link': email_ticket_link,})
send_mail(subject, content, settings.DEFAULT_FROM_EMAIL, to_email)
ticket_link = t.get_absolute_url(request)
return redirect(ticket_link)
models.py
class TicketPurchase(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE) #change CASCADE
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE) #change CASCADE
ticketpurchase_id = models.CharField(max_length=10, unique=True)
refunded = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
def get_absolute_url(self, request, email=False):
ticket_link = reverse("ticketsgenerator:show_tickets",
kwargs={"order_id": self.order.order_id, "customer_key": self.order.customer_key}
)
if email:
current_site = get_current_site(request)
return 'https://{}{}'.format(current_site, ticket_link)
return ticket_link
Related
I am currently challenge with a like functionality on a group page for that I'm creating, the problem is that when a first user of the group post on the group page other users can comment and like at his post, but as soon as a second user of the same group post, then when the post get liked or commented on, it throws thess error DoesNotExist at /group/3/. i wonder why am getting this error, because when the first user posted it didn't throw that error, when his post got liked or commented on, but the error only shows up when a another user on the same group post and it got like or commented on. I would appreciate some help. Here is my like function view.
def like(request, pk):
user = request.user
post = Post.objects.get(pk=pk)
group = GroupEx.objects.get(group=pk)
liked= False
like = Like.objects.filter(username=user, post=post,group=group)
if like:
like.delete()
else:
liked = True
Like.objects.create(username=user, post=post, group=group)
resp = {
'liked':liked
}
response = json.dumps(resp)
return redirect('group:main', pk=pk)
return HttpResponse(response,content_type = "application/json")
Here is my comment view.
def comment(request, pk):
context = {}
user = request.user
post = get_object_or_404(Post, pk=pk)
group = get_object_or_404(GroupEx,pk=pk)
if request.method == 'POST':
form = NewCommentForm(request.POST)
if form.is_valid():
data = form.save(commit=False)
data.post = post
data.group = group
data.username = user
data.save()
return redirect('group:main', pk=pk)
else:
form = NewCommentForm()
context['form'] = form
return render(request,'group/grouptimeline.html',context)
Here this model for the group post .
class Post(models.Model):
group = models.ForeignKey(GroupEx, on_delete=models.CASCADE, related_name="group_post")
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE ,related_name="group_user")
description = models.TextField(max_length=500, blank=True)
pic = models.ImageField(upload_to='path/post/img' ,blank=True)
date_posted = models.DateTimeField(auto_now_add=True)
tags = models.CharField(max_length=100, blank=True)
class Comment(models.Model):
group = models.ForeignKey(GroupEx, on_delete=models.CASCADE, related_name="group_comment")
post = models.ForeignKey(Post, related_name='g_comments', on_delete=models.CASCADE, null=True, blank=True)
username = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='g_comments', on_delete=models.CASCADE,null=True)
comment = models.CharField(max_length=5000)
class Like(models.Model):
group = models.ForeignKey(GroupEx, on_delete=models.CASCADE, related_name="group_liker")
username = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='g_likes', on_delete=models.CASCADE)
post = models.ForeignKey(Post, related_name='g_likes', on_delete=models.CASCADE)
like_date = models.DateTimeField(auto_now=True)
comment_date = models.DateTimeField(auto_now_add=True)
I'm working on a timesheet based system currently. I am getting a Foreign Key constraint failure when I am trying to assign the foreign key of one model to the other one.
Here are the two models
class Timesheet(models.Model):
id = models.CharField(primary_key=True, max_length=50, blank=True, unique=True, default=uuid.uuid4)
First_Name = models.CharField(max_length=32)
Last_Name = models.CharField(max_length=32)
Date_Created = models.DateField(auto_now_add=True)
Creator = models.ForeignKey(User, on_delete=models.DO_NOTHING)
Approved = models.BooleanField(default=False)
Company_Relationship = (
('Supplier', 'Supplier'),
('Contractor', 'Contractor'),
('Employee', 'Employee')
)
Worker_Type = models.CharField(max_length=32, choices=Company_Relationship)
Total_Days_Worked = models.DecimalField(decimal_places=2, max_digits=3)
class Meta:
ordering = ['-id']
#unique_together = ['Creator', 'Work_Week']
def get_absolute_url(self):
return reverse('timesheet-detail', kwargs={'pk': self.pk})
class InternalSheet(models.Model):
id = models.CharField(primary_key=True, max_length=50, blank=True, unique=True, default=uuid.uuid4)
Timesheet_id = models.ForeignKey(Timesheet, on_delete=models.DO_NOTHING)
Working_For = (
('7', 'Seven'),
('i', 'intelligent'),
('S', 'Sata'),
)
iPSL = (
('R16.1', 'Release 16.1'),
('R16', 'Release 16')
)
Company_name = models.CharField(max_length=5, choices=Working_For)
Company_name_change = models.CharField(max_length=5, choices=Working_For)
Internal_Company_Role = models.CharField(max_length=10, choices=iPSL)
DaysWorked = models.DecimalField(decimal_places=2, max_digits=3)
Managers = (
('GW', 'Greg Wood'),
('JC', 'Jamie Carson')
)
ManagerName = models.CharField(max_length=8, choices=Managers)
Approved = models.BooleanField(default=False)
def get_absolute_url(self):
return reverse('sheet-detail', kwargs={'pk': self.pk})
My issue is that I am getting a foreign key failure using this post request.
class TimesheetCreateView(LoginRequiredMixin, CreateView):
"""
Creates view and send the POST request of the submission to the backend.
"""
def get(self, request, *args, **kwargs):
internal_form_loop = create_internal_form_for_context()
context = {'form': CreateTimesheetForm(), 'UserGroup': User()}
context.update(internal_form_loop)
print("new", context)
return render(request, 'create.html', context)
def post(self, request, *args, **kwargs):
form = CreateTimesheetForm(request.POST)
internal_form_1 = CreateInternalSheetForm(request.POST)
if form.is_valid():
print("forms valid")
external_timesheet = form.save(commit=False)
print("self", self.request.user)
print("id", Timesheet.id)
external_timesheet.Creator = self.request.user
external_timesheet.save()
else:
print("Error Here")
if internal_form_1.is_valid():
print("Internal form valid")
internal = internal_form_1.save(commit=False)
internal.Timesheet_id_id = Timesheet.id
internal.id = uuid.uuid4()
internal.save()
return HttpResponseRedirect(reverse_lazy('timesheet-detail', args=[Timesheet.id]))
return render(request, 'create.html', {'form': form, 'UserGroup': User()})
It is failing on the line internal.save(). If I print the line internal.Timesheet_id_id I get a value like this, <django.db.models.query_utils.DeferredAttribute object at 0x000001580FDB75E0>. I'm guessing this is the issue? I need the actual Foreign key and not the location of that object. How do I do this. Thanks.
Figured out the issue, I had to replace the lines
internal.Timesheet_id_id = Timesheet.id
internal.id = uuid.uuid4()
internal.save()
with
internal.Timesheet_id_id = Timesheet.objects.get(id=external_timesheet.id)
internal.save()
name 'blog_posts__author' is not defined
In my project, anyone can submit any interest to any post, limited to 1 interest per post. Right now, i am trying to query the TOTAL number of interests (that is not accepted yet, meaning the status is still pending) that has been sent to any of my post, like all of them added up together, and I want that to be displayed in my account page. Is that possible to query it from the template based on the current code i have, and how can i do it? I have been trying the past hour but it hasn't be successful :( All help is appreciated thank you!
models.py
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
class BlogPost(models.Model):
title = models.CharField(max_length=50, null=False, blank=False, unique=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
body = models.TextField(max_length=5000, null=False, blank=False)
class Interest(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
blog_post = models.ForeignKey(BlogPost, on_delete=models.CASCADE)
my_interest = models.TextField(max_length=5000, null=False, blank=False)
class InterestInvite(models.Model):
ACCEPT = "ACCEPT"
DECLINE = "DECLINE"
PENDING = "PENDING"
STATUS_CHOICES = [
(ACCEPT, "accept"),
(DECLINE, "decline"),
(PENDING, "pending"),
]
interest = models.OneToOneField(Interest, on_delete=models.CASCADE, related_name="interest_invite")
status = models.CharField(max_length=25, choices=STATUS_CHOICES, default=PENDING)
views.py
def account_view(request, *args, **kwargs):
context = {}
user_id = kwargs.get("user_id")
account = Account.objects.get(pk=user_id)
context['account'] = account
blog_posts = BlogPost.objects.filter(author=account).order_by('-date_published')
total_interests = Interest.objects.filter(blog_post__author==request.user).count()
context['total_interests'] = total_interests
context['blog_posts'] = blog_posts
return render(request, "account/account.html", context)
Try this:
def account_view(request, *args, **kwargs):
context = {}
user_id = kwargs.get("user_id")
account = Account.objects.get(pk=user_id)
context['account'] = account
blog_posts = BlogPost.objects.filter(author=account).order_by('-date_published')
total_count_of_interests = 0
for post in blog_posts:
post_interests_count = Interest.objects.filter(blog_post=post, my_interest__status='pedning').count()
total_count_of_interests += post_interests_count
context['total_count_of_interests '] = total_count_of_interests
context['blog_posts'] = blog_posts
return render(request, "account/account.html", context)
P.S. calling count() on a QuerySet is a great practice if you only want to get the number of entries in it because it doesn't hit the objects in the database but only returns the total count of them.
i'm sending notifications to a user via django notifications. And i have username regex working on the html so anyone comments with #username it will post and the html is linkable so click on the #username it will take him to the username profile page. Now i am using django signals to match the username and print out the username. but when i use notify to send the notification. it does not work.
models.py:
class Comment(models.Model):
post = models.ForeignKey(post, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(User, on_delete=models.CASCADE)
reply = models.ForeignKey('Comment', null=True, related_name='replies', on_delete=models.CASCADE)
content = models.TextField()
image = models.ImageField(upload_to='comments-pics', null=True, blank=True)
voice_record = models.FileField(upload_to='voice-comments', null=True, blank=True)
timestamp = models.DateTimeField(auto_now_add=True)
def __str__ (self):
return '{}.{}'.format(self.post.title, str(self.user.username))
def save(self, *args, **kwargs):
super(Comment, self).save(*args, **kwargs)
def Comment_save_receiver(sender, instance, created, *args,**kwargs):
if created and not instance.parent:
user_regex = r'#(?P<username>[\w.#+-]+)'
m = re.search(user_regex, instance.content)
if m:
try:
recipient = User.objects.get(username=m.group('username'))
except (User.DoesNotExist, User.MultipleObjectsReturned):
pass
else:
notify.send(instance.user, recipient=recipient, actor=instance.user, verb='mention you in a post', target=instance, nf_type='tagged_by_one_user')
post_save.connect(Comment_save_receiver, sender=post)
the problem has been solved. i did wrong with the signal portion. it will be:
def comment_save_receiver(sender, instance, created, *args,**kwargs):
if created:
user_regex = r'#(?P<username>[\w.#+-]+)'
m = re.search(user_regex, instance.content)
if m:
try:
recipient = User.objects.get(username=m.group('username'))
except (User.DoesNotExist, User.MultipleObjectsReturned):
pass
else:
notify.send(instance.user, recipient=recipient, actor=instance.user, verb='mention you in a post', target=instance.post, nf_type='tagged_by_one_user')
post_save.connect(comment_save_receiver, sender=Comment)
I created a custom user model called Agent by extending AbstractUser. Now for some reason, my signup page is stuck and I can't figure out why (it was working fine before I created the custom user). When I click the Sign Up button, the page is stuck on Waiting for localhost...
There are 2 additional models on top of Agent that are created during registration - AgentBasicInfo and AgentPremiumInfo. AgentBasicInfo is displayed on the sign up page, while AgentPremiumInfo is created in the background, and not actually displayed during registration.
When I check my admin page, I see that an Agent model has been created, but no AgentBasicInfo and AgentPremiumInfo instances have been created. This leads me to believe something is getting stuck at or after agent_basic_info = basic_info_form.save(commit=False), but I can't figure out what it is.
Here is my code:
views.py
def signup(request):
if request.user.is_authenticated:
return HttpResponseRedirect('../dashboard/')
if request.method == 'POST':
signup_form = SignupForm(request.POST)
basic_info_form = AgentBasicInfoForm(request.POST)
if signup_form.is_valid() and basic_info_form.is_valid():
agent = signup_form.save(commit=False)
agent.is_active = False
agent.save()
# Creates a basic info form with user input
agent_basic_info = basic_info_form.save(commit=False)
agent_basic_info.agent = agent
agent_basic_info = agent_basic_info.save()
# Creates a profile model with the agent's premium information, empty except for 'agent' field. No actual form displayed on sign up page.
agent_premium_info = AgentPremiumInfo.objects.create(agent=agent)
agent_premium_info.save()
current_site = get_current_site(request)
message = render_to_string('acc_active_email.html', {
'agent':agent,
'domain':current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(agent.pk)),
'token': account_activation_token.make_token(agent),
})
mail_subject = 'Activate your blog account.'
to_email = signup_form.cleaned_data.get('email')
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
else:
signup_form = SignupForm()
basic_info_form = AgentBasicInfoForm()
return render(request, 'signup.html', {'signup_form': signup_form, 'basic_info_form': basic_info_form})
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
agent = Agent.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, Agent.DoesNotExist):
agent = None
if agent is not None and account_activation_token.check_token(agent, token):
agent.is_active = True
agent.save()
login(request, agent)
# return redirect('home')
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')
models.py
class Agent(AbstractUser):
pass
class AgentBasicInfo(models.Model):
TITLE = (
('Salesperson', 'Salesperson'),
('Sales Representative', 'Sales Representative'),
('Broker', 'Broker'),
('Broker of Record', 'Broker of Record'),
)
agent = models.OneToOneField(Agent, on_delete=models.CASCADE)
agent_first_name = models.CharField(max_length=30)
agent_last_name = models.CharField(max_length=30)
agent_preferred_email = models.EmailField()
office_phone_number = models.CharField(max_length=10)
agent_brokerage = models.CharField(max_length=50)
agent_title = models.CharField(max_length=20, choices=TITLE)
class AgentPremiumInfo(models.Model):
agent = models.OneToOneField(Agent, on_delete=models.CASCADE)
agent_phone_number = models.CharField(max_length=10, blank=True, null=True)
agent_website = models.CharField(max_length=50, blank=True, null=True)
agent_biography = models.TextField(blank=True, null=True)
agent_address_street = models.CharField(max_length=50, blank=True, null=True)
agent_address_city = models.CharField(max_length=25, blank=True, null=True)
agent_address_province = models.CharField(max_length=2, choices=PROVINCE, blank=True, null=True) # Add province choices later
agent_address_postal_code = models.CharField(max_length=6, blank=True, null=True)
agent_picture = models.ImageField(height_field=200, width_field=100, blank=True, null=True)
forms.py
class SignupForm(UserCreationForm):
email = forms.EmailField(max_length=200, help_text='Required')
def clean_email(self):
data = self.cleaned_data['email']
if not data.endswith('#gmail.com'):
raise forms.ValidationError("You must use your #gmail.com Email")
return data
class Meta:
model = Agent
fields = ('username', 'email', 'password1', 'password2')
class AgentBasicInfoForm(forms.ModelForm):
class Meta:
model = AgentBasicInfo
fields = ['agent_first_name', 'agent_last_name', 'agent_preferred_email', 'office_phone_number', 'agent_brokerage', 'agent_title']
class AgentPremiumInfoForm(forms.ModelForm):
class Meta:
model = AgentPremiumInfo
fields = ['agent_phone_number', 'agent_website', 'agent_biography', 'agent_picture', 'agent_address_street', 'agent_address_city', 'agent_address_province', 'agent_address_postal_code']
It seems something was wrong in the database despite me doing a reset_db. I did another reset_db and it magically fixed the issue.