如何在PIN激活过程中为来宾用户自动创建配置文件



我正在Django中的一个事件票务应用程序中工作,其中客人需要一个充值PIN在门户网站上进行验证,以便在他们购买PIN的事件中预订或预订座位。我还希望对这些来宾进行身份验证,以便我也能够对它们进行授权管理。

在我的模型中,我有一个配置文件模型,其中我使用信号在Admin用户创建时自动创建用户配置文件,并且我在创建新用户表单中也有三个用户组,具有相应的特权;组织者,员工和客人是组。我还开发了一个Django CreateUserForm类使用UserCreationForm模块在我的forms.py中,我有一个下拉的所有这些组的Admin添加用户(注册页是由Admin只能访问)使用Admin仪表板。

我的问题是,我希望每个PIN是由客人验证能够立即创建他/她的配置文件的PIN验证使用信号,但我不知道如何做到这一点。

我对我的CreateUserForm类感到困惑,因为所有的组都在下拉列表中列出。不知道如何在表单内使用条件来确定是否从来宾激活PIN,以便来宾组将是唯一的选项,一旦它是Admin,所有的用户组将被填充。

下面是我的Profile Model代码:

GENDER = (
('Male', 'Male'),
('Female', 'Female'),
)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
surname = models.CharField(max_length=20, null=True)
othernames = models.CharField(max_length=40, null=True)
gender = models.CharField(max_length=6, choices=GENDER, blank=True, null=True)
phone = PhoneNumberField()
image = models.ImageField(default='avatar.jpg', blank=False, null=False, upload_to ='profile_images', 
)

下面是我的用户创建表单代码:

class CreateUserForm(UserCreationForm):
email = forms.EmailField
group = forms.ModelChoiceField(queryset=Group.objects.all(),
required=True)
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2', 'group']

这是我的Pin激活在views.py:

def pin_activation(request):
if request.method == "POST":

#Create new form with name form
form = PinActivationForm(request.POST)
#Check if the the form has valid data in it
if form.is_valid():
#Check the status of the user Pin with the one in the Database
check_pin_status = Pin.objects.filter(value=form['pin'].value(), status='Not Activated')
#Check with the Status of the Pin is Right
if check_pin_status:
#Update the User Pin with a new status
Pin.objects.filter(value=form['pin'].value()).update(status='Activated')
#Message the User
messages.success(request, 'Pin Activated Successfully')
#Redirect the user
return redirect('user-register')

else:
messages.error(request, 'Pin Already Activated.You are Logged In.')
return redirect('guest-index')
else:
messages.error(request, 'Something Went Wrong. Try again')
else:
form = PinActivationForm()
context = {
'form':form,
}
return render(request, 'user/pin_activation.html', context)

应该有人慷慨地帮助以最好的方式处理这类问题。由于

我能够通过在pin动作之后立即添加注册表单来解决这个问题,以便来宾将使用用户和密码注册,然后使用相同的配置文件信号自动创建猜测配置文件。下面是代码:

def pin_activation(request):
if request.method == "POST":

#Create new form with name form
form = PinActivationForm(request.POST)
#Check if the the form has valid data in it
if form.is_valid():
#Check the status of the user Pin with the one in the Database
check_pin_status = Pin.objects.filter(value=form['pin'].value(), status='Not Activated')
#Check if the PIN is correct and NOT ACTIVATED
if check_pin_status:
#Update the User Pin with a new status of Activated
Pin.objects.filter(value=form['pin'].value()).update(status='Activated')
#Message the User
messages.success(request, 'Pin Activated Successfully')
#Redirect the user
return redirect('register-guest')

elif Pin.objects.filter(value=form['pin'].value(), status="Activated"):
messages.error(request, 'Pin Already Activated.')
return redirect('user-profile-update')
else:
messages.error(request, "Invalid PIN")
return redirect('user-login')


else:
messages.error(request, 'Something Went Wrong. Try again')
else:
form = PinActivationForm()
context = {
'form':form,
}
return render(request, 'user/pin_activation.html', context)

来宾注册功能

def register_guest(request):
#Create variable and query all users
workers = User.objects.all()
page_title = "Event Guest Register"
if request.method == 'POST':
form = GuestUserForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Registered Successfully.')
return redirect('dashboard-index')
else:
form = GuestUserForm()
context = {
'form':form,
'workers':workers,
'page_title':page_title,
}
return render(request, 'user/register.html', context)

Guest User Registration Form in forms.py

class GuestUserForm(UserCreationForm):
email = forms.EmailField
#Get Guest Group from DB
group = forms.ModelMultipleChoiceField(
queryset=Group.objects.filter(name='Guest'),
initial=Group.objects.filter(name='Guest'),
disabled=True
)

class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2', 'group']

My PIN Model

class Pin(models.Model):
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
value = models.CharField(max_length=6, default=generate_pin, blank=True)
added = models.DateTimeField(auto_now_add=True,  blank=False)
reference = models.UUIDField(primary_key = True, editable = False, default=uuid.uuid4)
status = models.CharField(max_length=20, default='Not Activated')
#Save Reference Number
def save(self, *args, **kwargs):
self.reference == str(uuid.uuid4())
super().save(*args, **kwargs)
def __unicode__(self):
return self.ticket
class Meta:
unique_together = ["ticket", "value"]
def __str__(self):
return f"{self.ticket} | {self.value}"

我现在遇到的唯一问题是,每当客人激活Pin,但无法继续注册或未完成注册表格并注销或关闭浏览器时,他/她的Pin将显示Pin已经激活,并且由于我在更新配置文件功能上使用Login Required Decorator,因此他不允许更新他/她的配置文件,因为他还不是用户,即使激活功能重定向客人更新他或她的配置文件。

确保没有用户注册的任何PIN激活都不是有效的PIN激活的逻辑是什么?或者这个用例的最佳解决方案是什么?

相关内容

  • 没有找到相关文章

最新更新