Commit 9e4e9597 authored by Skia's avatar Skia

Add the merge user function

parent f1e54439
Pipeline #237 failed with stage
in 8 seconds
......@@ -6,7 +6,6 @@
{% endblock %}
{% block content %}
<div id="user_profile_container">
<div id="user_profile">
<div id="right_column">
......
......@@ -12,6 +12,7 @@
<ul>
{% if user.is_root %}
<li><a href="{{ url('core:group_list') }}">{% trans %}Groups{% endtrans %}</a></li>
<li><a href="{{ url('rootplace:merge') }}">{% trans %}Merge users{% endtrans %}</a></li>
{% endif %}
{% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_root %}
<li><a href="{{ url('subscription:subscription') }}">{% trans %}Subscriptions{% endtrans %}</a></li>
......
......@@ -44,6 +44,14 @@ class Customer(models.Model):
raise ValidationError(_("Not enough money"))
super(Customer, self).save(*args, **kwargs)
def recompute_amount(self):
self.amount = 0
for r in self.refillings.all():
self.amount += r.amount
for s in self.buyings.filter(payment_method="SITH_ACCOUNT"):
self.amount -= s.quantity * s.unit_price
self.save()
class ProductType(models.Model):
"""
This describes a product type
......
This diff is collapsed.
from django.contrib import admin
# Register your models here.
from django.db import models
# Create your models here.
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Merge users{% endtrans %}
{% endblock %}
{% block content %}
<h3>{% trans %}Merge two users{% endtrans %}</h3>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p() }}
<p><input type="submit" value="{% trans %}Merge{% endtrans %}" /></p>
</form>
{% endblock %}
from django.test import TestCase
# Create your tests here.
from django.conf.urls import url, include
from rootplace.views import *
urlpatterns = [
url(r'^merge$', MergeUsersView.as_view(), name='merge'),
]
from django.shortcuts import render
from django.utils.translation import ugettext as _
from django.views.generic.edit import FormView
from django.core.urlresolvers import reverse
from django import forms
from django.core.exceptions import PermissionDenied
from ajax_select.fields import AutoCompleteSelectField
from core.models import User
from subscription.models import Subscriber
from counter.models import Customer
def merge_users(u1, u2):
u1.nick_name = u1.nick_name or u2.nick_name
u1.date_of_birth = u1.date_of_birth or u2.date_of_birth
u1.home = u1.home or u2.home
u1.sex = u1.sex or u2.sex
u1.tshirt_size = u1.tshirt_size or u2.tshirt_size
u1.role = u1.role or u2.role
u1.department = u1.department or u2.department
u1.dpt_option = u1.dpt_option or u2.dpt_option
u1.semester = u1.semester or u2.semester
u1.quote = u1.quote or u2.quote
u1.school = u1.school or u2.school
u1.promo = u1.promo or u2.promo
u1.forum_signature = u1.forum_signature or u2.forum_signature
u1.second_email = u1.second_email or u2.second_email
u1.phone = u1.phone or u2.phone
u1.parent_phone = u1.parent_phone or u2.parent_phone
u1.address = u1.address or u2.address
u1.parent_address = u1.parent_address or u2.parent_address
u1.save()
for u in u2.godfathers.all():
u1.godfathers.add(u)
u1.save()
for i in u2.invoices.all():
for f in i._meta.local_fields: # I have sadly not found anything better :/
if f.name == "date":
f.auto_now = False
u1.invoices.add(i)
u1.save()
s1 = Subscriber.objects.filter(id=u1.id).first()
s2 = Subscriber.objects.filter(id=u2.id).first()
for s in s2.subscriptions.all():
s1.subscriptions.add(s)
s1.save()
c1 = Customer.objects.filter(user__id=u1.id).first()
c2 = Customer.objects.filter(user__id=u2.id).first()
if c1 and c2:
for r in c2.refillings.all():
c1.refillings.add(r)
c1.save()
for s in c2.buyings.all():
c1.buyings.add(s)
c1.save()
elif c2 and not c1:
c2.user = u1
c1 = c2
c1.save()
c1.recompute_amount()
u2.delete()
return u1
class MergeForm(forms.Form):
user1 = AutoCompleteSelectField('users', label=_("User that will be kept"), help_text=None, required=True)
user2 = AutoCompleteSelectField('users', label=_("User that will be deleted"), help_text=None, required=True)
class MergeUsersView(FormView):
template_name = "rootplace/merge.jinja"
form_class = MergeForm
def dispatch(self, request, *arg, **kwargs):
res = super(MergeUsersView, self).dispatch(request, *arg, **kwargs)
if request.user.is_root:
return res
raise PermissionDenied
def form_valid(self, form):
self.final_user = merge_users(form.cleaned_data['user1'], form.cleaned_data['user2'])
return super(MergeUsersView, self).form_valid(form)
def get_success_url(self):
return reverse('core:user_profile', kwargs={'user_id': self.final_user.id})
......@@ -54,6 +54,7 @@ INSTALLED_APPS = (
'eboutic',
'launderette',
'api',
'rootplace',
)
MIDDLEWARE_CLASSES = (
......
......@@ -29,6 +29,7 @@ handler404 = "core.views.not_found"
urlpatterns = [
url(r'^', include('core.urls', namespace="core", app_name="core")),
url(r'^rootplace/', include('rootplace.urls', namespace="rootplace", app_name="rootplace")),
url(r'^subscription/', include('subscription.urls', namespace="subscription", app_name="subscription")),
url(r'^club/', include('club.urls', namespace="club", app_name="club")),
url(r'^counter/', include('counter.urls', namespace="counter", app_name="counter")),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment