How to implement user-friends relationship in cakephp? - cakephp

I am new to cakephp.
I am working on my Social network project.I am having users and friends relationship
concept.
I am having problem in sending friend request and accepting .....
I am not understanding how to start on this concept...
If anybody worked on such things then please help me..

You could make a new FriendRequest model.
Give this model belongTo User and hasOne Friend. Also, User hasMany FriendRequest Then, give the FriendRequest model a boolean field (cakePHP uses TINYINT(1) for boolean) to track acceptance.
Create a new FriendRequest record when a user sends a request.
List all FriendRequest records that a user has sent by searching for the ones the user hasMany
List all the FriendRequest records that are sent to a person by searching for all Friend belongTo User.
Change the acceptance field to TRUE when a User accepts the request.

Related

Salesforce Email logging on custom object

could really use some serious help on this. Below is the current business case:
We have the standard accounts, contacts, and opportunities objects. We've also created a custom object, and let's call it 'Customers.'
As of now, 'customers' are related to accounts.
Our users have einstein activity capture on, so all the email interactions get logged to the 'Account' level that the contact is related to, which makes sense.
The problem is that I want those emails tracked on the 'Customer' activity feed too, not just the 'Account' or 'Contact' activity feed.
Will creating a junction object between contacts and customers allow this activity to be shown in the 'customer' field? Or is there a simpler way to do this? Thank you in advance as this is a major roadblock I am facing right now.
I haven't worked with Einstein Activity Capture yet. What does it save stuff as? EmailMessage? Task? Either allows adding custom fields so you could add lookup to your Customer__c. With Task it's even simpler, it has that mutant lookup thing, Customer should appear as available option if it has "Allow Activities" ticked in setup.
Assuming that gives you something - next step would be to maybe make a custom quick action with some fields prepopulated, maybe a trigger to go "up" to Account and then to Customer(s)... Because out of the box
https://help.salesforce.com/articleView?id=aac_limitations.htm&type=5
Custom objects aren’t supported. When emails are sent from a custom
object, the email is logged on the activity timeline of the associated
contact.
You can upvote an idea: https://trailblazer.salesforce.com/ideaView?id=0873A000000EAIiQAO

Django, relate User with another table

So I got the tables you can see in the image below:
.
What I would like to do is to create a relationship so that each user (of django auth_user) will be enrolled(or able to enrol) to exactly one "course" so that he will be able to see next events for his modules.
Do I have to create another table and place 2 foreign keys or this is a way to do it in 'php' and it's more simple with Django? I was suggested to create 'student' model inheriting from 'User' with extended behavior and one to many relationship on auth. I tried to do that but unfortunately had not results since I'm really new to Django & Python.
If every auth_user (or auth.User) will be or have the opportunity to be enrolled on a course I would create a 'user profile' model that has a 1-to-1 relationship with the django User model. You can store additional User data in this model, including what course they are enrolled on. See https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model for more details but here is an example:
class UserProfile(models.Model):
user = models.OneToOneField('auth.User')
course = models.ForeignKey('courseapp.Course', null=True)
You would probably need to create a signal that gets fired each time an auth.User object is saved, such that if it is the first time that User object has been saved, it automatically creates the UserProfile:
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from yourusersapp.models import UserProfile
def create_user_profile(sender, instance, created, **kwargs):
# Automatically creates a UserProfile on User creation.
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
When you query a User object, you can then reference the User object's profile like:
user_object.userprofile
You could then create a Course object and link the user_object indirectly via its UserProfile to that Course:
course = Course.objects.create(name='course_name', next_field='whatever')
user_profile = user_object.userprofile
userprofile.course = course
userprofile.save()
Now you have a user object with a UserProfile that is linked to only 1 course. Many users can be on the same course, but a user can only be on 1 course. You can also reference all users on a particular course like:
course = Course.objects.get(name='course_name')
course_users = course.userprofile_set.all()
HTH
I think that you can go about this one of two ways.
Extend the User model. 'Student' would probably be a good name for your new model. It would have a OneToOne relationship with 'User', and a ForeignKey relationship with 'Course'. It can store any other information that is applicable to students only. Documentation for how to do that can be found here https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#extending-the-existing-user-model
Create a custom User model that has a ForeignKey relationship with Course. This approach is a bit more complicated, but yields a slightly cleaner end result. Documentation for that is here. https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#substituting-a-custom-user-model
Sorry if it seems like I'm just sending you to the Django docs, but both of those sections are well written and should explain things pretty clearly. If you'd like to post another question with example code we can try and see why your original attempt at extending the User model didn't work. By the way, your "Student" model shouldn't have to inherit from the User model in order to extend it.

What should I use as an Access Control Object in CakePHP?

I am quite new to ACL
The website is about cars where a dealer can have several brands and in several countries.
For example:
Dealer: John Doe
has the brands Audi in UK, and Kia in France.
I have the below models:
- Brand
- Country
- Dealer
- BrandsCountriesDealers (which links the above three together)
The dealer cannot add / edit a brand or a country. He can only add/edit/delete a car in the country that he has access to and to the brand that he owns.
I was wondering if I use a specific foreign key of the BrandsCountriesDealers model as an ACO only and when the dealer adds a car, I check if this car's brand and country are valid by checking the record in the BrandsCountriesDealers model which he has access to ?
I hope this was clear.
Implementing ACL is a bit hard but not impossible. Once you get use to working with ACL you undrestand how easy and powerful it is. It might take some research to configure and learn it but it is really worthed.
I think you should use ACL for certain reasons:
It makes your code very clean and tidy:
You don't need to keep assign
if user logged in do this
or if user type is this do that
Access Level: No need to write validation for each types of user.
High Security: ACL is working with Auth component
each method can be granted to one or more user type (Role)
Almost every thing is stored in DB so your code is clear again
I remember when I started to work with ACL I followed this tutorial. It is in Portuguese language. I don't know this language but I followed step by step and get it worked.
http://www.youtube.com/watch?v=EIjfwqqGRhs

Save an id from a related model on save

I'm sure there is a very simple solution to this, but the explanation is complex, so please do bear with me.
I have a registration form Registration::add() and it comprises of three sections.
The first is the Registration data, a number of fields.
The second is checkboxes from three related HABTM models, Role, Category and CategoryChild
Third is creating a record in User
My Registration model has a user_id and I need to save the generated User.id into that field.
I'm sure that I should be able to do this using the model relationships, as the User model saves the User.registration_id fine, but it doesn't seem to be writing into the Registration model.
Do I need to create another field in my add.ctp view so that the field is present in the $this->data->request array? I would much rather do this, than get embroiled in faffing with beforeSave() and afterSave().
So do I just need to create
$this->Form->input('Registration.user_id', array('type'=>'hidden'));
My relationships, for reference,
Registration hasOne User
Registration hasAndBelongsToMany Category
Registration hasAndBelongsToMany CategoryChild
Registration hasAndBelongsToMany Role
User hasOne Registration
I'm currently using saveAll($this->request->data) to save all my data, and it's managing to save everything except Registration.user_id
Your Relationships have
Registration hasOne User
However you have both a Registration.user_id and a User.registration_id implying that you need hasAndBelongsToMany relationship.
Just in case you're not using the Cake Baker to generate the models it is very useful.
Yes, something like this should work as per your question:
<?php echo $form->input('Registration.user_id',array('type'=>'hidden', 'value' => $user_id)); ?>
Hope it helps

Elegant way to store anonymous users with nick names in django?

I have a simple Post model in my django app:
class Post(models.Model):
category = models.CharField(max_length=10, choices=choices)
message = models.CharField(max_length=500)
user = models.ForeignKey(User, editable=False)
I'd like to implement the feature of having anonymous users create posts with nick names. Unfortunately django doesn't allow you to save an instance of AnonymousUser as a foreignkey to the Post class.
I was thinking of adding a "dummy" user record into the db that represents the anonymous user(id=0, or some negative number if possible) that would be used for all posts without a user. And if it is present a nullable name field would be used to represent the nickname of the anonymous user.
This solution seems a bit hacky to me. Is there any cleaner more effecient solution?
If you can identify new users by some session information, you could just create normal user accounts, pro forma so to speak - with a flag to identify them as volatile (this may lead to some regular maintenance cleanup).
If, during session lifetime, the user actually want to register, you can reuse the user account on your side and the user can keep all his data on his.
As #slacy commented and #Dominique answered; instead of rolling your own take a look at existing projects, e.g. this:
http://www.stereoplex.com/blog/introducing-django-lazysignup
Not tested , but this can help:
https://github.com/danfairs/django-lazysignup
You can add blank=True and null=True to User ForeignKey and set it to None, if user is anonymous. You just need to store the nickname somewhere.
I am new to Django. A friend told me not to use ForeignKey further stating that using CharField is ok. ForeignKey is slower than CharField, as it has some check for user info.

Resources